lib: Add support for extended mention syntax.
We are checking for the extended syntax only on the basis of the user id so that we can handle cases where a bot is renamed.
This commit is contained in:
parent
4fcd593da2
commit
fbe99b812e
|
@ -5,6 +5,7 @@ import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
from typing import Any, Optional, List, Dict, IO, Text
|
from typing import Any, Optional, List, Dict, IO, Text
|
||||||
|
@ -238,10 +239,18 @@ def extract_query_without_mention(message: Dict[str, Any], client: ExternalBotHa
|
||||||
If the bot is the first @mention in the message, then this function returns
|
If the bot is the first @mention in the message, then this function returns
|
||||||
the stripped message with the bot's @mention removed. Otherwise, it returns None.
|
the stripped message with the bot's @mention removed. Otherwise, it returns None.
|
||||||
"""
|
"""
|
||||||
|
content = message['content']
|
||||||
mention = '@**' + client.full_name + '**'
|
mention = '@**' + client.full_name + '**'
|
||||||
if not message['content'].startswith(mention):
|
extended_mention_regex = re.compile(r'^@\*\*.*\|' + str(client.user_id) + r'\*\*')
|
||||||
return None
|
extended_mention_match = extended_mention_regex.match(content)
|
||||||
return message['content'][len(mention):].lstrip()
|
|
||||||
|
if extended_mention_match:
|
||||||
|
return content[extended_mention_match.end():].lstrip()
|
||||||
|
|
||||||
|
if content.startswith(mention):
|
||||||
|
return content[len(mention):].lstrip()
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def is_private_message_from_another_user(message_dict: Dict[str, Any], current_user_id: int) -> bool:
|
def is_private_message_from_another_user(message_dict: Dict[str, Any], current_user_id: int) -> bool:
|
||||||
|
|
|
@ -4,6 +4,7 @@ from zulip_bots.lib import (
|
||||||
ExternalBotHandler,
|
ExternalBotHandler,
|
||||||
StateHandler,
|
StateHandler,
|
||||||
run_message_handler_for_bot,
|
run_message_handler_for_bot,
|
||||||
|
extract_query_without_mention
|
||||||
)
|
)
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
@ -192,6 +193,23 @@ class LibTest(TestCase):
|
||||||
|
|
||||||
client.upload_file.assert_called_once_with(file)
|
client.upload_file.assert_called_once_with(file)
|
||||||
|
|
||||||
|
def test_extract_query_without_mention(self):
|
||||||
|
client = FakeClient()
|
||||||
|
handler = ExternalBotHandler(
|
||||||
|
client=client,
|
||||||
|
root_dir=None,
|
||||||
|
bot_details=None,
|
||||||
|
bot_config_file=None
|
||||||
|
)
|
||||||
|
message = {'content': "@**Alice** Hello World"}
|
||||||
|
self.assertEqual(extract_query_without_mention(message, handler), "Hello World")
|
||||||
|
message = {'content': "@**Alice|alice** Hello World"}
|
||||||
|
self.assertEqual(extract_query_without_mention(message, handler), "Hello World")
|
||||||
|
message = {'content': "@**Alice Renamed|alice** Hello World"}
|
||||||
|
self.assertEqual(extract_query_without_mention(message, handler), "Hello World")
|
||||||
|
message = {'content': "Not at start @**Alice|alice** Hello World"}
|
||||||
|
self.assertEqual(extract_query_without_mention(message, handler), None)
|
||||||
|
|
||||||
def _create_client_and_handler_for_file_upload(self):
|
def _create_client_and_handler_for_file_upload(self):
|
||||||
client = FakeClient()
|
client = FakeClient()
|
||||||
client.upload_file = MagicMock()
|
client.upload_file = MagicMock()
|
||||||
|
|
Loading…
Reference in a new issue