Javascript Facebook Messenger API:设置 webhook 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36595183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:15:46  来源:igfitidea点击:

Facebook Messenger API: Trouble setting up a webhook

javascriptphpfacebookwebhooksfacebook-messenger

提问by Shane

I am trying to set up a web hook for the new Facebook Messenger bot platform on my PHP webserver and am receiving this error:

我正在尝试在我的 PHP 网络服务器上为新的 Facebook Messenger bot 平台设置一个网络挂钩,并收到此错误:

The URL couldn't be validated. Response does not match challenge, expected value = '364011207', received='

无法验证 URL。响应与挑战不匹配,预期值 = '364011207',收到 =

Resources

资源

https://developers.facebook.com/docs/messenger-platform/quickstart

https://developers.facebook.com/docs/messenger-platform/quickstart

https://developers.facebook.com/docs/messenger-platform/webhook-reference#common_format

https://developers.facebook.com/docs/messenger-platform/webhook-reference#common_format

Any help is greatly appreciated.

任何帮助是极大的赞赏。

回答by Shane

I came across a fix. I scrapped my js attempt and created a new php file with the following code:

我遇到了一个修复。我放弃了我的 js 尝试并使用以下代码创建了一个新的 php 文件:

<?php

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'my_token_code') {
echo $challenge;
}

I got this code from the first 10 minutes of this video: https://www.facebook.com/marketingdevelopers/videos/883648801749520/

我从这个视频的前 10 分钟得到了这个代码:https: //www.facebook.com/marketingdevelopers/videos/883648801749520/

回答by Jamund Ferguson

That code is node.jscode and should be run on the server not in a <script>tag in your HTML.

该代码是node.js代码,应该在服务器上运行,而不是在<script>HTML中的标记中。

Here's a simple walk through of setting up the messenger bot with node.js: https://github.com/voronianski/simon-le-bottle

这是使用 node.js 设置信使机器人的简单步骤:https: //github.com/voronianski/simon-le-bottle

Essentially you need to make sure you have a host that supports node.js applications and run it as such. It will not work inside of HTML.

本质上,你需要确保你有一个支持 node.js 应用程序的主机并运行它。它不会在 HTML 内部工作。

回答by Zernel

I just resolved this problem by adding '/webhook' in the callback URL...

我刚刚通过在回调 URL 中添加 '/webhook' 解决了这个问题......

回答by che-azeh

If you're running this as a Node.jsapplication, and you're coming from the The Facebook Quickstart Guidementioned in the question, you'll have to point the webhook URL to [your-server-root]/webhook. Notice this part of the tutorial:

如果您将此作为Node.js应用程序运行,并且您来自问题中提到的 Facebook 快速入门指南,则必须将 webhook URL 指向[your-server-root]/webhook. 注意教程的这一部分:

// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
    ...
    // Responds with the challenge token from the request
    res.status(200).send(challenge);
});

回答by Natraj

@shane

@shane

webhook: function(req, res) {

   if (req.query['hub.verify_token'] === 'tokentoken') {
      res.send(req.query['hub.challenge']);
   } else {
      res.send('Error, wrong validation token');    
   }
}

Note sure what exactly you're doing. but this is what I did and it's working.

注意确定你在做什么。但这就是我所做的并且正在起作用。

I've tested using ngrok server as I don't have a domain name and the callback URL is https://werwrwetwtw.ngrok.io/webhook

我已经使用 ngrok 服务器进行了测试,因为我没有域名并且回调 URL 是https://werwrwetwtw.ngrok.io/webhook

Hope this helps!

希望这可以帮助!

回答by Stan Kolev

Not sure if this help, but the query parameters that FB send are with underscore not dot e.g. :

不确定这是否有帮助,但是 FB 发送的查询参数是下划线而不是点,例如:

  • hub_verify_token
  • hub_mode
  • hub_challenge
  • hub_verify_token
  • 集线器模式
  • hub_challenge

P.S.

聚苯乙烯

Sorry, this is valid for PHP

抱歉,这对 PHP 有效