FB Messenger Bot:Webhook更新未发送 [英] FB Messenger Bot: Webhook updates not being delivered

查看:163
本文介绍了FB Messenger Bot:Webhook更新未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebook复选框插件一切正常工作,除非Facebook在确认选择启用时不会向我的webhook url发送请求。 facebook docs 中的
被提及那么


选择加入事件后,如果勾选了复选框状态,我们会在您的服务器上发布一个webhook事件。此回调与选择加入回调具有相同的格式,而不是发件人字段,它具有一个具有user_ref字段的optin对象。


但是它没有发送任何数据。这是我的webhook代码

  if(!empty($ _ REQUEST ['hub_mode'])&& $ _REQUEST [ hub_mode'] =='subscribe'&& $ _REQUEST ['hub_verify_token'] =='verificationtoken'){
echo $ _REQUEST ['hub_challenge'];
}
$ data = file_get_contents(php:// input);
$ fp = file_put_contents(PROTECH_FBB_DIR。'/ data.log',$ data);

我也尝试手动点击我的webhook,看看它是否响应。它的工作正常,所以这意味着Facebook不会发布数据,也可能是我做错了?



任何帮助将非常感激。谢谢

解决方案

我不是php开发人员,而是在javascript,node.js中实现了相同的逻辑。我想分享详细的步骤,还有JavaScript代码,希望你能弄清楚你可以做些什么来改善你的生活:P
如你所说,你从api电话中收到user_ref 。没错再次阅读文档,他们提到当用户检查复选框插件时,user_ref将被接收。这个user_ref是由你设置的,每次加载该页面时,user_ref必须是唯一的,那么只有复选框插件才会呈现,如果插件不是唯一的,则不会渲染。这里是完整的逻辑。您生成user_ref,当用户选中复选框时,您将收到此unqiue user_ref,使用此user_ref向用户发送消息(您可以根据需要使用user_ref向用户发送消息,但我建议您使用senderId) 。当您使用user_ref将消息发送给用户时,webhook api将给您一个响应,其中包含用户的senderId,这是我们通常在我们的应用程序中使用的psid。这是您需要保存在数据库中。
现在我将把我的代码放在这里,我是如何做到的。
接收user_ref并发送消息给用户:
我的有效载荷:

  function sendTextMessageRef(user_ref,messageText ,md){
var messageData = {
收件人:{
user_ref:user_ref
},
message:{
text:messageText,
元数据:md
}
};

callSendAPI(messageData);
}
函数callSendAPI(messageData){
请求({
uri:'https://graph.facebook.com/v2.6/me/messages',
qs:{access_token:PAGE_ACCESS_TOKEN},
方法:'POST',
json:messageData

},function(error,response,body){
if(!error&& response.statusCode == 200){
var recipientId = body.recipient_id;
var messageId = body.message_id;

if(messageId) {
console.log(成功发送带有%s的邮件到收件人%s,
messageId,recipientId);
} else {
console.log(Successfully called发送API为收件人%s,
recipientId);
}
} else {
console.error(失败的调用发送API,response.statusCode,response.statusMessage, body.error);
}
});
}

现在,在发送消息后,我收到一个这种json格式的响应将包含用户的发件人ID:

  {sender:{id:xxxxxxx},收件人:{id:xxxxx你要查找的是这个*******},timestamp:1504698781373,message:{is_echo:true,app_id:xxxxxxxxxxxxxxx :INVITATION__REPLY__qwe__2017-09-05T02xo20__xxxxxxxx__063__yes,mid:mid。$ cAAGcxxxxxxxxVxuAtJ,seq:120162,text::)}} 
pre>

在上面收到的json数据中,recipient.id是您要查找的内容。
这里为了让你明白我在聊天机器人中做的是第一个用户选择复选框插件,我收到我的服务器上的呼叫,如果检查是否包含user_ref,如果是,那么我发送一个短信给用户使用user_ref的自定义元数据。当用户收到消息时,webhook会以上面给出的格式发送一个json数据。要确定哪个user_ref已收到此响应,我将自定义元数据设置为一些字符串+ user_ref的组合。使用这个我识别我以前使用user_ref发送消息的用户的sender.id。 sender.id是我的pageid和recipient.id您尝试获取并使用我们通常向用户发送消息的用户标识,也称为psid。



希望这有帮助,如果您仍然使用上述解决方案获得一些问题,请进行更新:)


