[Django]-Slack Events API: Events are posted (multiple times) for responses of own bot user

4👍

It’s standard behavior of the Events API that your bot will receive all message events, including from it’s own posts. And by reacting to them you have created and endless loop.

To address this issue you need to stop reacting to the bot’s own messages by filtering them out as you suggested in #2. That is the standard approach.

The easiest is to just filter out all bot messages, if you don’t need to listen to other bots. Then you can just ignore all messages that have the subtype property. Alternatively you can of course filter our messages from your own bot.

0👍

I had the same issue. But when I printed the request, I couldn’t find subtype so the solution above didn’t work for me.
log detail showing what is in the request I receive. There was no subtype

Instead, I noticed that messages from bot had bot_id and messages from users didn’t.
Therefore, my solution was like this:

    @slack_events_adapter.on("message")
    def say_hello(event_data):
       print('EVENT_DATA')
       print(event_data)
       message = event_data['event']

       # if the incoming message contains "hello" NOT CASE SENSITIVE, then respond with a designed message
       # "subtype" doesnt help to filter bot's message event and user's message event
       if message.get('bot_id') is None and 'hello' in ((message.get('text')).lower()):
           channel_id = message['channel']
           user = message['user']
           message = "Hello <@%s>! :tada:" % user
           slack_web_client.chat_postMessage(channel=channel_id, text=message)
       else:
           return
👤Phoebe

0👍

As @Phoebe said – while retrieving the events using

slack_message.get('event') 

I didn’t find a key as bot_id. I think this is an issue from Slack Events api.

👤Abin M

Leave a comment