Skip to content

Commit c915502

Browse files
committed
bots: Add docstring notes for the functions shared by two repo.
This is done so that any further modification or refactoring of such functions takes into account working in both the repository and does not lead to breaking of any flow.
1 parent 26b7500 commit c915502

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

zulip_bots/zulip_bots/lib.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ def extract_query_without_mention(message, at_mention_bot_name):
164164
"""
165165
If the bot is the first @mention in the message, then this function returns
166166
the message with the bot's @mention removed. Otherwise, it returns None.
167+
This function is being leveraged by two systems; external bot system and embedded bot system.
168+
This function is being called by:
169+
1. 'run_message_handler_for_bot' function (zulip_bots/lib.py file in zulip/python-zulip-api
170+
repository) that executes/runs/calls external bots.
171+
2. 'consume' function in EmbeddedBotWorker class (zerver/worker/queue_processors.py
172+
file in zulip/zulip repository) that executes/runs/calls embedded bots.
173+
174+
Since, this is a general utility function for any working bot, it is planned to be an independent
175+
function for now. Any refactoring should correctly be reflected in all the bot systems using this
176+
function.
167177
"""
168178
bot_mention = r'^@(\*\*{0}\*\*)'.format(at_mention_bot_name)
169179
start_with_mention = re.compile(bot_mention).match(message['content'])
@@ -174,20 +184,46 @@ def extract_query_without_mention(message, at_mention_bot_name):
174184

175185
def is_private(message, at_mention_bot_name):
176186
# type: (Dict[str, Any], str) -> bool
177-
# bot will not reply if the sender name is the same as the bot name
178-
# to prevent infinite loop
187+
"""
188+
This function is to ensure that the bot doesn't go into infinite loop if the sender name is
189+
the same as the bot name. This function makes the bot not reply to such a sender.
190+
191+
This function is being leveraged by two systems; external bot system and embedded bot system,
192+
any change/modification in the structure of this should be reflected at other places accordingly.
193+
For details read "extract_query_without_mention" function docstring.
194+
"""
179195
if message['type'] == 'private':
180196
return at_mention_bot_name != message['sender_full_name']
181197
return False
182198

183199
def initialize_config_bot(message_handler, bot_handler):
184200
# type: (Any) -> None
201+
"""
202+
If a bot has bot-specific configuration settings (both public or private) to be set, then this
203+
function calls the 'initialize' function which in turn calls 'get_config_info' for bots.
204+
205+
This function is being leveraged by two systems; external bot system and embedded bot system,
206+
any change/modification in the structure of this should be reflected at other places accordingly.
207+
For details read "extract_query_without_mention" function docstring.
208+
"""
185209
if hasattr(message_handler, 'initialize'):
186210
message_handler.initialize(bot_handler=bot_handler)
187211

188212
def get_message_content_if_bot_is_called(message, at_mention_bot_name):
189213
# type: (Dict[str, Any], str) -> Any
190-
#
214+
"""
215+
Check if the bot is called or not; a bot can be called by 2 ways: @mention-botname or private message
216+
to the bot. Once it is confirmed if a bot is called or not, then we move to the second part of the
217+
function.
218+
If the bot is privately messaged, then the message content need not be modified and the bot can directly
219+
process the entire message content.
220+
If the bot is called by @mention-botname, then we need to remove @mention-botname for the bot to
221+
process the rest of the message content.
222+
223+
This function is being leveraged by two systems; external bot system and embedded bot system,
224+
any change/modification in the structure of this should be reflected at other places accordingly.
225+
For details read "extract_query_without_mention" function docstring.
226+
"""
191227
# is_mentioned is true if the bot is mentioned at ANY position (not necessarily
192228
# the first @mention in the message).
193229
is_mentioned = message['is_mentioned']

0 commit comments

Comments
 (0)