Build a Facebook Chatbot in Python

Nowadays there are many articles like ‘build a chatbot without any code’. But somebody still feels comfortable with codes. I’ll tell you here about to build a Facebook Messenger chatbot rapidly with BotHub.Studio.

Creating a Facebook Messenger Bot

(You need to have a Facebook page to link a Facebook Messenger chatbot .)

Let’s create an app. Move to Facebook developer site.

Create a new app.

Enter a bot name and contact email.

Now you can see a dashboard like below. You can find an App ID and App Secret Code here.

Add a product to the app. Click Set Up button in Messenger section.

Link it to your Facebook page. Then you can find a Page Access Token there.

Note three entries below to deploy your chatbot to hosting service later.

  1. App ID
  2. App Secret Code
  3. Page Access Token

Setup Hosting for Bot

First, create an account on BotHub.Studio, a chatbot hosting service for developers, and install CLI tool using the command below:

$ pip install bothub-cli

Run configure next:

$ bothub configure

Create a project directory:

$ mkdir MyBot
$ cd MyBot
$ bothub init

A template code will be generated after init finished:

|-- bothub
| |--
| `--
|-- bothub.yml
|-- requirements.txt
`-- tests

Primary template code, specifically contains an echo bot code which mimics a user message.

Now we use the facebook credentials you note:

$ bothub channel add facebook --page-access-token=<token> \
--app-secret=<secret> \

And deploy the project to server:

$ bothub deploy

Send a message to your chatbot and see it works well. Search your Facebook page name on Facebook Messenger, then you can talk to your chatbot.