def test_set_messages(): """Test the setMessages function.""" test_handler = Handler() messages = [[{'trigger': '/botname'}]] test_handler.set_messages(messages) assert test_handler.messages == messages assert test_handler.handler_help_command.get_help_command( ) == "*botname*\n\n"
def __call__(self, env, start_resp): env['CFG'] = self.cfg handler = Handler(env, start_resp) path_info = env.get('PATH_INFO', '/') func = self.routes.get(path_info) if func is not None: return handler.handle_func(func) return handler.handle_file(path_info)
def test_parse(): """Test the if the parse works as intended, replacing tags.""" test_handler = Handler() sender = "Tester" answer = 'hello, {user}! You tagged: {tag}' message = "yourself" message_template = "{tag}" sender = "tester" result = "hello, tester! You tagged: yourself" answer = test_handler.parse(answer, message_template, message, sender) assert result == answer
def test_botname_wrong_message_get_answer(): """Test the answer to a wrong message.""" test_handler = Handler() bot_name = "Test" messages = [[{'trigger': 'botname', 'answer': ['success!']}]] test_handler.set_botname(bot_name) test_handler.set_messages(messages) assert test_handler.check_message(bot_name + 'sssss', '') is None
def test_botname_right_message_get_answer(): """Test the answer to a right message.""" test_handler = Handler() bot_name = "Test" messages = [[{'trigger': 'botname', 'answer': ['success!']}]] test_handler.set_botname(bot_name) test_handler.set_messages(messages) assert test_handler.check_message(bot_name, '') == 'success!'
def test_botname_right_message(): """Test the reaction of the bot once a matching message is sent.""" test_handler = Handler() bot_name = message = "Test" messages = [[{'trigger': 'botname'}]] test_handler.set_botname(bot_name) test_handler.set_messages(messages) for message_list in test_handler.messages: for candidate_message in message_list: assert Matcher.matches(candidate_message, message, bot_name)
class BotManager(): """Manager class. Mandatory since some parameters must be set.""" messageHandler = Handler() triggers = None config = {} bot = None logger = None def init(self, logger, config_path, trigger_path): """Initialize the class.""" # notice the user that the process has begun logger.log("setting up the bot...") BotManager.__setup_logger(self, logger) BotManager.__setup_message_handler(logger) BotManager.__read_config_files(config_path, trigger_path) BotManager.__setup_bot_key() BotManager.__setup_bot_name() BotManager.__setup_bot_messages() @staticmethod def handle(msg): """Handle the message via messageHandler.""" BotManager.messageHandler.handle(msg, BotManager.bot) @staticmethod def __read_config_files(config_path, trigger_path): with open(config_path) as file: BotManager.config = json.load(file) with open(trigger_path) as file: BotManager.triggers = json.load(file) @staticmethod def __setup_message_handler(logger): """Setup the message hander.""" message_handler = Handler() message_handler.set_logger(logger) def __setup_logger(self, logger): """Setup the message handler and the logger.""" self.logger = logger @staticmethod def __setup_bot_key(): bot_key = BotManager.config['BOT_KEY'] BotManager.bot = telepot.Bot(bot_key) @staticmethod def __setup_bot_name(): bot_name = BotManager.bot.getMe()['username'] BotManager.messageHandler.set_botname(bot_name) @staticmethod def __setup_bot_messages(): """Split commands in arrays ordered by priority.""" config_splitter = splitter.Splitter() BotManager.triggers = config_splitter.split_by_priority( BotManager.triggers) BotManager.messageHandler.set_messages(BotManager.triggers) @staticmethod def start_looping(): """Start to listen for messages on telegram.""" bot_name = BotManager.bot.getMe()['username'] BotManager.bot.message_loop(BotManager.handle) Logger.log(bot_name + " is listening!")
def __setup_message_handler(logger): """Setup the message hander.""" message_handler = Handler() message_handler.set_logger(logger)
def test_set_botname(): """Test the setBotname function.""" test_handler = Handler() bot_name = "Test" test_handler.set_botname(bot_name) assert test_handler.botname == bot_name