bot.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os
  2. import telebot
  3. from latinica import Converter
  4. bot = telebot.TeleBot(os.getenv('TELEGRAM_API_TOKEN'), parse_mode='MARKDOWN')
  5. @bot.message_handler(commands=['start', 'help'])
  6. def handle_start(message):
  7. bot.reply_to(message, 'Danný bot sozdan dlja togo, čtoby perevodit́ tekst na russkom jazyke s kirillicy na latinicu.\nPrimer ispoĺzovanija:\n/cy2la Привет, мир!')
  8. @bot.message_handler(commands=['cy2la'])
  9. def handle_cy2la(message):
  10. text = ' '.join(message.text.split(' ')[1:]).strip()
  11. if len(text) < 1:
  12. bot.reply_to(message, 'Tekst soobŝenija ne dolžen byt́ pustym.')
  13. return
  14. converter = Converter(text)
  15. result = converter.convert()
  16. result = result.replace('```', '`').strip()
  17. bot.reply_to(message, f'Rezultat:\n```\n{result}```')
  18. @bot.inline_handler(lambda _: True)
  19. def query_text(inline_query):
  20. text = inline_query.query.strip()
  21. converter = Converter(text)
  22. result = converter.convert()
  23. try:
  24. r = telebot.types.InlineQueryResultArticle('1', result, telebot.types.InputTextMessageContent(result))
  25. bot.answer_inline_query(inline_query.id, [r])
  26. except:
  27. pass
  28. bot.infinity_polling()