Skip to content

Commit ab33f1a

Browse files
authored
Fix bot handling for non-classic/non-RTM bots. (#9)
* Fix bot handling for non-classic/non-RTM bots.
1 parent 2e906c8 commit ab33f1a

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

zdaemon/zdaemon.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,23 @@ def zdaemon_slack_router(triggers, ack, say, message):
234234
# print ("zdaemon_slack_router event: %s[%s]/%s" %
235235
# (message['type'], message['channel_type'],
236236
# message['subtype'] if 'subtype' in message else 'None'))
237+
# -- and/or --
238+
# print(message)
237239

238-
239-
bridge_bot_message = False
240+
bot_message = False # A message from any bot
241+
bridge_bot_message = False # A message specifically from the bridge bot
240242
if ('subtype' in message and
241-
message['subtype'] == 'bot_message' and
243+
message['subtype'] == 'bot_message' or
242244
'bot_id' in message):
245+
# Classic bots use the 'bot_message' subtype, but modern apps do not.
246+
# Regardless, all bots should include a bot_id.
247+
#
248+
# This check is therefore slightly more conservative than we need,
249+
# but we really don't want to find ourselves handling bot messages
250+
# unexpectedly.
251+
bot_message = True
252+
253+
if (bot_message and 'bot_id' in message):
243254
# Need to look up the bot and add the user id to the message.
244255
userid = get_slack_bot_userid(message['bot_id'])
245256
message['user'] = userid
@@ -254,6 +265,9 @@ def zdaemon_slack_router(triggers, ack, say, message):
254265
# so only do true messages (no subtype) and replies for now. (note: slack doesn't actually appear
255266
# to send the message_replied subtype as of Sep 2024)
256267
#
268+
# Note: You really want to do this first before any other check, since we might get a bot
269+
# loop via the other sanity checks below.
270+
#
257271
# This gets rid of bot_message explicity (so we avoid loops with other bots), which is nice,
258272
# but also removes troublesome things like message_changed and message_deleted which would
259273
# need their own very specific handling.
@@ -270,14 +284,14 @@ def zdaemon_slack_router(triggers, ack, say, message):
270284
# channel. These seem to be paried with a message_changed, which thankfully don't
271285
# seem to be relevant for our purposes.
272286
#
273-
# Note: You really want to do this first before any other check, since we might get a bot
274-
# loop via the other sanity checks below.
287+
# Apologies for the complexity of this check. It could almost certainly be better.
275288
if ('subtype' in message and
276-
(message['subtype'] not in ['thread_broadcast',
277-
'message_replied',
278-
'file_share',
279-
'bot_message'] or
280-
(message['subtype'] == 'bot_message' and not bridge_bot_message))):
289+
(message['subtype'] not in ['thread_broadcast',
290+
'message_replied',
291+
'file_share',
292+
'bot_message'] or
293+
(message['subtype'] == 'bot_message' and not bridge_bot_message)) or
294+
bot_message and not bridge_bot_message):
281295
# DEBUG
282296
# print("zdaemon_slack_router ignoring message: %s" % message)
283297
return

0 commit comments

Comments
 (0)