Building a Bot for Facebook Messenger using IBM Bluemix

5 min read

Building a Bot for Facebook Messenger using IBM Bluemix

This example shows how a basic weather bot can be built for Facebook Messenger using the IBM Bluemix cloud platform. Although this example is very simple, it shows all the steps needed to build a real-life cognitive messaging bot.

example shows

If you want more background on this example and some practical tips, please see my prior post Messaging Bots are the next big wave in Mobile.

Introduction to the sample weather bot

The sample extends Facebook Messenger so the user can ask weather-related questions in natural language format. The bot responds by determining if the user asked about the weather condition or the temperature. Assuming the bot recognizes the desired location, it answers the user appropriately.

The solution is composed of the following modules:

  • The Broker is responsible for the connectivity with the Messaging Platform. In this example, it is connected only with Facebook Messenger, but you may extend it to connect with other Messaging Platforms.

  • The Weather App is called by the dispatcher in order to interpret the text and generate a reply. Again, weather is just an example. Your solution may include multiple apps, each one specialized in a specific domain (banking, dining, shopping, etc.)

  • The Weather App utilizes a few standard services that are available in the Bluemix catalog.

Below are some reasons Bluemix is well suited for this solution:

  • Quality of Interaction – Thanks to IBM Watson, the app offers superb natural language and dialog capabilities. Since these services are available through IBM Bluemix cloud platform, you can make these capabilities part of your bot.

  • Messaging Platform Independence – Building your bot decoupled from a specific Messaging Platform simplifies making it available on other Messaging Platforms and connecting it with multiple chat accounts. Avoid ‘walled gardens’ as this domain is very dynamic!

  • Backend Connectivity & Services – It’s easy to imagine allowing your user to successfully complete an action by connecting your bot with other cloud services or with your legacy IT. IBM Bluemix cloud platform facilitates the necessary capabilities and in a flexible and scalable way.

Setup Instructions

To build this solution, follow the steps below.


  • A Bluemix account

  • A Facebook account for your Bot

Configure Facebook Page and Application

  • From your Facebook homepage, select ‘Create a New Page’ from the menu.

  • Select ‘Cause or Community’, fill-out a name and click ‘Get Started’. You make skip the following stages of the wizard.

  • From your Facebook for Developers homepage, select My Apps / Create a New App and click ‘basic setup’

  • Fill-in the necessary information and click ‘Create App ID’

Create the Broker App in Bluemix

  • From your Bluemix dashboard, under ‘Cloud Foundry App’ click ‘Create App’

  • Select ‘Web’ and then select ‘SDK for Node.JS’,

  • Click ‘Continue’ and enter a name for your app.

  • Click ‘Download Starter Code’ and choose a download location.

  • Open the app.js file and replace the default sample code with the Broker sample code available at this Gist on GitHub.

  • Open the package.json file, and under dependencies apply the following changes:

    “body-parser”: “^1.15,0”,<br>
    “express”: “^4.13.4”,<br>
    “request”: “2.72.0”[code]<br>
        <li>To push the Broker App to Bluemix, use the following CF CLI command: <code>‘cf push using manifest file path/manifest.yml</code>, replacing <code>path</code> with the path to your file.</li><br>
    <h3>Connect your Facebook App Application to your Broker App</h3><br>
        <li>In your Facebook Developer homepage, under ‘Webhooks’, click New Subscription / Page</li><br>
        <li>Under ‘Callback URL’, fill out the Broker App URL; you can find it in Bluemix just under the name of the app’<br>
    <img src="/blogs/bluemix/wp-content/uploads/2016/05/Broker_app_URL.png" alt="Broker app URL" /></li><br>
        <li>Under ‘Verify Token’ fill out the token you’ve defined in the Broker App (in our example, we’ve used ‘mySecretAccessToken’. You may of course have your own).</li><br>
        <li>Under ‘Subscription Fields’, select messages, message_deliveries, messaging_options and messaging_postbacks.</li><br>
        <li>To get the Facebook access token, Click ‘Messenger’ and then ‘Get Started’. Select the page you’ve created earlier, click ‘okay’ and copy the Page Access Token than has been generated.</li><br>
        <li>To subscribe your app to the page, use the Terminal to run the following command: <code>curl -ik -X POST “Facebook_access_token_goes_here”</code></li><br>
        <li>Open again the <code>app.js</code> file and use Facebook token to the <code>token</code> var defined at the last section of the code.</li><br>
        <li>Push the Broker App to Bluemix as described previously.</li><br>
    <h3>Create the Weather App in Bluemix</h3><br>
        <li>Create a Bluemix app with AlchemyAPI, Insights for Weather, and Natural Language Classifier services bound to it.</li><br>
        <li>We've trained a basic Watson Natural Language Classifier that identifies whether a question is about weather conditions or temperature. <a href="">Getting started with the Natural Language Classifier service</a> explains how to train your own classifier.</li><br>
        <li>After you train your own classifier, you should obtain a classifier id.</li><br>
        <li>Similar to the previous section for the Broker app, download the starter code for your Node.js app.</li><br>
        <li>Open the <code>app.js</code> file and replace the default sample code with the Weather sample code available at <a href="">this Gist</a> on GitHub.</li><br>
        <li>Replace with the classifier id you obtained.</li><br>
        <li>Open the <code>package.json</code> file, and under <code>dependencies</code>, apply the following edits:<br>
    [code]"JSON": "^1.0.0",<br>
    "body-parser": "^1.15.0",<br>
    "cfenv": "^1.0.3",<br>
    "express": "^4.13.4",<br>
    "node-geocoder": "^3.9.1",<br>
    "request": "^2.71.0",<br>
    "watson-developer-cloud": "^1.4.1"<br>
  • Push the Weather App to Bluemix as described previously.

Connect the Broker App with the Weather App

  • Open the app.js file of the Broker app

  • Locate the following code:

    request("" + text, function (error, response, body)
  • Change the address to the URL of your Weather app

  • Push the app to Bluemix.

Did it work? If you encounter problems with the instructions above or have questions, please let me know in the comments below.

Be the first to hear about news, product updates, and innovation from IBM Cloud