main.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import csv
  2. import telebot
  3. logger = telebot.logging.getLogger()
  4. telebot.logging.basicConfig(filename="logs/bot.log",
  5. datefmt='%d-%m-%Y %H:%M:%S',
  6. format='%(asctime)s - %(levelname)s - %(message)s',
  7. filemode='a+', encoding='utf-8',
  8. level=telebot.logging.DEBUG)
  9. with open('TOKEN.secret', 'r', encoding='utf-8') as token_file:
  10. TOKEN = token_file.readline()
  11. bot = telebot.TeleBot(TOKEN, parse_mode=None)
  12. LANG = "English"
  13. phrases = {}
  14. with open('phrases.csv', 'r', encoding='utf-8') as phrases_file:
  15. reader = csv.reader(phrases_file)
  16. for row in reader:
  17. phrases[row[0]] = {'English': row[1], 'Русский': row[2]}
  18. @bot.message_handler(commands=['start'])
  19. def send_welcome(message, language=LANG):
  20. chat_id = message.chat.id
  21. sender_fullname = message.from_user.full_name
  22. hello_str = phrases['hello'][language].format(sender_fullname)
  23. sent_msg = bot.send_message(chat_id, hello_str)
  24. change_language(sent_msg)
  25. bot.register_next_step_handler(sent_msg, send_instructions)
  26. def set_language(message):
  27. global LANG
  28. chat_id = message.chat.id
  29. if message.text in ('English', 'Русский'):
  30. LANG = message.text[:]
  31. lang_chosen_str = phrases['lang_chosen'][LANG]
  32. bot.send_message(chat_id, lang_chosen_str)
  33. logger.info('Successfully changed language')
  34. else:
  35. wrong_lang_str = phrases['wrong_lang'][LANG]
  36. bot.send_message(chat_id, wrong_lang_str)
  37. logger.error('Wrong language choice')
  38. @bot.message_handler(commands=['language'])
  39. def change_language(message, language=LANG):
  40. chat_id = message.chat.id
  41. markup = telebot.types.ReplyKeyboardMarkup(row_width=2,
  42. one_time_keyboard=True,
  43. resize_keyboard=True)
  44. itembtn1 = telebot.types.KeyboardButton("English")
  45. itembtn2 = telebot.types.KeyboardButton("Русский")
  46. markup.add(itembtn1, itembtn2)
  47. choose_lang_str = phrases['choose_lang'][language]
  48. sent_choice = bot.send_message(chat_id, choose_lang_str, reply_markup=markup)
  49. bot.register_next_step_handler(sent_choice, set_language)
  50. logger.info('Waiting for input')
  51. def send_instructions(message, language=LANG):
  52. chat_id = message.chat.id
  53. user_id = message.from_user.id
  54. with open('instruction.jpg', 'rb') as instr_pic_file:
  55. instruction_pic = instr_pic_file.read()
  56. bot.send_photo(chat_id, instruction_pic)
  57. instr_str = phrases['instruction'][language].format(user_id)
  58. bot.send_message(chat_id, instr_str)
  59. logger.info("Sent instructions")
  60. @bot.message_handler(commands=['help'])
  61. def send_help(message, language=LANG):
  62. help_str = phrases['help'][language]
  63. chat_id = message.chat.id
  64. bot.send_message(chat_id, help_str)
  65. logger.info("Sent help")
  66. bot.infinity_polling()