Getting repeated calls on facebook-messenger webhook
Asked Answered
C

4

15

I have successfully setup a facebook-messenger webhook. Until yesterday I was able to send and receive messages as well. But today, when I am sending one message from user, I am getting multiple calls at server webhook POST API. They never seem to stop.

Cotenant answered 26/4, 2016 at 7:51 Comment(0)
C
7

Figured it out. I was sending response to every communication that came from facebook. So I ended up responding to ACK messages as well. In turn one more ACK came. Thats why it led to infinite loop.

In this page we can find different object structures for messages recieved:

text

{
"object":"page",
"entry":[
{
  "id":PAGE_ID,
  "time":1457764198246,
  "messaging":[
    {
      "sender":{
        "id":USER_ID
      },
      "recipient":{
        "id":PAGE_ID
      },
      "timestamp":1457764197627,
      "message":{
        "mid":"mid.1457764197618:41d102a3e1ae206a38",
        "seq":73,
        "text":"hello, world!"
      }
    }
  ]
}
]
}

Message-Delivered callback

{
 "object":"page",
 "entry":[
  {
     "id":PAGE_ID,
     "time":1458668856451,
     "messaging":[
        {
           "sender":{
              "id":USER_ID
           },
           "recipient":{
              "id":PAGE_ID
           },
           "delivery":{
              "mids":[
                 "mid.1458668856218:ed81099e15d3f4f233"
              ],
              "watermark":1458668856253,
              "seq":37
           }
        }
     ]
  }
 ]
}

So, for differentiating we can refer to entry[0].messaging[0].message this exist only in user sent message. Callback or postbacks do not contain this part. Check for this, before responding. If it exists, respond, otherwise dont.

Cotenant answered 28/4, 2016 at 8:37 Comment(1)
so how did you avoided that,... i am how can you differentiate between the two....can you share a sample code of your request. thanksNephrosis
B
15

Do all of those calls have the same content or are they different? You could log the exact message string that facebook sends to you and see what they include.

For example there's a message delivery callback that informs you that the user received the message. The JSON looks like this:

{'delivery': {'mids': ['mid.146174459xxx:30a42600a95exxxxx'], 'seq': 429, 'watermark': 146174459xxx}, 'recipient': {'id': xxxxxxxx}, 'sender': {'id': xxxxxx}}

Edit: It could also be the case that your are not confirming incoming calls with a http status 200. If facebook receives an error from your webhook the message will be sent multiple times.

Brahmin answered 27/4, 2016 at 8:24 Comment(3)
I've tried acknowledging the looping message with res.status(200) but it keeps coming in. Is there a way to test whether the message is being acknowledged as received?Derive
Using sails js, I had to send res.ok() [which is equal to res.status(200)] and facebook stopped sending me multiple payloadsJinja
+1 b/c I was missing the 200 response and you reminded me of that. I added res.status(200).send('EVENT_RECEIVED') before my function fires and this kills my infinite loopUnderpart
C
7

Figured it out. I was sending response to every communication that came from facebook. So I ended up responding to ACK messages as well. In turn one more ACK came. Thats why it led to infinite loop.

In this page we can find different object structures for messages recieved:

text

{
"object":"page",
"entry":[
{
  "id":PAGE_ID,
  "time":1457764198246,
  "messaging":[
    {
      "sender":{
        "id":USER_ID
      },
      "recipient":{
        "id":PAGE_ID
      },
      "timestamp":1457764197627,
      "message":{
        "mid":"mid.1457764197618:41d102a3e1ae206a38",
        "seq":73,
        "text":"hello, world!"
      }
    }
  ]
}
]
}

Message-Delivered callback

{
 "object":"page",
 "entry":[
  {
     "id":PAGE_ID,
     "time":1458668856451,
     "messaging":[
        {
           "sender":{
              "id":USER_ID
           },
           "recipient":{
              "id":PAGE_ID
           },
           "delivery":{
              "mids":[
                 "mid.1458668856218:ed81099e15d3f4f233"
              ],
              "watermark":1458668856253,
              "seq":37
           }
        }
     ]
  }
 ]
}

So, for differentiating we can refer to entry[0].messaging[0].message this exist only in user sent message. Callback or postbacks do not contain this part. Check for this, before responding. If it exists, respond, otherwise dont.

Cotenant answered 28/4, 2016 at 8:37 Comment(1)
so how did you avoided that,... i am how can you differentiate between the two....can you share a sample code of your request. thanksNephrosis
E
7

My problem was similar but I was getting Multiple Message delivery posts. After a few hours of frustration, I realized that Message Delivered callback is called every time the message is delivered to EVERY DEVICE. So, if you are logged into both web and mobile app, the callback would be called twice.

Encaustic answered 30/5, 2017 at 15:11 Comment(2)
This is why I get 3 identical delivery confirmation lol. Thanks so much !Sickener
Dear lord this was driving me mad! Didn't realise I had 6 TABS OPEN ALL RECEIVING RECEIPTS!Went
D
1

When working with messenger of facebook you need to take in account two things after you send the message :

A) Message Delivery

B) Message Read

Since you are working with webhooks this will be trigger everytime one of the events happen (receive a message , deliver the message you sent , the user read the message). So if you activate for example message_deliveries in your webhook and you send a message as action , you will end up in a loop.

The proper way to handle this is in the base code. PHP example :

        // Skipping delivery messages
        if (!empty($message['delivery'])) {
            #Do something here if you want
            continue;
        }

        // Skipping read messages
        if (!empty($message['read'])) {
            #Do something here if you want
            continue;
        }

Hope it helps !

Dewey answered 1/10, 2016 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.