Facebook 信使平台。使用 SSL 设置 Webhook

Facebook Messenger Platform. Setting up webhook with SSL

Facebook 终于为他们的 Messenger API for their messenger 推出了 API。这将使我们能够创建聊天机器人。

In getting started guide,我需要设置 webhook。这需要驻留在特定域中的网络服务器并且必须使用 SSL 连接。

我有 VPS,它有静态 IP。我制作了自签名证书并创建了使用该证书的简单 Node JS Web 服务器。首先我需要验证 webhook 的令牌:

app.get('/webhook/', function (req, res) {
  if (req.query['hub.verify_token'] === '<validation_token>') {
      res.send(req.query['hub.challenge']);
  }
  res.send('Error, wrong validation token');
})

然后我启动了这个服务器应用程序,并在我的 facebook 应用程序仪表板中单击 验证并保存 按钮。

它向我抛出此错误消息:

这意味着 Facebook 不想接受我的自签名证书

这带来了几个问题:

我是否需要使用仅由证书颁发机构提供的 SSL 证书才能使用 Facebook Messenger?

使用 Facebook Messenger 比使用 Telegram 困难得多。

您可以使用cloudflare to get https instead of self-sign. Or you can use https://letsencrypt.org

如果只是测试,您可以使用 localtunnel。 https://localtunnel.me/

摘自here

New webhook subscriptions must use a secure HTTPS callback URL as of v2.5. With the next version of the Graph API we will stop sending updates to non-HTTPS callback URLs.
If you need more information about setting up HTTPS for your callback URL, check out the Getting Started guide from Let's Encrypt and the SSL Certificate Installation instructions from Digicert.

来自 here,就像@saturngod 所说:

Self signed certificates won't be accepted by facebook. Letsencrypt certificates works perfectly.

验证网络钩子使用:-

$_REQUEST["hub_token"] and $_REQUEST["hub_challenge"];

首先将令牌验证为您的页面令牌,然后打印:-

echo $_REQUEST["hub_challenge"];
exit;

我将 my bot 部署到 Heroku,他们很容易提供 HTTPS 连接。本地开发,我用的是ngrok,同样支持HTTPS转发

对于新手,我写了 this small tutorial 给那些想开始使用 Facebook Messenger (Bot) API 的人,从第一行代码到 Heroku 部署。我发现 Heroku 非常适合这样的项目,因为我使用了 Python flask + gunicorn,我部署所需的一切真的花了 5 分钟来设置,而 Heroku 花了照顾其余的。

我在 Apache 上使用 Comodo SSL 时遇到了同样的错误。在我的例子中,我丢失了 SSLCertificateChainFile,只需要上传那个包文件和我的虚拟主机配置中的文件路径,然后重新加载 apache。这是 link 用于安装从 ssls.com https://helpdesk.ssls.com/hc/en-us/articles/203482651-How-to-install-a-SSL-certificate-on-Apache.

购买的 comodo SSL