123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import logging
- from aiogram import Bot, Dispatcher, executor, types
- from aiogram.dispatcher import FSMContext
- from config import BOT_TOKEN, logfile, ROOT_ID
- from parse_vedabase import get_full_verse
- from users import Users
- from memory import memory
- def split_big_text(text: str, num_symbols: int) -> list[str]:
- messages = [p for p in text.split('\n')]
- message = ''
- for p in messages:
- if len(message + p) < num_symbols:
- print('Yes')
- message += f'{p}\n'
- else:
- messages.append(message)
- message = ''
- if message != '':
- messages.append(message)
- return messages
- users_db = Users()
- logging.basicConfig(filename=logfile,
- format='%(asctime)s %(levelname)-2s %(message)s',
- filemode='a',
- datefmt='%Y-%m-%d %H:%M:%S',
- level=logging.INFO)
- bot = Bot(token=BOT_TOKEN)
- dp = Dispatcher(bot)
- @dp.message_handler(commands=['start', 'help'])
- async def send_welcome(message: types.Message):
- """
- Обрабатывает команды `/start` и `/help`
- """
- # user_info = [
- # str(message.from_user.id),
- # message.from_user.first_name,
- # dict(message.from_user).get(
- # 'last_name', 'None'),
- # dict(message.from_user).get('username', 'None')
- # ]
- users_db.add_user(message.from_user.id)
- await message.reply("""To find the verse from the scripture, enter:
- - start with 'verse '
- - 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')
- BG: 'verse ru bg 10.8'
- SB: 'verse uk sb 1.10.8'
- CC (adi, madhya, antya): 'verse en cc adi 10.8'""")
- @dp.message_handler(lambda message: message.text.lower().startswith("verse "))
- async def search_verse(message: types.Message):
- message_text = message.text.lower()
- if len(message_text.split()) < 4 or len(message_text.split()) > 5:
- return await message.reply('Wrong query string. Try /help command')
- query_string = message_text.split('verse ')[1]
- verse = get_full_verse(query_string)
- if 'errors' in verse:
- logging.warning(
- f"{message_text.split('verse ')[1]} - {' | '.join(verse['errors'])}")
- await message.reply('\n'.join(verse['errors']))
- else:
- if verse['purport_id'] == '':
- watch_purport = ''
- else:
- watch_purport = f"\n===============\nClick to read purport /purport_{verse['purport_id']}"
- await message.reply('\n\n'.join(list(verse.values())[:-1]) + watch_purport)
- @dp.message_handler(lambda message: message.text.startswith("/message "))
- async def sendind_to_users(message: types.Message):
- if message.from_user.id == ROOT_ID:
- for user in users_db.users:
- await bot.send_message(user, message.text.replace('/message ', ''))
- @dp.message_handler(lambda message: message.text.startswith("/purport_"))
- async def sendind_purport(message: types.Message):
- user_id = message.from_user.id
- purport_id = message.text.replace('/purport_', '')
- try:
- purport = memory.get(purport_id).decode("utf-8")
- except AttributeError:
- 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')
- else:
- try:
- await message.reply(purport)
- except aiogram.utils.exceptions.MessageIsTooLong:
- messages = split_big_text(purport, 4096)
- for m in messages:
- await bot.send_message(user_id, m)
- @dp.message_handler()
- async def echo(message: types.Message):
- await message.answer("Do not understand this message. Try /help command", reply_markup=None)
- if __name__ == '__main__':
- executor.start_polling(dp, skip_updates=True)
|