简体   繁体   English

在facebook-messenger webhook上重复调用

[英]Getting repeated calls on facebook-messenger webhook

I have successfully setup a facebook-messenger webhook. 我已成功设置了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. 但今天,当我从用户发送一条消息时,我在服务器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. 您可以记录facebook发送给您的确切消息字符串,并查看它们包含的内容。

For example there's a message delivery callback that informs you that the user received the message. 例如,有一个消息传递回调,通知您用户收到了消息。 The JSON looks like this: JSON看起来像这样:

{'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. 编辑:也可能是您没有确认http状态为200的来电。如果Facebook收到来自您的webhook的错误,该消息将被多次发送。

Figured it out. 弄清楚了。 I was sending response to every communication that came from facebook. 我正在回复来自facebook的每一个消息。 So I ended up responding to ACK messages as well. 所以我最终也回应了ACK消息。 In turn one more ACK came. 反过来再来一个ACK了。 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. 因此,为区分我们可以参考entry[0].messaging[0].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. 经过几个小时的挫折之后,我意识到每次将消息传递到每个设备时都会调用Message Delivered回调 So, if you are logged into both web and mobile app, the callback would be called twice. 因此,如果您同时登录Web和移动应用程序,则会调用两次回调。

When working with messenger of facebook you need to take in account two things after you send the message : 使用facebook的Messenger时,您需要在发送消息后考虑两件事:

A) Message Delivery A) 消息传递

B) Message Read B) 消息读取

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). 由于您正在使用webhooks,因此每次发生一个事件时都会触发(接收消息,传递您发送的消息,用户阅读消息)。 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. 因此,如果您在webhook中激活了例如message_deliveries并且您将消息作为操作发送,那么您将最终处于循环中。

The proper way to handle this is in the base code. 处理此问题的正确方法是在基本代码中。 PHP example : PHP示例:

        // 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 ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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