Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Building Facebook chatbot with bluemix

How does it work?

Facebook Messager API <-----> Broker <-----> Weather Bot

The broker app bind with Facebook API would receive the message if there is a text sent to your fanpage. And the broker would deal with the message and make a request to the weather bot. And then the weather bot would use Bluemix Natural Language Classifier to determine what kind of information does the user want. Finally the weather bot would make a request to Bluemix Weather Insights to get the weather data of the location and send it back to Facebook messager.


Please install the following tools you need.

For linux/mac os x users, it is recommended to install them by the package manager of your linux system.

For windows users, you can link to following sites and install them by instruction.

  1. git

  2. python

    please remember to add python to enviroment path while installing.

  3. cloudfoundry-cli

Also, you need to have a bluemix account, that's important.

Broker Setup

Please open your terminal/windows command line and type the following command.

git clone

And then change directory to Bluemix-tutorial/facebook-weather-bot/broker.

Edit the manifest.yml, chanage the name and host to whatever you like.

Notice that the name you choosed must be unique because the server would be work on the internet,

- path: .
  memory: 256M
  instances: 1
  name: yourname-broker
  host: yourname-broker
  disk_quota: 1024M

Create a facebook fanpage if you don't have one, and then visit the facebook for developers website and create a app.


Click 'Add Product' -> 'Messenger'.


Token Generation

Choose your fanpage at Select a Page, and copy the page access token. Edit the app.js and replace the var token.



var token = "pasteYourAccessTokenHere";
var host = (process.env.VCAP_APP_HOST || 'localhost');
var port = (process.env.VCAP_APP_PORT || 3000);
app.listen(port, host);

And edit the request url of the weather bot, just replace the 'yourname' to what you want, but notice that to it must be the same with the name set in following weather bot setting.

For example, set "

Bluemix-tutorial/facebook-weather-bot/broker/app.js'/webhook/', function (req, res) {
    messaging_events = req.body.entry[0].messaging;
    for (i = 0; i < messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i];
        sender =;
        if (event.message && event.message.text) {
            text = event.message.text;
// Calling the Weather App. Change the address below to the url of your Weather app. Response is sent back to the user via the sendMessage function //
            request("" + text, function (error, response, body) {
                sendMessage(sender, body);

And remember to run the following python code to set the access token.

python "PasteYourAccessTokenHere"

Now we can use cloudfoundry-cli to deploy this app. Note that you need to execute the 'cf push' command in the broker directory.

cf login -a
cf push

If you can't login by cf, make sure you have set the organization and space correctly, note that the locale must be set to US South.

And if the Bluemix UI don't automatically guide you to set it when you first log in, try to create a new CloudFoundry on Bluemix website UI, and choose any SDK since you don't really use it, this process would invoke the guide.

After the broker app successfully running at cloud, we should set webhook in Facebook developer webpage.

Setup the Webhooks


  • Callback URL: fill in your broker webhook url (e.g. )
  • Verification Token: fb_weather_bot_verify_token (this token is set in broker/app.js)
  • Subscription Fields: messages, messaging_options, messaging_postbacks, message_reads, message_delivers

and remember to select your fanpage to subscribe your webhook to page events.

Weather Bot Setup

Create the Bluemix necessary services.

cf create-service natural_language_classifier standard yourname-weather-bot-NLC 
cf create-service weatherinsights Free-v2 yourname-weather-bot-WI

And edit the Bluemix/facebook-weather-bot/weather-bot/manifest.yml, then fill in your name.

- path: .
  memory: 256M
  instances: 1
  name: yourname-weather-bot
  host: yourname-weather-bot
  disk_quota: 1024M
  - yourname-weather-bot-NLC
  - yourname-weather-bot-WI

Push it with cf, it would create a weather-bot app on your Bluemix, and bind the service you created to this app.

cf push

Now you can obeserve the status of your app, please check wether the two apps broker and weathor-bot are working normally.

cf apps
cf logs yourname-weather-bot --recent
cf logs yourname-borker --recent

Train the Natural Language Classifier

First we need to get the credential key of your service.

We can view this by the environment variable in weather-bot app.

cf env yourname-weather-bot

Copy the credential of natural language service and save it as new file credential.json in Bluemix-tutorial/facebook-weather-bot/weather-bot/NLC.

For example credential.json.

 "password": "PLX7Ua3YeZaK",
 "url": "",
 "username": "c918a7f7-6f5e-4f7a-8426-4c2080645cca"

Upload the training file weather_data_train.csv to Bluemix and create a classifier.

Since we use python to do this task, so we need to install some packages before creating the classifier. Install pandoc from the following link.

Install watson_developer_cloud API for python.

pip install watson_developer_cloud

And it may take some time to create a new classifer (training may take 10 minutes).


You can check the status by running the


And after the trainging finished, you can check wether the NLC is working by running


Now push again to update the new app to Bluemix. Change directory back to Bluemix-tutorial/facebook-weather-bot/weather-bot and type in the following command.

cf push


Log in your facebook and try to chat with weather-bot!



  • If you get 404 error, it may be caused by your weather-bot app is died, just restart it.
cf restart yourname-weather-bot


You can’t perform that action at this time.