main.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import config
  2. from EchoBot import JabberBot
  3. import logging
  4. from argparse import ArgumentParser
  5. import importlib
  6. import asyncio
  7. if __name__ == '__main__':
  8. # Setup the command line arguments.
  9. parser = ArgumentParser(description=JabberBot.__doc__)
  10. # Output verbosity options.
  11. parser.add_argument("-q", "--quiet", help="set logging to ERROR",
  12. action="store_const", dest="loglevel",
  13. const=logging.ERROR, default=logging.INFO)
  14. parser.add_argument("-d", "--debug", help="set logging to DEBUG",
  15. action="store_const", dest="loglevel",
  16. const=logging.DEBUG, default=logging.INFO)
  17. # JID and password options.
  18. parser.add_argument("-b", "--bot", dest="bot",
  19. help="Bot name")
  20. args = parser.parse_args()
  21. # Setup logging.
  22. logging.basicConfig(level=args.loglevel,
  23. format='%(levelname)-8s %(message)s')
  24. if args.bot and args.bot in config.BOTLIST:
  25. module = importlib.import_module("handlers." + args.bot)
  26. start_handler = module.start_handler
  27. message_handler = module.message_handler
  28. bot = module.bot
  29. # Отправляет информацию о присутствии бота, чтобы показать,
  30. # что он онлайн и готов к взаимодействию.
  31. bot.send_presence()
  32. # bot.get_roster() # TODO: Возможно не нужно
  33. logging.info(f"Bot {bot.jid} started!")
  34. bot.add_event_handler("message", message_handler)
  35. # Connect to the XMPP server and start processing XMPP stanzas.
  36. bot.connect()
  37. start_handler()
  38. # TODO: Сделать корректную обработку остановки программы;
  39. # Ctrl+C, Ctrl+Z
  40. # systemctl stop с последующей отправкой кодов и корректной остановкой программы
  41. # bot.stop()
  42. asyncio.get_event_loop().run_forever()
  43. else:
  44. print("Select using -n flag for select availible bot from botlist:\n")
  45. for botname in config.BOTLIST:
  46. print(f"- {botname}")