Serverless Facebook Quotebot

100% Serverless Facebook messenger chatbot which will respond with inspiring quotes

Anirudhi Thanvi
4 min readApr 18, 2022

We never realise #serverless was so absurdly quick and easy to use. Lets see how can we create a 100% Serverless #Facebook Messenger (Chatbot).

:Anirudhi Thanvi

Serverless is a cloud-native development model that allows developers to build and run applications without having to manage servers.

Servers are still there in serverless, but they are separated from the app development process. The normal job of establishing, maintaining, and scaling the server infrastructure is handled by a cloud provider. For deployment, developers can simply package their code in containers.

Serverless programmes respond to demand and autonomously scale up and down as needed once they’ve been launched. Public cloud providers’ serverless products are often metered on-demand using an event-driven execution approach. As a result, a serverless function does not cost anything when it is idle.

Building a Facebook Messenger Chatbot with Serverless

Chatbots are a buzzword these days, but what exactly are they, why are they important, and how can you develop one? You’ll find answers to all of your queries, as well as instructions on how to create your own Serverless Facebook Messenger Chatbot, in this post.

What are Chatbots

A chatbot, at its most basic level, is a computer programme that simulates and processes human dialogue (written or spoken), allowing humans to communicate with digital devices as if they were speaking with a real person. Chatbots can be as simple as one-line programmes that respond to a simple query, or as sophisticated as digital assistants that learn and adapt as they gather and process data to give increasing levels of personalisation.

Developing a Serverless Chatbot

Here’s how to build a Facebook Messenger chatbot with the Serverless Framework. Note: We’ll use Amazon Web Services (AWS) as our cloud provider of choice. We’ll assume that the installation and setup of Serverless and your AWS credentials are done.

We’ll build a chatbot called Quotebot that will send us a quote if we send it a message.

Creating our Serverless service

To begin, establish a new Serverless service that contains all of the code and resources required for our chatbot. To build a new Serverless service based on the aws-nodejs template in the quotebot directory, run the following command:

serverless create — template aws-nodejs — path quotebot

Creating a new Facebook application

The next step is to make a new Facebook app. To create Facebook applications and engage with the Facebook Open Graph and their ecosystem, you must be a Facebook developer.

  1. If you haven’t already, go to developers.facebook.com and establish an account or sign in.
  2. Go to developers.facebook.com/apps and select “Add a New App” from the drop-down menu.
  3. Skip the wizard that appears and go to the bottom of the page to “Basic Setup.
  4. Set the Contact E-Mail to your E-Mail address and the Display Name to Quotebot.
  5. Click “Create App ID” after selecting a related category (I choose “Communication”).
  6. Your Quotebot’s Facebook app will be developed, and you’ll be taken to the overview page. You must now configure the Facebook product you wish to utilise.
  7. Click on “Get Started” in the “Messenger” section. After that click “Get Started” again and you should see the page where you can configure your messenger integration.

Creating a webhook

A webhook is how Facebook Messenger communicates with you. Your webhook is an API endpoint to which the messenger will make HTTP queries. Open the serverless.yml file and make some minor changes. The hello function definition will be renamed to webhook. The handler attribute will also be renamed to point to the handler.js file and the webhook function. Then, under the events definition, you’ll add two HTTP events. With the help of the webhook path, they’re both accessible. One uses the GET method, while the other uses the POST method.

  1. You must now edit the handler.js file. You’ll create a method for responding to GET requests submitted to our webhook.
  2. Check to see if the request submitted to your webhook was a GET request. If this is the case, double-check that the token Facebook issued you corresponds to the one you selected.
  3. Note: You should replace “STRONGTOKEN” with a really strong token if you deploy a chatbot in the wild. Run command “serverless deploy” to deploy your Serverless service.
  4. In your terminal, you should see the API endpoints. Copy and paste the GET endpoint’s link.

Verifying your webhook from Facebook

Now it’s time to make sure you’re the rightful owner of your Quotebot webhook. Select the “Messenger” product on the left-hand side of the Facebook Messenger app you previously established.

  1. Underneath it, click “Settings."
  2. In the “Webhooks” section, click “Setup Webhooks.
  3. Copy the GET endpoint URL and paste it into the Callback URL field, along with your token (e.g. STRONGTOKEN).
  4. For the time being, just pick all of the Subscription Fields and then click “Verify and Save.
  5. Facebook will now send a GET request to your webhook and verify that you’re the owner of this Chatbot.
  6. You should see a green “Completed” message in the “Webhooks” section.

Conclusion

You’ve just finished your first Facebook Chatbot, which will provide you with fantastic quotations whenever you need them! Your chatbot can scale indefinitely without you having to do anything, and you’ll only be charged when people use it. This is only the start. Chatbots are becoming increasingly popular, and the Serverless Framework is an excellent choice for building an infinitely scalable Chatbot with low operational costs.

Thank you for giving your valuable time and reading it to the end!!!
I hope you find it helpful and like it.
Just give a clap if you found something interesting here… #AWSwithVimalDaga

--

--