@@ -199,6 +199,39 @@ def is_private(message, at_mention_bot_id):
199
199
return at_mention_bot_id != message ['sender_id' ]
200
200
return False
201
201
202
+ def get_message_content_if_bot_is_called (message , at_mention_bot_name , at_mention_bot_id ):
203
+ # type: (Dict[str, Any], str) -> Any
204
+ """
205
+ Check if the bot is called or not; a bot can be called by 2 ways: @mention-botname or private message
206
+ to the bot. Once it is confirmed if a bot is called or not, then we move to the second part of the
207
+ function.
208
+ If the bot is privately messaged, then the message content need not be modified and the bot can directly
209
+ process the entire message content.
210
+ If the bot is called by @mention-botname, then we need to remove @mention-botname for the bot to
211
+ process the rest of the message content.
212
+
213
+ This function is being leveraged by two systems; external bot system and embedded bot system,
214
+ any change/modification in the structure of this should be reflected at other places accordingly.
215
+ For details read "extract_query_without_mention" function docstring.
216
+ """
217
+ # is_mentioned is true if the bot is mentioned at ANY position (not necessarily
218
+ # the first @mention in the message).
219
+ is_mentioned = message ['is_mentioned' ]
220
+ is_private_message = is_private (message = message , at_mention_bot_id = at_mention_bot_id )
221
+
222
+ # Strip at-mention botname from the message
223
+ if is_mentioned :
224
+ # message['content'] will be None when the bot's @-mention is not at the beginning.
225
+ # In that case, the message shall not be handled.
226
+ message ['content' ] = extract_query_without_mention (message = message ,
227
+ at_mention_bot_name = at_mention_bot_name )
228
+ if message ['content' ] is None :
229
+ return
230
+
231
+ if (is_private_message or is_mentioned ):
232
+ return message ['content' ]
233
+ return None
234
+
202
235
def run_message_handler_for_bot (lib_module , quiet , config_file , bot_name ):
203
236
# type: (Any, bool, str) -> Any
204
237
#
@@ -225,20 +258,13 @@ def handle_message(message):
225
258
# type: (Dict[str, Any]) -> None
226
259
logging .info ('waiting for next message' )
227
260
228
- # is_mentioned is true if the bot is mentioned at ANY position (not necessarily
229
- # the first @mention in the message).
230
- is_mentioned = message ['is_mentioned' ]
231
- is_private_message = is_private (message , restricted_client .id )
261
+ message_content_if_bot_is_called = get_message_content_if_bot_is_called (message = message ,
262
+ at_mention_bot_name = restricted_client .full_name ,
263
+ at_mention_bot_id = restricted_client .id )
232
264
233
- # Strip at-mention botname from the message
234
- if is_mentioned :
235
- # message['content'] will be None when the bot's @-mention is not at the beginning.
236
- # In that case, the message shall not be handled.
237
- message ['content' ] = extract_query_without_mention (message = message , client = restricted_client )
238
- if message ['content' ] is None :
239
- return
265
+ if message_content_if_bot_is_called :
266
+ message ['content' ] = message_content_if_bot_is_called
240
267
241
- if is_private_message or is_mentioned :
242
268
message_handler .handle_message (
243
269
message = message ,
244
270
bot_handler = restricted_client ,
0 commit comments