May 26, 2016 | Written by: Eyal Herman
Categorized: How-tos | Mobile
Share this post:
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:
- 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.
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:
<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>
<h3>Connect your Facebook App Application to your Broker App</h3>
<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>
<h3>Create the Weather App in Bluemix</h3>
<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:
- 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.