sastrabot.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import logging
  2. from aiogram import Bot, Dispatcher, executor, types
  3. from aiogram.dispatcher import FSMContext
  4. from config import BOT_TOKEN, logfile, ROOT_ID
  5. from parse_vedabase import get_full_verse
  6. from users import Users
  7. from memory import memory
  8. users_db = Users()
  9. logging.basicConfig(filename=logfile,
  10. format='%(asctime)s %(levelname)-2s %(message)s',
  11. filemode='a',
  12. datefmt='%Y-%m-%d %H:%M:%S',
  13. level=logging.INFO)
  14. bot = Bot(token=BOT_TOKEN)
  15. dp = Dispatcher(bot)
  16. @dp.message_handler(commands=['start', 'help'])
  17. async def send_welcome(message: types.Message):
  18. """
  19. Обрабатывает команды `/start` и `/help`
  20. """
  21. # user_info = [
  22. # str(message.from_user.id),
  23. # message.from_user.first_name,
  24. # dict(message.from_user).get(
  25. # 'last_name', 'None'),
  26. # dict(message.from_user).get('username', 'None')
  27. # ]
  28. users_db.add_user(message.from_user.id)
  29. await message.reply("""To find the verse from the scripture, enter:
  30. - start with 'verse '
  31. - then enter the desired language ('en', 'nl', 'ru', 'da', 'et', 'sk', 'es', 'de', 'uk', 'lt', 'sl', 'fi', 'cs', 'hu', 'fr', 'ko', 'pt-br', 'bg', 'ja', 'zu')
  32. BG: 'verse ru bg 10.8'
  33. SB: 'verse uk sb 1.10.8'
  34. CC (adi, madhya, antya): 'verse en cc adi 10.8'""")
  35. @dp.message_handler(lambda message: message.text.lower().startswith("verse "))
  36. async def search_verse(message: types.Message):
  37. message_text = message.text.lower()
  38. if len(message_text.split()) < 4 or len(message_text.split()) > 5:
  39. return await message.reply('Wrong query string. Try /help command')
  40. query_string = message_text.split('verse ')[1]
  41. verse = get_full_verse(query_string)
  42. if 'errors' in verse:
  43. logging.warning(
  44. f"{message_text.split('verse ')[1]} - {' | '.join(verse['errors'])}")
  45. await message.reply('\n'.join(verse['errors']))
  46. else:
  47. if verse['purport_id'] == '':
  48. watch_purport = ''
  49. else:
  50. watch_purport = f"\n===============\nClick to read purport /purport_{verse['purport_id']}"
  51. await message.reply('\n\n'.join(list(verse.values())[:-1]) + watch_purport)
  52. @dp.message_handler(lambda message: message.text.startswith("/message "))
  53. async def sendind_to_users(message: types.Message):
  54. if message.from_user.id == ROOT_ID:
  55. for user in users_db.users:
  56. await bot.send_message(user, message.text.replace('/message ', ''))
  57. @dp.message_handler(lambda message: message.text.startswith("/purport_"))
  58. async def sendind_purport(message: types.Message):
  59. purport_id = message.text.replace('/purport_', '')
  60. try:
  61. await message.reply(memory.get(purport_id))
  62. except KeyError:
  63. await message.reply(f'The link is out of date. You need to re-enter the verse number to getting text and comment.\nEx. verse en sb 1.10.8')
  64. @dp.message_handler()
  65. async def echo(message: types.Message):
  66. await message.answer("Do not understand this message. Try /help command", reply_markup=None)
  67. if __name__ == '__main__':
  68. executor.start_polling(dp, skip_updates=True)