bot.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import os
  2. import telebot
  3. from translations import translations
  4. from members import is_member
  5. token = os.environ['BOT_TOKEN']
  6. # with open('bot/bot-token', 'r', encoding='utf-8') as token_file:
  7. # token = token_file.readline()
  8. logger = telebot.logging.getLogger()
  9. telebot.logging.basicConfig(filename="logs/bot.log",
  10. datefmt='%d-%m-%Y %H:%M:%S',
  11. format='%(asctime)s - %(levelname)s - %(message)s',
  12. filemode='a+',
  13. encoding='utf-8',
  14. level=telebot.logging.DEBUG)
  15. telebot.apihelper.ENABLE_MIDDLEWARE = True
  16. MAIN_STATE = 'ON_MAIN_MENU'
  17. CHANGE_LANG = 'ON_LANG_MENU'
  18. SET_PRINTER = 'ON_PRINTER_MENU'
  19. NEW_PRINT = 'ON_PRINTING_MENU'
  20. SESSIONS = {}
  21. TRANSLATIONS = translations.load_tr()
  22. _lang = 'en'
  23. def activate(lang):
  24. global _lang
  25. _lang = lang
  26. def tr(string):
  27. """Returns string respectively to language"""
  28. try:
  29. return TRANSLATIONS[string][_lang]
  30. except KeyError as e:
  31. logger.error('%e: %s is not in TRANSLATIONS', e, string)
  32. translations.json_from_csv()
  33. try:
  34. tr_str = TRANSLATIONS[string][_lang]
  35. logger.info('%s found in TRANSLATIONS after regenerating', string)
  36. return tr_str
  37. except KeyError as ee:
  38. logger.fatal(
  39. '%e: %s is not in TRANSLATIONS after regenerating file', ee, string)
  40. return tr('not_tr')
  41. def get_or_create_session(user_id):
  42. try:
  43. return SESSIONS[user_id]
  44. except KeyError:
  45. SESSIONS[user_id] = {'state': MAIN_STATE}
  46. SESSIONS[user_id] = {'lang': 'en'}
  47. return SESSIONS[user_id]
  48. bot = telebot.TeleBot(token, parse_mode=None)
  49. @bot.middleware_handler(update_types=['message'])
  50. def set_session(bot_instance, message):
  51. bot_instance.session = get_or_create_session(message.from_user.id)
  52. @bot.middleware_handler(update_types=['message'])
  53. def activate_language(bot_instance, message):
  54. lang = message.from_user.language_code
  55. SESSIONS['lang'] = lang
  56. activate(lang)
  57. def not_member(message):
  58. user_id = message.from_user.id
  59. chat_id = message.chat.id
  60. sent_msg = bot.send_message(chat_id, tr('not_member'))
  61. bot.register_next_step_handler(sent_msg, send_instructions(message))
  62. logger.info("Access denied to user %s", user_id)
  63. @bot.message_handler(commands=['start'])
  64. def start(message):
  65. user_id = message.from_user.id
  66. chat_id = message.chat.id
  67. bot.session['state'] = MAIN_STATE
  68. sender_fullname = message.from_user.full_name
  69. bot.send_message(chat_id, tr('hello').format(sender_fullname))
  70. choose_language(message)
  71. send_instructions(message)
  72. @bot.message_handler(commands=['language'])
  73. def choose_language(message):
  74. bot.session['state'] = CHANGE_LANG
  75. user_id = message.from_user.id
  76. chat_id = message.chat.id
  77. markup = telebot.types.ReplyKeyboardMarkup(row_width=2,
  78. one_time_keyboard=True,
  79. resize_keyboard=True)
  80. itembtn1 = telebot.types.KeyboardButton("English")
  81. itembtn2 = telebot.types.KeyboardButton("Русский")
  82. markup.add(itembtn1, itembtn2)
  83. sent_markup = bot.send_message(chat_id, tr('choose_lang'), reply_markup=markup)
  84. bot.register_next_step_handler(sent_markup, set_language)
  85. logger.info('Waiting for language input')
  86. def set_language(message):
  87. user_id = message.from_user.id
  88. chat_id = message.chat.id
  89. if message.text in ('English', 'Русский'):
  90. bot.session['lang'] = message.text
  91. msg = bot.send_message(chat_id, tr('lang_chosen'))
  92. logger.info('Successfully changed lang to %s', message.text)
  93. else:
  94. msg = bot.send_message(chat_id, tr('wrong_lang'))
  95. bot.register_next_step_handler(msg, choose_language(message))
  96. logger.error('Wrong language choice')
  97. def send_instructions(message):
  98. user_id = message.from_user.id
  99. chat_id = message.chat.id
  100. with open('src/instruction.jpg', 'rb') as instr_pic_file:
  101. instruction_pic = instr_pic_file.read()
  102. bot.send_photo(chat_id, instruction_pic)
  103. bot.send_message(chat_id, tr('instruction').format(user_id))
  104. logger.info("Sent instructions")
  105. @bot.message_handler(commands=['help'])
  106. def send_help(message):
  107. user_id = message.from_user.id
  108. chat_id = message.chat.id
  109. bot.send_message(chat_id, tr('help'))
  110. logger.info("Sent help")
  111. @bot.message_handler(commands=['printer'])
  112. def choose_printer(message):
  113. bot.session['state'] = SET_PRINTER
  114. user_id = message.from_user.id
  115. chat_id = message.chat.id
  116. if not is_member(user_id):
  117. not_member(message)
  118. return
  119. markup = telebot.types.ReplyKeyboardMarkup(row_width=3,
  120. one_time_keyboard=True,
  121. resize_keyboard=True)
  122. itembtn1 = telebot.types.KeyboardButton("Entrance")
  123. itembtn2 = telebot.types.KeyboardButton("Coffee")
  124. itembtn3 = telebot.types.KeyboardButton("Library")
  125. markup.add(itembtn1, itembtn2, itembtn3)
  126. bot.send_message(chat_id, tr('choose_printer'), reply_markup=markup)
  127. bot.register_next_step_handler_by_chat_id(chat_id, set_printer)
  128. def set_printer(message):
  129. user_id = message.from_user.id
  130. chat_id = message.chat.id
  131. if not is_member(user_id):
  132. not_member(message)
  133. return
  134. if message.text in ('Entrance', 'Coffee', 'Library'):
  135. bot.session['printer'] = message.text
  136. bot.send_message(chat_id, tr('printer_chosen'))
  137. logger.info('Successfully set printer (%s)', message.text)
  138. else:
  139. bot.send_message(chat_id, tr('wrong_printer'))
  140. bot.register_next_step_handler_by_chat_id(
  141. chat_id, choose_printer(message))
  142. logger.error('Wrong printer choice')
  143. bot.infinity_polling()