Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 294
Jonny Vu Avatar asked Mar 30 '19 07:03

Jonny Vu


1 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

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"
like image 177
Jonny Vu Avatar answered Oct 07 '22 17:10

Jonny Vu