Chatbots have been around for a while now and I’ve had the opportunity to build a couple of them for clients using Oracle and AWS tools.

I’m not a huge fan of brands building chatbot to use as customer support, let’s be honest, there is nothing worse than having to cope with a robot when you need to contact a support team for a non-generic issue. However, Chatbots have some pretty good use cases and are really fun to build.

In this tutorial, I’ll show you the easiest way to leverage AWS lambda and claudia.js bot builder framework to easily build a fairly simple Facebook joke chatbot.

Feel free to take a look at mine, fully open source, here (currently unavailable) :

http://m.me/1429298330507615

Estimated completion time: ~1 to 2 hours.

Prerequisite :

– Available AWS account.

– Npm library installed (see: www.npmjs.com/get-npm)

1. Setting up our environment

Create a project folder wherever you want to store the project locally (mine is /home/ubuntu/FbJokeBot/).

Open a terminal inside the project folder and run the following commands :

Note: for this tutorial we’re gonna diverge from the official hello world tutorial on claudia.js by building a bot that gives you joke on keywords using the great icanhazdadjoke.com/api API!

If you don’t want to build a joke bot, feel free to follow the official hello world tutorial here claudiajs.com/tutorials/hello-world-chatbot.html

Now that all the library we need is installed in our project folder, we just need to create an empty bot.js file. (for me : /home/ubuntu/FbJokeBot/bot.js)

Now add the following js code if you want to build a replica of my joke bot :

Note: This bot uses the great API: icanhazdadjoke.com/api to find jokes. If you like the jokes, go show them your support!

As you can understand by reading the comments, this bot will try to find a joke using keywords from the user message and send it back to him as a Facebook message.

That’s it, our code is ready. We now need to deploy it to AWS Lambda and connect a facebook app to it.

 

2.Deploying the bot to AWS Lambda

Claudia.js come packaged with a couple of commands to set up AWS from your project folder terminal.

For more information about those commands see: create.md and claudiajs.com/documentation.html

claudia create –region eu-west-1 –api-module bot

Now that our bot is deployed to AWS Lambda let’s create a Facebook app and link it :

 

3.Linking it to facebook API

To do that we need to create a Facebook application following the Facebook developer documentation: developers.facebook.com/docs/messenger-platform/getting-started/quick-start

As said in the documentation: A Facebook app is what links your Facebook Page to your webhook, and is where you will configure various settings for your Messenger bot, generate access tokens, and choose what events are sent to your webhook.

So what we need to do now, is create a Facebook Page, add to it a Facebook app that will send all text message to some webhook that we will generate using Claudiajs.

  • Create a facebook page

Go to www.facebook.com/pages/create and create a new page to host your messenger bot. This is the identity of your bot, so choose the name well.

  • Create a facebook application

If you don’t already have one, go to developers.facebook.com/apps/, and create a Facebook developer account.

Then create a new app. Specify the app name and your email.

You can now add a product to the newly created app. We want to add a messenger product to get our users messages.

  • Link your messenger app by generating a token to your page.

Since the 2018 Cambridge Analytica’s user privacy scandal you may have to ask for a review, for more information refer to developers.facebook.com/status/issues/104197713763517/

To do so, go to Messenger > configure > Parameters > pages_messaging > add to examine > Add details > ADD YOUR PAGE AND BOT INFORMATION

Then generate the token : Messenger > Configure > Parameters > Generate Tokens > Select your page

Save the Page token for the next step.

  • Send a message to our Lambda function using claudiajs generated webhook

(Messenger > Configure > Parameters > Webhooks > Generate Webhooks)

Run the following command in your project terminal to get the webhooks URL and verify token :

And that’s it, the bot is ready to be tested. If you want to make it live, submit the Facebook application to review.

Note: From now on to update your bot code just run ‘Claudia update‘ in the project folder terminal.