简体   繁体   English

如何以编程方式订阅 messenger webhook

[英]How to programmatically subscribe messenger webhook

I was trying to implement Facebook Checkbox for my customers but I realized that it's required to subscribe to the messenging_optins event to render Facebook checkbox.我试图为我的客户实现 Facebook Checkbox,但我意识到需要订阅messenging_optins事件才能呈现 Facebook 复选框。 So I tried to subscribe to the messaging_optins event programmatically.所以我尝试以编程方式订阅messaging_optins事件。

Note that my facebook app is in development app.请注意,我的 facebook 应用程序正在开发应用程序中。 So in fact, I still test on my Facebook account.所以实际上,我仍然在我的 Facebook 帐户上进行测试。

curl -i -X POST "https://graph.facebook.com/v3.2/{my-customer-page-id}/subscriptions?access_token={my_app_access_token}&callback_url=https%3A%2F%2Fmy-server.ngrok.io%2Fwebhook%2Ffacebook&fields=messaging_optins&object=page&verify_token=abc123abc123456"

The response is {"success":true}响应是{"success":true}

But my Facebook Checkbox still does not work until manually go to Facebook Developer setting and subscribe to the Page as the below screenshot.但我的 Facebook 复选框仍然无法工作,直到手动转到 Facebook 开发人员设置并订阅页面,如下面的屏幕截图。

在此处输入图片说明

I realize that the above curl command just help me subscribe to the Page Webhook (not Messenger webhook) event as the below screenshot.我意识到上面的curl命令只是帮助我订阅 Page Webhook(而不是 Messenger webhook)事件,如下面的屏幕截图所示。

在此处输入图片说明

Can anyone point me out why I can't subscribe to "Messenger event" but Facebook graph API return that it successfully subscribed?谁能指出我为什么不能订阅“Messenger 事件”但 Facebook 图形 API 返回它已成功订阅?

Thanks.谢谢。

After 1 day digging Facebook Docs, try and test many cases with Facebook Graph Explorer, I finally found the way to programmatically subscribe to the Page's Webhook.经过 1 天的 Facebook Docs 挖掘,尝试使用 Facebook Graph Explorer 测试许多案例,我终于找到了以编程方式订阅页面 Webhook 的方法。

In Facebook Docs, it had writes 3 lines like this在 Facebook Docs 中,它写了 3 行这样

Webhooks for Messenger allows you to receive real-time notifications when a variety of interactions or events happen, including when a person sends a message. Messenger 的 Webhook 允许您在发生各种交互或事件时接收实时通知,包括当某人发送消息时。 Webhooks for Messenger works a little differently than other Webhooks so please use theWebhooks for Messenger docs when setting up this type of Webhook. Webhooks for Messenger 的工作方式与其他 Webhooks 略有不同,因此在设置此类 Webhook 时请使用Webhooks for Messenger 文档

Here is the way I subscribe messages , messaging_postbacks , messaging_optins , message_reads events.这里是我订阅的方式messagesmessaging_postbacksmessaging_optinsmessage_reads事件。 Refer to: https://developers.facebook.com/docs/graph-api/webhooks/getting-started/webhooks-for-pages参考: https : //developers.facebook.com/docs/graph-api/webhooks/getting-started/webhooks-for-pages

curl -i -X POST "https://graph.facebook.com/v3.2/{page-id}/subscribed_apps?access_token={page-access-token}&subscribed_fields=messages,messaging_postbacks,messaging_optins,message_reads"

Facebook Graph API has a Subscriptions endpoint which can be used to set up webhooks for graph network's objects(eg page , user etc). Facebook Graph API 有一个Subscriptions端点,可用于为图网络的对象(例如pageuser等)设置 webhook。
You need to make a POST request to the subscriptions endpoint and provide the required parameters, which are:您需要向订阅端点发出POST请求并提供所需的参数,它们是:

  1. object the object whose webhook you're going to subscribe to object您要订阅其 webhook 的对象
  2. callback the publicly accessible URL where Facebook will be sending the webhook event data when the subscribed event gets triggered(eg your page receives a message) callback可公开访问的 URL,当订阅的事件被触发时,Facebook 将在其中发送 webhook 事件数据(例如,您的page收到一条消息)
  3. fields the list of fields you want to subscribe to(eg messages ) fields您要订阅的字段列表(例如messages
  4. verify_token Facebook will send this token to your callback URL so that you can verify that it's coming from Facebook and not some malicious source see this guide for handling webhook callbacks verify_token Facebook 会将此令牌发送到您的回调 URL,以便您可以验证它来自 Facebook 而不是某些恶意来源请参阅此指南以处理 webhook 回调
  5. access_token your app access token Here is a snippet from official docs access_token您的应用访问令牌 这是官方文档中的一个片段
curl -F "object=user" \
     -F "callback_url=https://your-clever-domain-name.com/webhooks" \
     -F "fields=photos" \
     -F "verify_token=your-verify-token" \
     -F "access_token=your-app-access-token" \
     "https://graph.facebook.com/your-app-id/subscriptions"

If succeeded, response will be如果成功,响应将是

{
  "success": "true"
}

To see the subscriptions you've, send a GET request to this HTTP endpoint要查看您的订阅,请向此 HTTP 端点发送GET请求

https://graph.facebook.com/your-app-id/subscriptions?access_token=your-app-access-token

which returns a response like this它返回这样的响应

{
  "data": [
    {
      "object": "user",
      "callback_url": "https://your-clever-domain-name.com/webhooks",
      "active": true,
      "fields": [
        {
          "name": "photos",
          "version": "v2.10"
        },
        {
          "name": "feed",
          "version": "v2.10"
        }
      ]
    }
  ]
}

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

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