From 60d4c343d783c5e706cd4b0c5a92d01a1c087029 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Tue, 23 May 2017 01:14:30 +0200 Subject: [PATCH] bots: Allow empty bot queries. This commit allows bots to be adressed with empty queries, that is, by just @-mentioning them. @-mentioning is now the only way to adress a bot. --- contrib_bots/bot_lib.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/contrib_bots/bot_lib.py b/contrib_bots/bot_lib.py index 52d45b1..8b085d3 100644 --- a/contrib_bots/bot_lib.py +++ b/contrib_bots/bot_lib.py @@ -78,28 +78,13 @@ def run_message_handler_for_bot(lib_module, quiet, config_file): if not quiet: print(message_handler.usage()) - def extract_message_if_mentioned(message, client): - bot_mention = r'^@(\*\*{0}\*\*\s|{0}\s)(?=.*)'.format(client.full_name) + def extract_message(message, client): + bot_mention = r'^@(\*\*{0}\*\*)'.format(client.full_name) start_with_mention = re.compile(bot_mention).match(message['content']) - if start_with_mention: - query = message['content'][len(start_with_mention.group()):] - return query - else: - bot_response = 'Please mention me first, then type the query.' - if message['type'] == 'private': - client.send_message(dict( - type='private', - to=message['sender_email'], - content=bot_response, - )) - else: - client.send_message(dict( - type='stream', - to=message['display_recipient'], - subject=message['subject'], - content=bot_response, - )) - return None + if start_with_mention is None: + return None + query_without_mention = message['content'][len(start_with_mention.group()):] + return query_without_mention def is_private(message, client): # bot will not reply if the sender name is the same as the bot name @@ -116,7 +101,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file): # Strip at-mention botname from the message if is_mentioned: - message['content'] = extract_message_if_mentioned(message=message, client=restricted_client) + message['content'] = extract_message(message=message, client=restricted_client) if message['content'] is None: return