vanitybot.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import re
  2. import logging
  3. import asyncio
  4. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  5. from aiogram import Bot, Dispatcher, executor, types
  6. from aiogram_dialog import DialogManager, DialogRegistry, StartMode, BaseDialogManager
  7. from src.config import API_TOKEN
  8. from src.dialog import dialog, MySG
  9. from src.progress import bg_dialog, TrickyUser
  10. from src.summarize import get_paper_desc
  11. # Initialize bot and dispatcher
  12. bot = Bot(token=API_TOKEN) # type: ignore
  13. dp = Dispatcher(bot, storage=MemoryStorage())
  14. registry = DialogRegistry(dp)
  15. dp.middleware.setup(TrickyUser(1))
  16. registry.register(dialog)
  17. registry.register(bg_dialog)
  18. help_message = "Hello!\n\n\
  19. Send me a link paper from arxiv.org and \
  20. I'll send you back snippet of paper and arxiv-vanity.com mobile friendly link!\n\
  21. Or add me to chat and I'll be watching the arxiv link and \
  22. reply to them with fancy arxiv-vanity links.\n\
  23. Also, I can get summary and highlight of a paper for you."
  24. async def deploy_message():
  25. await bot.send_message(chat_id=1147194, text='The deployment has been performed')
  26. @dp.message_handler(commands=['start', 'help'])
  27. async def process_start_command(message: types.Message):
  28. await message.reply(help_message)
  29. @dp.message_handler(regexp=r'arxiv.org\/(?:abs|pdf)\/\d{4}\.\d{5}')
  30. async def vanitify(message: types.Message, dialog_manager: DialogManager):
  31. papers_ids = re.findall(r'arxiv.org\/(?:abs|pdf)\/(\d{4}\.\d{5}[v]?[\d]?)', message.text)
  32. async def start_dialog(manager=dialog_manager.bg(), state=MySG.main, mode=StartMode.NEW_STACK, data={}):
  33. await manager.start(state=state, mode=mode, data=data)
  34. async def get_paper_abs(id_):
  35. reply_message = f"[Here you maybe can read the paper in mobile friendly way](https://www.arxiv-vanity.com/papers/{id_})"
  36. data = {
  37. "id": id_,
  38. "reply_message": reply_message,
  39. "url": None,
  40. "title": None,
  41. "abs": None
  42. }
  43. if paper := await get_paper_desc(id_):
  44. id_, url, title, abstract, authors = paper.values()
  45. abstract = re.sub(r"\$.*\$", "[replaced mathsf formulae]", abstract)
  46. reply_message = f'{url}\n\n***{title}***\n\n{abstract}\n\n{reply_message}'
  47. data.update({
  48. "id": id_,
  49. "reply_message": reply_message,
  50. "url": url,
  51. "title": title,
  52. "abs": abstract,
  53. "authors": authors
  54. })
  55. else:
  56. reply_message = f'Something went wrong. Can not reach arxiv.com :('
  57. data["reply_message"] = reply_message
  58. return data
  59. list_data = await asyncio.gather(*[get_paper_abs(id_) for id_ in papers_ids])
  60. asyncio.gather(*[start_dialog(data=data) for data in list_data])
  61. async def main():
  62. await deploy_message()
  63. await dp.skip_updates()
  64. await dp.start_polling()
  65. if __name__ == "__main__":
  66. logging.basicConfig(level=logging.INFO)
  67. # logging.getLogger("asyncio").setLevel(logging.DEBUG)
  68. # logging.getLogger("aiogram_dialog").setLevel(logging.DEBUG)
  69. # asyncio.get_event_loop().run_until_complete(deploy_message())
  70. # executor.start_polling(dp, skip_updates=True)
  71. asyncio.run(main())