Building a Facebook Messenger Jobs Bot For Coinmonks Using The Crowdbotics App Builder

I used the Crowdbotics App Builder to build and deploy a Facebook Messenger chat bot for my business. The CB Builder helped me get up and running faster. Here’s how it works.

Gaurav Agrawal
Crowdbotics
6 min readJan 29, 2019

--

Apart from occasionally contributing articles to Crowdbotics, I also run Coinmonks — a Blockchain job portal and a Medium publication. We have a Facebook page where people frequently ping us for help with blockchain jobs. When I learned about the new Facebook Messenger scaffold on the Crowdbotics App Builder I thought, let’s build a bot for my company page that will automatically send jobs to those souls who are seeking them .😆 I hopped on the Crowdbotics beta App Builder and started building a bot for our Coinmonks Facebook page.

When it comes to building a new app, I love using Crowdbotics’ scaffolding. It makes my life so much easier. I’ve used the Crowdbotics App Builder to help me build several different new apps faster and cleaner than I otherwise would have been able to.

Check out live Bot here.

What is the Crowdbotics App Builder?

I explained this on my previous blog where I built a small cryptocurrency portfolio tracker using Crowdbotics. I’ll recap briefly:

With the Crowdbotics App Builder, you can build, deploy, and scale applications with high-quality app templates. (If you are not a developer, you can also hire domain-expert developers directly through the Crowdbotics app. Essentially, Crowdbotics takes care of the initial setup and devops that can be quite time-consuming, and sometimes tricky to get right — especially for app types and frameworks you are not particularly familiar with.

Scaffold your own Facebook Messenger bot in 30 seconds using the Crowdbotics App Builder

Building a Facebook Messenger Bot

When you use the Facebook Messenger Bot scaffold on Crowdbotics, it gives you two things:

  • Facebook messager bot config + Basic code
  • Wit.ai configuration + Basic code

Prerequisite

  • Facebook Page Token
  • Wit AI token

While creating your scaffold with the Crowdbotics App Builder, it will ask for Facebook verification token (decided by you), Facebook Page token, and Wit.ai Token. The Wit.ai Team

How to get Facebook Page token?

You can read here about setting a Facebook app and getting “page token” for your app. I will add steps for simplicity If you get confused read this guide.

  1. Create a Facebook developer account, If you don’t already have one.
  2. Create a Facebook page, if you don’t own one.
  3. Create a new Facebook app using this link.
  4. Add basic info, You don’t need to fill extra info while developing the bot.
  5. Add messenger using, “Add product” option.
  6. In messenger settings, get “Page token”. Next get a webhook.

Getting a Webhook

Facebook send events to your application using webbooks. In this step, you will see how the Crowdbotics really saves you time. So, when you create a scaffold, Crowdbotics adds a new repository which you will clone for developing your application. This is my repository which Crwodbotics automatically created for me.

Currently, this scaffold is in Python, though, I know Crowdbotics is planing to add more languages.

When you add a Webhook on messenger platform, it sends a verify token with a challenge string, you need to return this string back, that's how Facebook checks that Webhook is alive and valid.

The Crowdbotics scaffold already has code for this.

@app.get('/webhook')
def messenger_webhook():
"""
A webhook to return a challenge
"""
verify_token = request.query.get('hub.verify_token')
# check whether the verify tokens match
if verify_token == FB_VERIFY_TOKEN:
# respond with the challenge to confirm
challenge = request.query.get('hub.challenge')
return challenge
else:
return 'Invalid Request or Verification Token'

Running Facebook messenger Scaffold

Crowdbotics Messenger scaffold uses Bottle, A lightweight WSGI micro web-framework for Python. To run scaffold, you need to create a virtual environment and install dependencies. You can use below commands for that. Before that, you need to install pipenv. Let’s run our scaffold.

pipenv --python /usr/bin/python3.5pipenv installpython messenger.py 8080

Note: I am using ubuntu.

You should add all the tokens (WIT_TOKEN, FB_PAGE_TOKEN, FB_VERIFY_TOKEN) as environment variables or for testing purpose add them directly in the messenger.py and hello.py. 😉 (Not a secure practice).

Note- FB_VERIFY_TOKEN is “verify token” which you decide, In my case, it was simple coinmonks. This “verify token” will be used while adding our webhook on Facebook app console.

Using Ngrok

Webhooks must have a valid SSL certificate, signed by a certificate authority and won’t work on your localhost, so we will use ngrok as a proxy to expose our local host for facebook webhook URL. Simply install ngrok and run below command.

./ngrok http 8080

This will give us an Https URL which we will enter while adding our webhook.

Adding Webhook to Facebook

Now, we need to add this webhook to Facebook's app console. Go to your app and add this webhook, and subscribe for messages and messaging_postback events for your facebook page. While adding webhook, facebook send a get request to our app to verify our webhook.

Note: Our webhook is under YOUR_NGROK_URL/webhook.

Interacting with our Bot

If you were able to do above setup, now we are ready to interact with our messenger bot. Just send a message from messenger app or facebook desktop to your page. On ngrok console, you will be able to see that facebook sends a post request your app. Crowdbotics scaffold already has code to handle the basic interaction too 😃.

Adding Coinmonks Jobs API

I didn’t need Wit.ai, so I didn’t use it, rather, I converted Coinmomks Jobs RSS feed into JSON and used it to send jobs to through messenger. I wrote a small NodeJs app for this, as I am having a hard time to find a good RSS to JSON library. (If you have a recommendation, let me know.)

You can check that Node app here, it’s hosted on Glitch.

Testing Our Messenger Bot

The bot is live and you can use it for getting blockchains related jobs.

To test our Bot check here.

Remember you need to use “job” Keyword to get the results, so you can just type “job” and you will get results from Coinmonks Jobs portal.

Deploying our Bot on Heroku

Crowdbotics helps you to deploy you app on Heroku. To deploy the messenger I added a new file with the name Procfile (No extension) and add below ths line in the file.

web: python messenger.py $PORT

Once deployed you should change your webhook URL.

Messenger for Developers

Facebook messenger platform provides a rich set of APIs. Here are some important features:

  • Messaging — Send and receive text, media, structured templates, and so much more
  • Payments — Accept payments inside conversations, or build your own flow in the web view
  • Natural language Processing — Get conversational and gain context with natural language processing built right in

@Facebook’s integration of different technologies on messenger platform enables developers to build highly engaging applications and bots.

Building Bots is fun. If you are a developer, you should check out the Facebook messenger platform. Using Crowdbotics will make your life easier too 😃. The Crowdbotics App Builder is a very helpful free tool for developers.

Messenger for Businesses

Facebook is giving a lot of functionality to pages which can drive sales and marketing efforts. Every business should have a Facebook page and If you already have one, then you can engage your users/customers using messenger bots. There are thousands of Apps already integrate to Facebook messenger platform.

Facebook has more than 22 billion average monthly views and more than 2.27 Billion monthly active users. In the United States, Facebook Messenger is the most popular mobile messaging app in terms of number of users. With 103.55 million monthly active U.S. users [February 2018]. If you are business, you should leverage Facebook Messenger platform to reach a wider audience and engage your customer or get new customers.

So what are you waiting for? Build a messenger bot!

Scaffold and deploy an app using the Crowdbotics App Builder here for free. I recommend giving it a try.

If you have questions or feedback on my Facebook messenger app, drop them in the comments.

Thanks!

--

--