Search code examples
webhooksslackslack-api

Webhook incoming with event subscription create loop


I am using a web service api to receive message from a slack channel (event subscription) and the same web service api to send message (Incoming webhook) to the user on the same slack channel.

But when I send a message, this same message is send back to my api by the slack event subscription. That create an infinite loop.

How can I declare my message that I am sending to the channel which I can check when I receive back by the event subscription ?


Solution

  • This is normal behavior of Slack and can not be changed by any configuration. When you subscribe to message events you will receive events for all messages, including the ones from your own app.

    To avoid an infinite loop you therefore need to detect and filter out the messages from your own bot before continue processing. This can be done easily, since all messages contain the name of the the sender. e.g. you can filter out all messages that are have the bot_id of your Slack app or all messages from bots (that have subtype = bot_message).

    Here are some examples how messages look like and how messages from real users are different then message from Slack apps:

    Example message from a real user:

     {
          "type": "message",
          "user": "U12345467",
          "text": "Good read for some of guys",
          "ts": "1531745292.000021"
     }
    

    Example message from a Slack app or bot

    {
          "text": "Hey guys",
          "bot_id": "B12345678",          
          "type": "message",
          "subtype": "bot_message",
          "ts": "1531700187.000049"
     }
    

    See also this answer.