How I Built a Facebook Messenger bot in 30 minutes, Part 4

July 25, 2017

October 11, 2016

This is a continuing series that Jeffrey is writing on how to build Facebook Messenger bots. For part 1, go here, and for part 2 go here. For part 3 go here.

  1. Add config variables to Heroku

Go back to the Heroku Dashboard, click on Settings for the app, scroll down and click “Reveal Config Vars.” You’ll want to type in “PAGE_ACCESS_TOKEN” for the key (no quotes) and your Token generated way back in step 2 on developers.facebook.com. I’ll show another screen shot here just in case you forgot.

  1. Setup web hooks.

We’ll need to head back to developers.facebook.com/apps and click on the Product->Messenger->Settings to add some web hooks to get Heroku and Facebook to talk to each other. Click on the “Setup Webhooks” button.

Your callback URL is something we’ll have to create on the server, but we now know the name of the web app that we just created on Heroku. I called mine “testbots-jeffrey” remember? So the format of the callback will be [https://testbots-jeffrey.heroku.com/webhook]. Choose a token name (no spaces allowed). This doesn’t matter, but you’ll have to remember it and place it in code later. I used “pick_a_good_token_name” (Okay, I changed it after I took a screenshot. No I didn’t. Maybe I did, maybe I didn’t.). When we try to click “Verify and save” it doesn’t work. Why? Because we don’t have a page at /webhook. Let’s add that now.

Subscribe the custom web hook to the Page

After adding the key and value pair to Heroku, you’ll need to subscribe your custom web hook to the Page events by issuing a post request. The simplest way to do that is to open up your terminal and type the following:

curl -X POST “https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=PAGE_ACCESS_TOKEN”

(be sure to update your PAGE_ACCESS_TOKEN to your unique identifier. You should get a {“success”: true} response)

  1. Last step!

Subscribe the app to the correct page.

  1. Now to test!

Go back to your Facebook Page and click on “Message” or the shorthand is to go to the URL messenger.com/t/facebookappname. Type anything and it will reply!

Right now the functionality of the app is extremely limited. Tune in next time and we’ll add some functionality!

Published on July 25th, 2017

Last updated on August 10th, 2017


Share: