Reading Time: 6 minutes

Everyone is very excited about chatbots as the future of messaging, customer service, information delivery, etc. Facebook touts Messenger bots as (e.g. Jarvis) and the press gets excited about how they will change the world. But really, bots are essentially an ESB that listens for keywords and routes responses back to the user. This project will show you how to build one using Anypoint Platform.

This example project provides a framework to build a Facebook Messenger Bot that can leverage APIs built on Anypoint Platform.

mulesoft facebook messenger bot

Set up Your Facebook App and Page

  1. To get started, set up a new App in Facebook under the developers site
  2. Be sure to select Apps for Messenger under the Category dropdown before clicking Create App ID.
  3. Once the app gets created, scroll down to the Token Generation section and click on Create a new page. You'll be re-directed to the Facebook site to create a new page.
  4. Select any category (I selected Brand or Product > Software) and give it a name (e.g. MuleSoft Demos).
  5. Skip through the setup steps and jump back to the Facebook site.
  6. Once you refresh the page, the new Facebook Page with show under the Token Generation section under the Page dropdown.
  7. Select your new page and it will create a Page Access Token. You'll need this for your Mule app to call the Send later.

mulesoft facebook messenger bot

Deploy App to

  1. Next, deploy the example app from Github and before you deploy it to CloudHub, open the the mule-app.properties file and copy and paste the Page Access Token to the access_token variable.
  2. The app is built to demonstrate a few of the the required event listeners for the Facebook Messenger platform. It handles the webhooks for messages and postbacks. Messages are parsed for keywords such as ‘template', ‘button', etc.. and all other messages are echoed back to the user using the Send API.
  3. When the app is deployed and running in CloudHub, test out the webhook challenge using the following URL below. The response in the browser should be ‘1234'
    1. https://<app name>.cloudhub.io/webhook?hub.challenge=1234

mulesoft facebook messenger bot

Setup Webhooks

  1. With the challenge returned successfully, we can setup the webhooks in Facebook now. Switch back to the Facebook Developers site and click on Setup Webhooks under the Webhooks section.
  2. Enter the app URL under Callback URL with the path webhook. Enter any text under the Verify Token field. Under the Subscription Fields section, check messages and messaging_postbacks and then click on Verify and Save.
  3. Once the webhook setup is complete, select the Facebook Page that was created previously and click on Subscribe in the Webhooks section.

Test Messages

  1. Everything should be setup and running now on both CloudHub and Facebook. Switch back to Facebook and open up the Page you created. You should see a button that says Send Message. Mouseover that button and then click on Test Button.
  2. Go ahead and test out various messages and see their responses. When you type in messages that contain keywords such as button or template, you'll see specific message formats returned.
  3. You can also review the live console in CloudHub for the app to see the logs for the request and response.

mulesoft facebook messenger bot

And that is how you setup and build a Facebook Messenger bot using Anypoint Platform!


 

latest report
Learn why we are the Leaders in API management and iPaaS