Learn more >
Riya Mary Roy | Published November 8, 2017
In this tutorial, we demonstrate how to build a Facebook messenger chatbot using the Watson Assistant service and Botkit middleware. The Watson Assistant service allows you to quickly build conversations for chatbots and virtual agents. Botkit is designed to ease the process of designing and running useful, creative bots that live inside messaging platforms, such as Slack, Facebook, and Twilio. The Botkit middleware we use is created by Watson APIs team as a plugin to Botkit. It allows developers to easily integrate a Watson Assistant workspace with the messaging platforms supported by Botkit.
This guide will take approximately one hour to complete.
The first step, is to create a Watson Assistant service in IBM Cloud. Then, create a new Watson Assistant workspace, and begin to define Intents, Entities and the Dialog. Finally, let’s build a simple chabot by following this getting started guide. This conversation forms the basis for our integration with Facebook messenger.
Take note of the Watson Assistant credentials when creating the service. We will need the USERNAME, PASSWORD and WORKSPACE ID in later steps.
Create a Facebook page to deploy the Facebook messenger bot. If additional help is needed, refer to Facebook’s How do I create a Page? documentation.
Go to Facebook for Developers, click on My Apps, and select the Add a new app option.
Create a new app ID by entering a Display Name and Email.
Once the app is created, go to your app’s Settings, and under PRODUCTS, click Add Product. Select Messenger, and choose to Set up the messenger product.
Create a page access token to start using Facebook APIs. This page token has all messenger permissions, regardless of whether your app is approved to use them or not. Select your page and choose to generate a token. Make note of the token, as it is required in a few steps. Note that these tokens do not expire.
Fork and clone the Botkit middleware repository.
git clone https://github.com/watson-developer-cloud/botkit-middleware
Navigate to the folder examples/multi-bot within the repository. You can choose to either deploy your bot to a single platform (Slack, Facebook, or Twilio), or choose multiple platforms. For this exercise, choose to deploy to Facebook Messenger only.
Edit the .env file by updating the Watson Assistant USERNAME, PASSWORD and WORKSPACE ID.
Still in the .env file, specify the Facebook related variables FB_ACCESS_TOKEN, FB_VERIFY_TOKEN, FB_APP_SECRET and USE_FACEBOOK.
FB_ACCESS_TOKEN=<token you had generated in the token generation step>
FB_VERIFY_TOKEN=<random token that you can generate>
FB_APP_SECRET=<secret key which Facebook provides available in the facebook developer dashboard>
Upload your application to IBM Cloud from your working directory (examples/multi-bot).
You can check your IBM Cloud dashboard to view the application after it starts running successfully. Take note of the application URL as that is required in a few steps.
Go to the Facebook apps page, configure the Webhook with your IBM Cloud application URL, and verify the token.
Callback URL: https://<your_unique_url>.mybluemix.net/facebook/receive
Verify Token: random token that you specified in step 4
Ensure the messages and messaging_postbacks options are selected. Click Verify and Save.
In order for your webhook to receive events for a specific page, you must subscribe your app to your page. You can do this in the Webhooks section under the Messenger tab.
Your Facebook messenger chatbot is now ready! You can test it by using Facebook messenger.
September 27, 2019
September 6, 2019
What's on the outlook for chatbots? And what challenges do you face as developer when building one? We discuss this…
Back to top