Building a Facebook messenger bot deployed on Nimbella

Shreya Sinha
3 min readNov 21, 2020

Zebra is a Facebook messenger utility bot. When provided with a set of keywords — it displays the recent news that is floating around related to those keywords — and also gives a verdict on those news.

Zebra was built as a part of the #DeveloperIPL hackathon organized by Nimbella , Postman and HackerEarth

The aim of the chat bot that we will build is to get verdicts on news articles based on keywords that are provided as an input. We will be doing it in the following manner:

  1. Set-up the APIs for out chat-bot and expose the endpoints
  2. Deploying on Nimbella
  3. Set-up the Facebook messenger bot
  4. Set-up Postman monitors to monitor the health of our bot

Set up the endpoints

The code follows the following directory structure:

zebra (name of your project) > packages > default > webhook (name of your endpoint/function)

In the webhook module we will create a file __main__.py which contains the main method which acts as an entry point for our bot.

In the above code snippet, since we wish to use a common endpoint (/webhook) for both our GET and POST requests, we are handling them based on the “_ow_method” parameter received from the request sent by Facebook.

The [GET] /webhook endpoint is used for a challenge-response authentication for our chat bot. If the verify_token sent as a part of the request from Facebook is equal to the “WEBHOOK_VERIFY_TOKEN” configured in the server — the bot is authenticated.

The purpose and the values of environment variables WEBHOOK_VERIFY_TOKEN and TOKEN will be explained as part of the section “Set up the Messenger Bot”.

If the request is for [POST] /webhook endpoint, we process the request message and send the appropriate verdict to the user. The message is processed as follows in the method processDataFromInputMessage:

The method getClaimReviews uses the Fact Check API by Google Developers. You will need to set up a developer account on the same and use the API_KEY that is generated for you. Add this API_KEY to your environment variables to use in the above code.

This documentation by Facebook serves as a good source to know more about the format of sending messages to the Facebook messenger bot.

Deploying on Nimbella

  1. Create a Nimbella account and get your token. You can also use your GitHub account to create your Nimbella account.
  2. This token can be used on Nimbella Workbench or with the command line tool nim. Setup details can be found here.
  3. Run the command nim auth login to authenticate. This should also give you the path at which your APIs will be deployed. (Eg: https://abcd0235–5trfcqwerpoiu-apigcp.nimbella.io)
  4. Deploy from your local path by running the following nim command in your terminal: nim project deploy /local/path/to/demos/<demo-subdirectory> (just this one step — and it is deployed!!)
  5. Run nim action list to see all the actions that are currently up.
  6. Your endpoints should now be up for use! They should be accessible at a path derived from the domain name assigned to you + your folder structure. (Eg: https://abcd0235–5trfcqwerpoiu-apigcp.nimbella.io/api/default/webhook)

Set up the Messenger Bot

In this post we can see how to set-up our Facebook messenger bot and integrate with the same.

Set up Postman Monitors

We can set up postman monitors in order to monitor the health and usage of our app. This documentation by Postman serves as a good starting point for the same.

Sample json for the monitor can be found in the repository mentioned in the footnotes.

The complete code can be found at: https://github.com/shreya0702/zebra-bot

--

--