def main() -> None: args = parse_args() # NOTE: Use of only this implies bots from eg. registry cannot be explored in this way result = resolve_bot_path(args.bot) if result is None: print(f"Cannot find find and import bot '{args.bot}'") sys.exit(1) bot_path, bot_name = result bot_dir = os.path.dirname(bot_path) sys.path.insert(0, bot_dir) try: lib_module = import_module_from_source(bot_path.as_posix(), bot_name) if lib_module is None: raise OSError except OSError: print(f"Could not find and import bot '{bot_name}'") sys.exit(1) try: message_handler = lib_module.handler_class() except AttributeError: print( "This module does not appear to have a bot handler_class specified." ) sys.exit(1) message_server = MockMessageServer() bot_handler = TerminalBotHandler(args.bot_config_file, message_server) if hasattr(message_handler, "initialize") and callable( message_handler.initialize): message_handler.initialize(bot_handler) sender_email = "*****@*****.**" try: while True: content = input("Enter your message: ") message = message_server.send( dict( content=content, sender_email=sender_email, display_recipient=sender_email, )) message_handler.handle_message( message=message, bot_handler=bot_handler, ) except KeyboardInterrupt: print( "\n\nOk, if you're happy with your terminal-based testing, try it out with a Zulip server.", "\nYou can refer to https://zulip.com/api/running-bots#running-a-bot.", ) sys.exit(1)
def main() -> None: args = parse_args() bot_path, bot_name = finder.resolve_bot_path(args.bot) sys.path.insert(0, os.path.dirname(bot_path)) if args.provision: provision_bot(os.path.dirname(bot_path), args.force) try: lib_module = finder.import_module_from_source(bot_path, bot_name) except ImportError as e: req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt") with open(req_path) as fp: deps_list = fp.read() dep_err_msg = ( "ERROR: The following dependencies for the {bot_name} bot are not installed:\n\n" "{deps_list}\n" "If you'd like us to install these dependencies, run:\n" " zulip-run-bot {bot_name} --provision") print(dep_err_msg.format(bot_name=bot_name, deps_list=deps_list)) sys.exit(1) if lib_module is None: print("ERROR: Could not load bot module. Exiting now.") sys.exit(1) if not args.quiet: logging.basicConfig(stream=sys.stdout, level=logging.INFO) # It's a bit unfortunate that we have two config files, but the # alternative would be way worse for people running multiple bots # or testing against multiple Zulip servers. exit_gracefully_if_zulip_config_file_does_not_exist(args.config_file) exit_gracefully_if_bot_config_file_does_not_exist(args.bot_config_file) try: run_message_handler_for_bot(lib_module=lib_module, config_file=args.config_file, bot_config_file=args.bot_config_file, quiet=args.quiet, bot_name=bot_name) except NoBotConfigException: print(''' ERROR: Your bot requires you to specify a third party config file with the --bot-config-file option. Exiting now. ''') sys.exit(1)
def main(): args = parse_args() bot_path, bot_name = resolve_bot_path(args.bot) bot_dir = os.path.dirname(bot_path) sys.path.insert(0, bot_dir) try: lib_module = import_module_from_source(bot_path, bot_name) if lib_module is None: raise IOError except IOError: print("Could not find and import bot '{}'".format(bot_name)) sys.exit(1) try: message_handler = lib_module.handler_class() except AttributeError: print( "This module does not appear to have a bot handler_class specified." ) sys.exit(1) bot_handler = TerminalBotHandler(args.bot_config_file) if hasattr(message_handler, 'initialize') and callable( message_handler.initialize): message_handler.initialize(bot_handler) sender_email = '*****@*****.**' try: while True: content = input('Enter your message: ') message = dict( content=content, sender_email=sender_email, display_recipient=sender_email, ) message_handler.handle_message( message=message, bot_handler=bot_handler, ) except KeyboardInterrupt: print( "\n\nOk, if you're happy with your terminal-based testing, try it out with a Zulip server.", "\nYou can refer to https://zulipchat.com/api/running-bots#running-a-bot." ) sys.exit(1)
def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]: bots_lib_module = {} for bot in available_bots: try: if bot.endswith(".py") and os.path.isfile(bot): lib_module = import_module_from_source(bot, "custom_bot_module") else: module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot) lib_module = import_module(module_name) bots_lib_module[bot] = lib_module except ImportError: _, bots_lib_module[bot] = import_module_from_zulip_bot_registry( bot) if bots_lib_module[bot] is None: error_message = ( f'Error: Bot "{bot}" doesn\'t exist. Please make sure ' "you have set up the botserverrc file correctly.\n") if bot == "api": error_message += ( "Did you forget to specify the bot you want to run with -b <botname> ?" ) sys.exit(error_message) return bots_lib_module
def main() -> None: args = parse_args() if args.registry: try: bot_source, lib_module = finder.import_module_from_zulip_bot_registry( args.bot) except finder.DuplicateRegisteredBotName as error: print( f'ERROR: Found duplicate entries for "{error}" in zulip bots registry.\n' "Make sure that you don't install bots using the same entry point. Exiting now." ) sys.exit(1) if lib_module: bot_name = args.bot else: result = finder.resolve_bot_path(args.bot) if result: bot_path, bot_name = result sys.path.insert(0, os.path.dirname(bot_path)) if args.provision: provision_bot(os.path.dirname(bot_path), args.force) try: lib_module = finder.import_module_from_source( bot_path.as_posix(), bot_name) except ImportError: req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt") with open(req_path) as fp: deps_list = fp.read() dep_err_msg = ( "ERROR: The following dependencies for the {bot_name} bot are not installed:\n\n" "{deps_list}\n" "If you'd like us to install these dependencies, run:\n" " zulip-run-bot {bot_name} --provision") print( dep_err_msg.format(bot_name=bot_name, deps_list=deps_list)) sys.exit(1) bot_source = "source" else: lib_module = finder.import_module_by_name(args.bot) if lib_module: bot_name = lib_module.__name__ bot_source = "named module" if args.provision: print( "ERROR: Could not load bot's module for '{}'. Exiting now." ) sys.exit(1) if lib_module is None: print("ERROR: Could not load bot module. Exiting now.") sys.exit(1) if not args.quiet: logging.basicConfig(stream=sys.stdout, level=logging.INFO) # It's a bit unfortunate that we have two config files, but the # alternative would be way worse for people running multiple bots # or testing against multiple Zulip servers. exit_gracefully_if_zulip_config_is_missing(args.config_file) exit_gracefully_if_bot_config_file_does_not_exist(args.bot_config_file) try: run_message_handler_for_bot( lib_module=lib_module, config_file=args.config_file, bot_config_file=args.bot_config_file, quiet=args.quiet, bot_name=bot_name, bot_source=bot_source, ) except NoBotConfigException: print(""" ERROR: Your bot requires you to specify a third party config file with the --bot-config-file option. Exiting now. """) sys.exit(1)