简体   繁体   中英

Getting repeated calls on facebook-messenger webhook

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.

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.

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.

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.

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM