How-tos

Building a Bot for Facebook Messenger using IBM Bluemix

Share this post:

Weather Chat Bot 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.

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:

Main Modules for Bots

  • 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.

Prerequisites

  • 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”,
    “express”: “^4.13.4”,
    “request”: “2.72.0”
    </li>
    	<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>
    </ul>
    <h3>Connect your Facebook App Application to your Broker App</h3>
    <ul>
    	<li>In your Facebook Developer homepage, under ‘Webhooks’, click New Subscription / Page</li>
    	<li>Under ‘Callback URL’, fill out the Broker App URL; you can find it in Bluemix just under the name of the app’
    <img src="/blogs/bluemix/wp-content/uploads/2016/05/Broker_app_URL.png" alt="Broker app URL" /></li>
    	<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>
    	<li>Under ‘Subscription Fields’, select messages, message_deliveries, messaging_options and messaging_postbacks.</li>
    	<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>
    	<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>
    	<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>
    	<li>Push the Broker App to Bluemix as described previously.</li>
    </ul>
    <h3>Create the Weather App in Bluemix</h3>
    <ul>
    	<li>Create a Bluemix app with AlchemyAPI, Insights for Weather, and Natural Language Classifier services bound to it.</li>
    	<li>We've trained a basic Watson Natural Language Classifier that identifies whether a question is about weather conditions or temperature. <a href="http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/nl-classifier/get_start.shtml">Getting started with the Natural Language Classifier service</a> explains how to train your own classifier.</li>
    	<li>After you train your own classifier, you should obtain a classifier id.</li>
    	<li>Similar to the previous section for the Broker app, download the starter code for your Node.js app.</li>
    	<li>Open the <code>app.js</code> file and replace the default sample code with the Weather sample code available at <a href="https://gist.github.com/eyal-he/a97e0bc70687dd0c579fab63b5727e91">this Gist</a> on GitHub.</li>
    	<li>Replace with the classifier id you obtained.</li>
    	<li>Open the <code>package.json</code> file, and under <code>dependencies</code>, apply the following edits:
    "JSON": "^1.0.0",
    "body-parser": "^1.15.0",
    "cfenv": "^1.0.3",
    "express": "^4.13.4",
    "node-geocoder": "^3.9.1",
    "request": "^2.71.0",
    "watson-developer-cloud": "^1.4.1"
    
  • 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("https://whatistheweather.mybluemix.net/getWeather?text=" + 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.

More How-tos stories

Configuring IBM Cloud App ID From the Toolchain

Learn how to configure the IBM Cloud App ID from a toolchain as part of continuous delivery process. This post gives some background and the script we use.

Continue reading

How to Backup Your IBM Cloud Linux Server

This post covers how to backup entire partitions in Linux systems. The process utilizes the tar command in IBM Cloud's unique rescue environment and showcases the simplicity and flexibility of the process.

Continue reading

Speed up your WordPress with IBM Cloud

WordPress is one of the most popular content management systems available, but the many websites and blogs that use it experience issues with speed. At IBM Cloud, there are several solutions that can help alleviate some of these issues and allow you to have a better and faster WordPress experience.

Continue reading