From e1758945dea7ae537dd8e169209f125bce8e0443 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 17 Aug 2017 10:53:36 -0700 Subject: [PATCH] bots: Extract some functions to top-level. We'll want to use these functions for the embedded bots system. --- zulip_bots/zulip_bots/lib.py | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 51eb53b..11c8dd0 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -161,6 +161,27 @@ class StateHandler(object): yield new_state self.set_state(new_state) +def extract_query_without_mention(message, client): + # type: (Dict[str, Any], ExternalBotHandler) -> str + """ + If the bot is the first @mention in the message, then this function returns + the message with the bot's @mention removed. Otherwise, it returns None. + """ + bot_mention = r'^@(\*\*{0}\*\*)'.format(client.full_name) + start_with_mention = re.compile(bot_mention).match(message['content']) + if start_with_mention is None: + return None + query_without_mention = message['content'][len(start_with_mention.group()):] + return query_without_mention.lstrip() + +def is_private(message, client): + # type: (Dict[str, Any], ExternalBotHandler) -> bool + # bot will not reply if the sender name is the same as the bot name + # to prevent infinite loop + if message['type'] == 'private': + return client.full_name != message['sender_full_name'] + return False + def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): # type: (Any, bool, str) -> Any # @@ -183,27 +204,6 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): if not quiet: print(message_handler.usage()) - def extract_query_without_mention(message, client): - # type: (Dict[str, Any], ExternalBotHandler) -> str - """ - If the bot is the first @mention in the message, then this function returns - the message with the bot's @mention removed. Otherwise, it returns None. - """ - bot_mention = r'^@(\*\*{0}\*\*)'.format(client.full_name) - start_with_mention = re.compile(bot_mention).match(message['content']) - if start_with_mention is None: - return None - query_without_mention = message['content'][len(start_with_mention.group()):] - return query_without_mention.lstrip() - - def is_private(message, client): - # type: (Dict[str, Any], ExternalBotHandler) -> bool - # bot will not reply if the sender name is the same as the bot name - # to prevent infinite loop - if message['type'] == 'private': - return client.full_name != message['sender_full_name'] - return False - def handle_message(message): # type: (Dict[str, Any]) -> None logging.info('waiting for next message')