I seem to have a problem when responding to incoming messages via the Slack Events API (im.message event).
When a user (in this case UQ364CBPF
) sends a message to my App Home, the events API correctly posts an event to my backend (=first line in the logs below).
I respond to the event with an HTTP 200 OK, and in my code (see below), I trigger a response from my Bot User.
This response is sent correctly in Slack.
But: after that, the events API keeps posting events that my own bot user has posted a message in this channel...
Also, where the events are usually posted only 3 times, these events just keep being posted non-stop. Even though the bot user only sent one response message in Slack.
User UQ364CBPF has posted message: I'm typing a message to my Slack bot. in DQ5FF35N2 of channel type: im
[26/Dec/2019 15:16:30] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ5FF35N2 of channel type: im
[26/Dec/2019 15:16:32] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:33] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:35] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:37] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:39] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:40] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:42] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
[26/Dec/2019 15:16:43] "POST /slack/events/ HTTP/1.1" 200 0
User UQ5529KTR has posted message: Thank you for your message! We will get back to you soon! in DQ3KX0NPJ of channel type: im
In my code, this is part of a bigger function that processes Slack events, the relevant part:
@csrf_exempt
def slack_events(request):
if request.method == 'POST':
json_data = json.loads(request.body)
event_callback_type = json_data['event']['type']
if event_callback_type == 'message':
user_id = json_data['event']['user']
text = json_data['event']['text']
channel_id = json_data['event']['channel']
channel_type = json_data['event']['channel_type']
timestamp = json_data['event']['ts']
print ('User ' + user_id + ' has posted message: ' + text + ' in ' + channel_id + ' of channel type: ' + channel_type)
slack_message_received(user_id, channel_id, channel_type, team_id, timestamp, text)
return HttpResponse(status=200)
There is nothing in here (or in the function slack_message_received
) could trigger this recurring flow.
So
1. Is it strange behavior that Slack is bombarding me with all these POST requests from my own bot user?
2. Can I avoid this behavior, other than filtering out my own Bot User ID and ignoring these requests on backend side?
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.