build Facebook chatbot

Build a Facebook Messenger chat-bot in 10 minutes
Let’s build a chat-bot using FB Messenger, API.ai and PythonAnywhere in about 10 minutes. No server setup, 60 lines of code — easy.

Ingredients we’ll need:

an api.ai account
a PythonAnywhere account
your Facebook account
Once you’ve registered for these, we can begin.

Please follow the steps in order as the FB Messenger setup requires our webApp to respond.

Create an ‘agent’ on api.ai, call it ‘MyAgent’

Click ‘Domains’ to import a few conversational domains: “Small Talk” and “Wisdom”.

Click on “Intents” and let’s create a simple intent:

and its response: 42

Click “Save” then test it by using “Try it now…” in the upper right.

We now have a simple chat-bot.

To call it from our code we’ll need its “Client access token”. To see this information, click on the gear to the right of ‘MyAgent’ in the upper-left.

The next step is to run a WebApp (our ‘webHook) using Python and Flask on pythonanywhere. This will be the back-end to our chat-bot.

Add a new WebApp.

Provide a prefix for your WebApp: some-name.pythonanywhere.com

Select ‘Flask’ for a framework, on Python 3.5

You should see a default flask_app.py in your WebApp files directory. Click to edit it.

Here’s our 60 lines of code to handle the flow between FB messenger and api.ai:

use your FB and api.ai credentials
You will need to pip install packages (eg. apiai) in your pythonanywhere Bash console, as follows:

installing apiai in Bash console
Also remember to restart your pythonanywhere web app anytime you alter the code. Click on the circular button in the upper-right, as shown below:

pythonanywhere shortcuts
If everything is in place you should be able to ping your new Flask app:

be sure to use your URL prefix instead of ‘gk.’

Now we can setup a FB page and messenger setup.

This will take 5 quick steps.

Step 1: create a new FB page
(click on the link above to create a new page) Give it a name, like ‘BuildBot10mins’, or something less idiotic.

Step 2: create a FB App
Click “Skip and Create App ID” at the top right. Then create a new Facebook App for your bot and give your app a name, category and contact email.

Step 3: create a new access token
Create a new token (for the FB page created in step 1):

