In this blog, we will 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 the Watson Developer Cloud team as a plugin to Botkit. It allows developers to easily integrate a Watson Assistant workspace with the messaging platforms supported by Botkit.
Configure Watson Assistant service and create a conversation
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
WORKSPACE ID in later steps.
Create a Facebook page and configure Facebook Messenger
- 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.
Configure Botkit middleware
- Fork and clone the Botkit middleware repository.
git clone https://github.com/watson-developer-cloud/botkit-middleware.git cd botkit-middleware
- Navigate to the folder
examples/multi-botwithin 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
.envfile by updating the Watson Assistant
- Still in the
.envfile, specify the Facebook related variables
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> USE_FACEBOOK=true
- Upload your application to IBM Cloud from your working directory (examples/multi-bot). Follow the steps for Uploading your application to push your application to IBM Cloud through the CLI.
- 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
messaging_postbacksoptions 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!