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.
I realize that the above curl
command just help me subscribe to the Page Webhook (not Messenger webhook) event as the below screenshot.
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
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
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-pagesFacebook Graph API has a
Subscriptions
endpoint which can be used to set up webhooks for graph network’s objects(e.gpage
,user
etc).You need to make a
POST
request to the subscriptions endpoint and provide the required parameters, which are:object
the object whose webhook you’re going to subscribe tocallback
the publicly accessible URL where Facebook will be sending the webhook event data when the subscribed event gets triggered(e.g yourpage
receives a message)fields
the list of fields you want to subscribe to(e.gmessages
)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 callbacksaccess_token
your app access tokenHere is a snippet from official docs
If succeeded, response will be
To see the subscriptions you’ve, send a
GET
request to this HTTP endpointwhich returns a response like this