In the past few years, there’s been a huge increase in interest around chatbots, specifically due to their ability to engage and entertain in a conversational format. Facebook Messenger Bots is a great target for customer engagement given the billions of people that are active on Facebook. At StdLib, we’re going to show you how you can easily create a FBM bot in a few simple steps — and we’re going to do it using new “serverless” technology that means you don’t have to set up or configure servers or infrastructure.

Messenger bots, or chat bots of any kind really, are an example of when serverless technology can shine. When running a chat bot, a server only needs to respond to events, generally passed to a webhook exposed by the server. That can lead to a lot of idle time, which can be costly. Here at StdLib, we’ve worked hard to make deploying serverless APIs as easy as can be. And the best part is, you’ll never pay for idle time and your webhooks will always be available. With this tutorial in just a few minutes you’ll have your own blackjack dealing Facebook messenger bot! If you’d like to see the full code for the webhook it’s available here otherwise keep reading.

Minute 1: Sign Up for StdLib

Getting started with StdLib is easy — head over to our website, choose a username and hit “Claim Namespace”. Your namespace, or username, is where all your services get published to. For instance, your blackjack service will called lib.<username>.blackjack-bot . Later on, we’ll go over installing our CLI and deploying services. But before that, you need set up some things on Facebook’s end.

Minute 2: Create a Messenger Bot

First things first, messenger bots need to be attached to a Facebook page. Assuming you don’t have one already, head here to make one. It doesn’t really matter what kind of page you make but a ‘Product’ page type with ‘Video Game’ as the category will work.

While not necessary, after creating the page you should unpublished it so people don’t stumble upon the bot. Click ‘Settings’ on the top right of the page and it’s the first option available.

Now it’s time to create the bot. Head over to the Facebook Developers page , click ‘Add a New App’ and fill out the prompt.

Now, while on the new app’s page, hit ‘Add Product’ and choose messenger. Scroll down and you’ll see a section titled ‘Webhooks’. When your bot receives a message from someone, that event will be sent to the webhook specified. For this task, StdLib is a great option. With zero infrastructure management, you can have your webhook set up in only a few minutes.

Minute 4: Create a Webhook

First, in order to ship to StdLib, you’ll need to get the command line tools, available here on GitHub. If you don’t have Node.js installed you can download the latest version, along with npm, here. Now with Node installed you can get the StdLib CLI by opening up a terminal and running:

$ npm install lib.cli -g

Now create a workspace and navigate to it with:

$ mkdir stdlib-workspace

$ cd stdlib-workspace

$ lib init

Next, get the code for the webhook by running:

$ lib create -s @steve/blackjack-bot

You’ll be prompted to give the new service a name, the rest of this tutorial assumes it’s still blackjack-bot . Now you can navigate to the new service with:

$ cd <username>/blackjack-bot

Before we deploy our service, we have to fill in some environment variables. Open the env.json file found in the root of the directory. There are three fields, VERIFY_TOKEN , PAGE_ACCESS_TOKEN , and STDLIB_TOKEN . VERIFY_TOKEN is a token that you create and then give to Facebook so that you can verify the events sent to the webhook are legitimate. You can create one by running

$ lib steve.generateToken

From your terminal or by using this code snippet:

PAGE_ACCESS_TOKEN gives your bot access to a Facebook page. On the messenger page, scroll to ‘Token Generation’, click the drop down, select the page you just created above and copy the token into env.json .

Finally, the STDLIB_TOKEN is used to authorize your function calls to StdLib. You can find a token in your StdLib dashboard.

With your three environment variables ready to go, its time to deploy the webhook. While in the blackjack-bot directory run:

$ lib up dev

This creates an infinitely scalable webhook that you can expose to Facebook. If you change your code, just run the command again to redeploy. Now, with the webhook ready, click “Setup Webhook” and fill out the form.

The URL will be https://<username>.api.stdlib.com/blackjack-bot@dev/:bg . Note the :bg at the end tells the webhook to run as a background worker, meaning it instantly returns a 200 OK and then executes the function. This is useful because Facebook requires prompt responses to their events, and this gives the webhook more time to respond. ‘Verify Token’ is the same one you put in your env.json . Before saving, make sure to also subscribe your webhook to ‘messages’ and ‘messaging_postbacks’. This allows the bot to receive messages send to the page.

After the webhook is verified, you’re all set! Go to your new Facebook page and send it a message. If you type ‘blackjack’ it’ll prompt you to start a hand. From there the game plays completely through the quick replies. Place a bet, play your hand and repeat!

Minute 6: Extending Your Bot

This bot is just a starting point — you can easily modify and extend it to create more complex workflows that suit your needs. Out of the box it comes ready to handle messages and quick replies (like ‘Start Game’ and ‘Nope’ above). There are two endpoints that run the bot:

__main__.js : Receives one or more events from Facebook, where each event is a message to your bot. It looks up the state of the conversation with that user in a key value store. If the event contains a quick reply, it gets dispatched to handlePayload() , or if it is just text it goes to handleText() . If you want to change the bots functionality, you would change those functions. After the new state is made and the response from the bot generated, the state is saved and the response is sent to Facebook.

auth.js : Receives the VERIFY_TOKEN you gave Facebook as well as a challenge parameter. The function checks that the VERIFY_TOKEN matches the one in env.json and then sends the challenge back as the response.

And that’s it! Thanks for reading! Hopefully this guide has shown you how easy it is to work with StdLib. Building a Messenger bot is just one of the many ways you can get started with StdLib. If you have a neat idea you’d like to share, reach out to me directly by e-mail: steven@stdlib.com, or follow me and the StdLib team on Twitter.

As always, we look forward to hearing from you and happy building!