skip to Main Content

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. So I tried to subscribe to the messaging_optins event programmatically.

Note that my facebook app is in development app. So in fact, I still test on my Facebook account.

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}

But my Facebook Checkbox still does not work until manually go to Facebook Developer setting and subscribe to the Page as the below screenshot.

enter image description here

I realize that the above curl command just help me subscribe to the Page Webhook (not Messenger webhook) event as the below screenshot.

enter image description here

Can anyone point me out why I can’t subscribe to “Messenger event” but Facebook graph API return that it successfully subscribed?

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    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.

    In Facebook Docs, it had writes 3 lines like this

    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. Webhooks for Messenger works a little differently than other Webhooks so please use the Webhooks for Messenger docs when setting up this type of Webhook.

    Here is the way I subscribe messages, messaging_postbacks, messaging_optins, message_reads events. Refer to: 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"
    

  2. Facebook Graph API has a Subscriptions endpoint which can be used to set up webhooks for graph network’s objects(e.g page, user etc).
    You need to make a POST request to the subscriptions endpoint and provide the required parameters, which are:

    1. object the object whose webhook you’re going to subscribe to
    2. callback the publicly accessible URL where Facebook will be sending the webhook event data when the subscribed event gets triggered(e.g your page receives a message)
    3. fields the list of fields you want to subscribe to(e.g 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
    5. access_token your app access token
      Here is a snippet from official docs
    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

    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"
            }
          ]
        }
      ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search