main.py 2.3 KB

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