main.py 2.2 KB

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