Skip to content

Commit c5bb72e

Browse files
committed
bots: Modify is_private function to take id as the input.
This is done so that Embedded bot system can also make use of this function directly, as only id is needed in this function. Also, there would be ambiguity as what type of BotHandler is passed in the arguments: EmbeddedBotHandler or ExternalBotHandler.
1 parent 3559b98 commit c5bb72e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

zulip_bots/zulip_bots/lib.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,18 @@ def extract_query_without_mention(message, client):
175175
query_without_mention = message['content'][len(start_with_mention.group()):]
176176
return query_without_mention.lstrip()
177177

178-
def is_private(message, client):
179-
# type: (Dict[str, Any], ExternalBotHandler) -> bool
180-
# bot will not reply if the sender id is the same as the bot id
181-
# to prevent infinite loop
178+
def is_private(message, at_mention_bot_id):
179+
# type: (Dict[str, Any], int) -> bool
180+
"""
181+
This function is to ensure that the bot doesn't go into infinite loop if the message sender id is
182+
the same as the id of the bot which is called. This function makes the bot not reply to itself.
183+
184+
This function is being leveraged by two systems; external bot system and embedded bot system,
185+
any change/modification in the structure of this should be reflected at other places accordingly.
186+
For details read "extract_query_without_mention" function docstring.
187+
"""
182188
if message['type'] == 'private':
183-
return client.id != message['sender_id']
189+
return at_mention_bot_id != message['sender_id']
184190
return False
185191

186192
def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name):
@@ -212,7 +218,7 @@ def handle_message(message):
212218
# is_mentioned is true if the bot is mentioned at ANY position (not necessarily
213219
# the first @mention in the message).
214220
is_mentioned = message['is_mentioned']
215-
is_private_message = is_private(message, restricted_client)
221+
is_private_message = is_private(message, restricted_client.id)
216222

217223
# Strip at-mention botname from the message
218224
if is_mentioned:

0 commit comments

Comments
 (0)