i am working with Facebook Checkbox Plugin everything is working fine except facebook is not sending request to my webhook url when Confirming Opt-in. in facebook docs it is mentioned that

After the opt-in event, we will post a webhook event to your server if the checkbox state was checked. This callback has the same format as the opt-in callback, but instead of a sender field, it has an optin object with a user_ref field.

But it is not sending any data. here is my webhook code

if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == 'verificationtoken') {
    echo $_REQUEST['hub_challenge'];        
}
$data = file_get_contents("php://input");
$fp = file_put_contents( PROTECH_FBB_DIR.'/data.log', $data);

i have also tried hitting my webhook manually and see if it responds. and it does work perfectly normal, so it means facebook is not posting data or maybe i am doing something wrong?

Any help would be highly appreciated. thanks

解决方案

I am not php developer but have implemented the same logic in javascript, node.js. I would like to share the steps in detail and also the javascript code and hope you can figure out what you can do with it to make your life better :P As you said, you are receiving the user_ref from the api call. That's correct. Read the documentation once again they have mentioned the user_ref will be received when user check the checkbox plugin. This user_ref is set by you and every-time the page loads this user_ref must be unique then only the checkbox plugin will render, if it is not unique the plugin wont render. And here is the complete logic behind it. You generate the user_ref, when user check the checkbox, you receive this unqiue user_ref, using this user_ref you send message to the user(you can send message to user using user_ref as many time as you want but I will suggest you rather use senderId). When you send the message to user using user_ref, the webhook api will give you a response containing senderId of the user which is actually psid we normally use in our app. This is what you need to save in your db. Now I will put my code here how I did it. Receiving the user_ref and sending message to user: My payload:

    function sendTextMessageRef(user_ref, messageText,md) {
  var messageData = {
    recipient: {
      user_ref: user_ref
    },
    message: {
      text: messageText,
      metadata: md
    }
  };

  callSendAPI(messageData);
}
function callSendAPI(messageData) {
  request({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: PAGE_ACCESS_TOKEN },
    method: 'POST',
    json: messageData

  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      if (messageId) {
        console.log("Successfully sent message with id %s to recipient %s", 
          messageId, recipientId);
      } else {
      console.log("Successfully called Send API for recipient %s", 
        recipientId);
      }
    } else {
      console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
    }
  });  
}

Now, after sending the message, I receive a response in this json format which will include the sender id of the user:

{"sender":{"id":"xxxxxxx"},"recipient":{"id":"xxxxxWhat you are looking for is this*******"},"timestamp":1504698781373,"message":{"is_echo":true,"app_id":xxxxxxxxxxxxxxx,"metadata":"INVITATION__REPLY__qwe__2017-09-05T02xo20__xxxxxxxx__063__yes","mid":"mid.$cAAGcxxxxxxxxVxuAtJ","seq":120162,"text":":)"}}

In above received json data, the recipient.id is what you are looking for. Here To make you understand what I did in my chat bot is first user select the checkbox plugin, I receive the call on my server, if check if it contains user_ref, if yes then I send a text message to user with a custom metadata using user_ref. When user receives the message, the webhook send me a json data in the above given format. To identify for which user_ref I have received this response, I set custom metadata which is combination of some string+user_ref. Using this I identify the sender.id of the user for which I previously sent message using user_ref. The sender.id is my pageid and recipient.id the the user id which you are trying to get and using which we generally send message to the user and is also know as psid.

Hope this helps, if you still get some issue using the above mentioned solution, then do update about it :)

这篇关于FB Messenger Bot:Webhook更新未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