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.
This commit is contained in:
parent
2d785ea3d1
commit
60d4c343d7
|
@ -78,28 +78,13 @@ def run_message_handler_for_bot(lib_module, quiet, config_file):
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(message_handler.usage())
|
print(message_handler.usage())
|
||||||
|
|
||||||
def extract_message_if_mentioned(message, client):
|
def extract_message(message, client):
|
||||||
bot_mention = r'^@(\*\*{0}\*\*\s|{0}\s)(?=.*)'.format(client.full_name)
|
bot_mention = r'^@(\*\*{0}\*\*)'.format(client.full_name)
|
||||||
start_with_mention = re.compile(bot_mention).match(message['content'])
|
start_with_mention = re.compile(bot_mention).match(message['content'])
|
||||||
if start_with_mention:
|
if start_with_mention is None:
|
||||||
query = message['content'][len(start_with_mention.group()):]
|
return None
|
||||||
return query
|
query_without_mention = message['content'][len(start_with_mention.group()):]
|
||||||
else:
|
return query_without_mention
|
||||||
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
|
|
||||||
|
|
||||||
def is_private(message, client):
|
def is_private(message, client):
|
||||||
# bot will not reply if the sender name is the same as the bot name
|
# 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
|
# Strip at-mention botname from the message
|
||||||
if is_mentioned:
|
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:
|
if message['content'] is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue