2 Commits a17b490bb2 ... 52d744fb1d

Autor SHA1 Mensaje Fecha
  foreverpositive 52d744fb1d New structure hace 2 años
  foreverpositive bc6c7dc6fa Added primitive logging, better language selection hace 2 años
Se han modificado 6 ficheros con 114 adiciones y 3 borrados
  1. 3 0
      .gitignore
  2. 12 3
      README.md
  3. 92 0
      bot.py
  4. 1 0
      requirements.txt
  5. BIN
      src/instruction.jpg
  6. 6 0
      src/phrases.csv

+ 3 - 0
.gitignore

@@ -1,3 +1,6 @@
+TOKEN.secret
+TODO.md
+
 ### IDEs
 
 .idea/

+ 12 - 3
README.md

@@ -4,8 +4,17 @@ Telegram bot that helps faculty members of New Phystech work with faculty's publ
 
 This bot can:
 
--
--
--
+- Print text directly from chat
+- Print PDF files
+- Ask to convert non-PDF files to PDF and then print them
+
+To activate the bot, please do this:
+
+```
+python -m venv venv
+. venv/bin/activate
+pip install -r requirements.txt
+python bot.py
+```
 
 Licensed under GNU General Public License v3.0.

+ 92 - 0
bot.py

@@ -0,0 +1,92 @@
+import csv
+import telebot
+
+logger = telebot.logging.getLogger()
+telebot.logging.basicConfig(filename="logs/bot.log",
+                            datefmt='%d-%m-%Y %H:%M:%S',
+                            format='%(asctime)s - %(levelname)s - %(message)s',
+                            filemode='a+', encoding='utf-8',
+                            level=telebot.logging.DEBUG)
+
+with open('TOKEN.secret', 'r', encoding='utf-8') as token_file:
+    TOKEN = token_file.readline()
+
+bot = telebot.TeleBot(TOKEN, parse_mode=None)
+LANG = "English"
+
+phrases = {}
+with open('src/phrases.csv', 'r', encoding='utf-8') as phrases_file:
+    reader = csv.reader(phrases_file)
+    for row in reader:
+        phrases[row[0]] = {'English': row[1], 'Русский': row[2]}
+
+
+@bot.message_handler(commands=['start'])
+def send_welcome(message, language=LANG):
+    chat_id = message.chat.id
+    sender_fullname = message.from_user.full_name
+
+    hello_str = phrases['hello'][language].format(sender_fullname)
+    sent_msg = bot.send_message(chat_id, hello_str)
+
+    change_language(sent_msg)
+
+    bot.register_next_step_handler(sent_msg, send_instructions)
+
+
+def set_language(message):
+    global LANG
+
+    chat_id = message.chat.id
+
+    if message.text in ('English', 'Русский'):
+        LANG = message.text[:]
+        lang_chosen_str = phrases['lang_chosen'][LANG]
+        bot.send_message(chat_id, lang_chosen_str)
+        logger.info('Successfully changed language')
+    else:
+        wrong_lang_str = phrases['wrong_lang'][LANG]
+        bot.send_message(chat_id, wrong_lang_str)
+        logger.error('Wrong language choice')
+
+
+@bot.message_handler(commands=['language'])
+def change_language(message, language=LANG):
+    chat_id = message.chat.id
+
+    markup = telebot.types.ReplyKeyboardMarkup(row_width=2,
+                                               one_time_keyboard=True,
+                                               resize_keyboard=True)
+    itembtn1 = telebot.types.KeyboardButton("English")
+    itembtn2 = telebot.types.KeyboardButton("Русский")
+    markup.add(itembtn1, itembtn2)
+    choose_lang_str = phrases['choose_lang'][language]
+
+    sent_choice = bot.send_message(chat_id, choose_lang_str, reply_markup=markup)
+
+    bot.register_next_step_handler(sent_choice, set_language)
+    logger.info('Waiting for input')
+
+
+def send_instructions(message, language=LANG):
+    chat_id = message.chat.id
+    user_id = message.from_user.id
+
+    with open('src/instruction.jpg', 'rb') as instr_pic_file:
+        instruction_pic = instr_pic_file.read()
+
+    bot.send_photo(chat_id, instruction_pic)
+    instr_str = phrases['instruction'][language].format(user_id)
+    bot.send_message(chat_id, instr_str)
+    logger.info("Sent instructions")
+
+
+@bot.message_handler(commands=['help'])
+def send_help(message, language=LANG):
+    help_str = phrases['help'][language]
+    chat_id = message.chat.id
+    bot.send_message(chat_id, help_str)
+    logger.info("Sent help")
+
+
+bot.infinity_polling()

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+pyTelegramBotAPI

BIN
src/instruction.jpg


+ 6 - 0
src/phrases.csv

@@ -0,0 +1,6 @@
+"hello","Hello, {}!","Привет, {}!"
+"choose_lang","Please choose the language:","Пожалуйста, выберите язык:"
+"lang_chosen","Language has been succesfully set!","Язык был успешно изменён!"
+"instruction","Are you first time here? If so, please paste your Telegram ID ({}) to your profile here: https://physics.itmo.ru/","Вы здесь впервые? Если так, то, пожалуйста, вставьте ваш Telegram ID ({}) в профиль на сайте факультета: https://physics.itmo.ru/"
+"help","This is a bot that helps faculty members of New Phystech work with faculty's public printers. Here you can easily choose a method of printing, which data will be printed with selected printer.","Данный бот помогает сотрудникам Нового Физтеха ФТФ ИТМО работать с общими принтерами факультета. Здесь вы можете ввести информацию, которую нужно распечатать, или отправить файл для печати, а также выбрать, на каком принтере будет происходить печать."
+"wrong_lang","Wrong language chosen. You can always change it using /language command.","Выбран неправильный язык. Вы всегда можете изменить его, используя команду /language."