Copy this new Access Token and Paste it in your pythonanywhere code (line 4: under # FB messenger credentials)

Step 4: create our WebHook, click “Setup Webhooks”
Provide your Callback URL (be sure to use https://)

Use the same verify token specified in your pythonanywhere code.

be sure to specify https:// for the Callback URL
Step 5: subscribe to our WebHook
Select your newly created FB page and click ‘Subscribe’, this connects our FB Page with the FB Messenger App we just setup.

don’t forget this last step
What did we just do? Let’s review our steps:

1 (handling responses)
In the first step we created a really simple chat-bot on api.ai, it has some basic conversational abilities, and knows the meaning of life. You can use whatever chat-bot framework you like.

2 (the back-end)
In the second step we built a Python Flask app on pythonanywhere, this allows us to host a WebApp without building a server. You can use other serverless platforms such as Amazon Lambda.

3 (the front-end)
Our third step was setting up FB Messenger and connecting a WebHook between it and our WebApp. You can also build the same bot using SMS.

The WebApp is the broker between incoming messages, the chat-bot interface and outgoing responses.

Makes sense? Good. Now you can chat with your bot, find it by searching for its name:

then scroll down the page to ‘Message Now’

There you have it — a FB Messenger chat-bot in 10min

NewsPages see if links are okay

21742819_1993316340907079_1869873187264341192_n.png?oh=775babd09dcdd8fe4d696b1747a200d9&oe=5A92D1D5

Guam News8

Wyoming news6

21078602_1965508583685049_3108069310817822970_n.png?oh=8f8054000927ded46d652ff5a78b7d58&oe=5ACCB7C9

Wisconsin news6

22089381_226600761203566_5461355387362945684_n.jpg?oh=ed9801d0be1f438f0b8884bda3c06f10&oe=5AD410A4

West Virginia news8

21105548_943869322420543_2007244046496709048_n.png?oh=eb3c9f0df9d6533558731831efe26657&oe=5AD2C741

Washington news12

21231100_117998902196106_8250289503379453761_n.jpg?oh=4aced20f028c326ee8d381859754dede&oe=5AD6CF77

Virginia news7

21192007_131177570836046_2180845165800185834_n.png?oh=c84847ebbc0bf2c7ca34d8243cacf52f&oe=5A89BEFE

Vermont News15

21106310_1979877072297347_8077558567690766214_n.png?oh=e57791c54c151315ab42ac3e0c8d68d8&oe=5A996239

Utah News8

21106522_1491937774176687_3165132741938044021_n.png?oh=fbd6d1f1ce2d1a7b1bbe126095089c5e&oe=5AD15D07

Texas News15

21077353_1940854372869052_3224287936060582230_n.png?oh=532119779c02d28949759ab6553a4363&oe=5AD28FCF

Tennessee News13

21078394_117732018953159_858650517962931684_n.png?oh=67bc81275f80acad5e4659ac498b291c&oe=5A8DCC56

South Dakota News18

21105480_319378058527760_7159254643304636912_n.png?oh=c77e9b9d5a8f11727fb6a16901303965&oe=5A8B7A3E

South Carolina News7

21106504_1884538371865360_3829355158670259999_n.png?oh=1731cd0801856978ea52ffbde35c576e&oe=5A9EDCE9

Rhode Island News9

20708193_664503197087098_8327191679641519847_n.png?oh=7d9c34d4901e13214f4b6f329618d177&oe=5A91AB72

Oregon News9

20664882_1009595499143568_3559991076943466189_n.png?oh=ad52a6ca7d257d6be065899893250369&oe=5AD5BD4A

Oklahoma News9

Ohio news6

19554422_122398355027277_2147571674793298970_n.jpg?oh=e44cfd5ed5ecafad84d4145ec0951d56&oe=5A9CB56B

North Dakota News18

19756825_452318298478394_3813600018138527565_n.jpg?oh=bbfb3fbfd3c2d41375d3c92fbb3f8828&oe=5A9F0553

North Carolina News8

19554441_445724402451247_9114722221931346247_n.jpg?oh=57aa2851f434970f5c4b07f7f2088183&oe=5A905293

New York News10

19553870_119648438644462_4339228793826553019_n.jpg?oh=dba6312b4ff23cab42794735574d6229&oe=5AD12570

New Jersey News16

19642323_280317709100620_3476987266604175585_n.jpg?oh=278dafd21b0c03a607a0c087447f6a9b&oe=5ACF2F89

New Hampshire News16

19642634_139327976645174_7025403935608894457_n.jpg?oh=486f523259d8264a0a0627000209e916&oe=5AD3ACA6

Nevada News

19554533_201165343745616_6074199828370835617_n.jpg?oh=ecb6faa0bc921e7be08e907de0138a78&oe=5AD17814

Nebraska News8

19554144_304117340058472_1233674118686198679_n.jpg?oh=e500bde002cbc7257c6277a2534f452e&oe=5ACEF0C9

Montana News8

19437387_111287809493629_6862413131635050846_n.jpg?oh=c5613415ab80606d325422bb1779b3a6&oe=5ACF6746

Missouri News8

19511141_1824576797871024_500511464427352615_n.jpg?oh=95909fd4c3d6319d8872bfb4982fbed3&oe=5A8ECE69

Mississippi News7

19511087_144599222762828_6078436469822605688_n.jpg?oh=61b3dce6946f2a17b03750b79b6702d7&oe=5AC9BE18

Minnesota News12

19511417_664153417113114_2938710228437703024_n.jpg?oh=c51b3dbb9110513a034a85d3db3d438e&oe=5A939CCD

News in Michigan20+

19424470_701151363410464_7893597126620108375_n.jpg?oh=25ddb0983cd78173ce7e42d5ad563f32&oe=5A98813C

Maryland News20+

399548_10149999285987789_1102888142_n.png?oh=9272252b14fdfc811c394a6148afccf6&oe=5A98EAEC

GuamFix8

18951187_685996208254436_1125895315542865159_n.jpg?oh=ebf825cbd88aadc5b2905b8d0c667d70&oe=5A92FDC9

Louisiana news20+

18838811_1615361855142645_2493812731713050820_n.jpg?oh=3d9b0eef5e52a76655032f885baf578f&oe=5A9A9499

Kentucky News11

18839111_103398186928891_2456039326262549509_n.jpg?oh=167e85f0a128922ca9574adbddcf9bd7&oe=5AD5BF76

Kansas news20+

18835613_1920444988244495_3581312864556506405_n.jpg?oh=86388b6059122d06d73992ee6a783802&oe=5AA01564

Iowa news10

18740444_316590525421345_6679477187052597709_n.jpg?oh=cf8b55a5a3438f4d4c274ca6e1231d99&oe=5A97DE3C

Indiana news9

18835940_1306574726129273_5226011231974641566_n.jpg?oh=466322d186033107ba5cd5221d944c82&oe=5A8E02EB

Illinois news3

18814185_264133154060653_3004459831338013070_n.jpg?oh=302be8ffe3db9caca0fbf47f524d1b82&oe=5AA14624

Idaho news20

18765855_120009998498429_1277230053853628222_n.jpg?oh=c1e71fbf770938f88a3942a01d5f8250&oe=5ACFD7EA

Georgia news8

18893250_236890146798408_2262721092563402081_n.jpg?oh=3aaea0bc43bfdbe488bf30ea6617c093&oe=5A898A90

Florida News8

18921630_1222820547827267_4745196106450127500_n.jpg?oh=f1f6884d7c0838a46e3e3840eebcba01&oe=5ACF919C

Delaware News

18893053_102695993666380_551366044493493684_n.jpg?oh=5f51f14342592181fb72262da1057325&oe=5A9FFD05

Connecticut News

18881737_1484284434925853_7853713594809247484_n.jpg?oh=75d41c70e164f30425405f6688bbe63b&oe=5A8F0CDD

California News8

18893353_1384415234958502_8451091027979334724_n.jpg?oh=cd36d1a9b5301cbb09158d6b747bb1f8&oe=5A9158C9

Arkansas News11

18814308_1370740433019959_5917779576890458863_n.jpg?oh=4344fa035289a4b4dd585a4e2e402f18&oe=5A9E0CA8

Alabama News6

18892895_1348389815196184_1556716302300674632_n.jpg?oh=0036e2a2042326b7021f0916146986af&oe=5A9B243A

Hawaii News20+

18814173_105708240027169_2662425591598469313_n.jpg?oh=eb08aa4b233a652627b7a623f5c9120d&oe=5A9B1D4F

Alaska News12

News Arizona20+

News NewMexico7

Colorado news18

Solar NowNow