Itsy Bitsy Bytes


Building a Facebook Messenger bot - how to start

Of all the new web technologies announced in the past years, nothing seemed more interesting to me than the new chat bot movement, particularly the one presented by Facebook.

As someone who uses Facebook to keep in touch with friends, I've found the Messenger app useful simply because it allowed me to talk with people. Enhancing it with bots would have been the icing on the cake! And as a curious web developer, I've been meaning to play with it for some time.

This post wont't cover the technical aspects of actually building a chat bot (I'll start digging into that right after I finish writing!) but some of the gotchas that I ran into while trying to make the sample app work. Because while Facebook does have a tutorial on their website explaining how to start, it doesn't also hold your hands during the process. There are some aspects that are neither beginner-friendly, nor well-documented. That being said... let's begin!

First and first, you need a server capable of running a Node app and a secure connection.

Personally, I have a fun pet project, Când e liber? (translated somewhat as "When is the next holiday") and is a site that automatically calculates the time remaining until the next holiday. Since I intend to make it as "advanced" as possible (it has offline support with web workers, add to home screen capabilities, performance best practices, etc.) adding a Messenger bot for it's Facebook page was a nice and logical addition. Why go to the site when you could simply ask it in a chat window?

After a bit of fiddling bot.candeliber.com was all set up and had https support. Uploading and starting the sample app was all that was left.

Before running node app, you need to edit the config file, node/config/default.json.

{
    "appSecret": "itsasecret",
    "pageAccessToken": "itsasecretaswell",
    "validationToken": "not_gonna_tell_you",
    "serverURL": "https://bot.candeliber.com"
}

Here's where you need to pay attention, since not everything is straightforward.

  • the appSecret can be found inside the Facebook Apps Dashboard
  • the pageAccessToken is also inside the Dashboard, in the Products > Messenger > Settings section, under Token Generation
  • the validationToken is something that you randomly create, not generated by Facebook. Just write a passwordish token and you're good to go
  • ok, the serverURL part actually is straightforward...

The next and final step is the webhook setup.

Facebook page messenger webhook setup

The callback url has to include the /webhook path (so in my case it was https://bot.candeliber.com/webook and not simply https://bot.candeliber.com.
Inside the Verify token input field you need to enter the validationToken that you made up and saved inside the default.json configuration file.

And that's it!

messenger bot chat window printscreen

After a simple node app, my sample bot was app and running and parroting my messages. Not very impressive, I know, but that's just for now.


comments powered by Disqus