Hello World Chat-bot using Lambda

This is a step-by-step guide for creating a simple serverless chat-bot using AWS Lambda. You’ll learn how to create a chat bot for Facebook Messenger, Skype, Telegram and Slack – all in less than 10 lines of code. We’ll use the Claudia Bot Builder to set-up the chat bot.

Claudia Bot Builder helps JavaScript developers create chat-bots for Facebook, Telegram, Skype and Slack easily, and deploy them to AWS Lambda and API Gateway in minutes. The key idea behind the project is to remove all the boilerplate code and common infrastructure tasks, so you can focus on writing the really important part of the bot – your business workflows. Everything else is handled by the Bot Builder.

Prerequisites

If you’re completely new to using Claudia.js, check out Installing and configuring Claudia.js for information on how to set up access credentials.

Creating a simple text bot

First, create an empty folder, and a new NPM project inside it. Just make sure to give it a descriptive name:

npm init

Then, add the claudia-bot-builder library as a project dependency:

npm install claudia-bot-builder -S

For this particular bot, we’ll generate some dynamic content using the huh excuse generator. So add that as a project dependency as well:

npm install huh -S

Claudia Bot Builder simplifies most of the messaging workflows, and converts incoming messages from all the supported platforms into a common format, so you can handle it easily. It also automatically packages text responses into the right format for the requesting bot engine, so you don’t have to worry about formatting results for simple responses. That makes it easy to create simple text responses.

Create a file called bot.js and paste the following content:

var botBuilder = require('claudia-bot-builder'),
    excuse = require('huh');

module.exports = botBuilder(function (request) {
  return 'Thanks for sending ' + request.text  + 
      '. Your message is very important to us, but ' + 
      excuse.get();
});

That’s pretty much it.

Deploying the bot

You can now deploy the bot to AWS:

claudia create --region us-east-1 --api-module bot

Claudia will automatically create the right web hooks for all the supported bot platforms. After that, you just need to to connect your bot to the bot platforms.

Connecting with Facebook Messenger

Creating a Slack Slash command for your channel

Creating a Slack Slash Command Application

Viber bot configuration

Telegram bot configuration

Skype bot configuration

Twilio bot configuration

Kik bot configuration

GroupMe bot configuration

Try it out live

You can see this bot in action and play with it live from the Github Claudia Examples repository.

More complex workflows

The example bot just responds with silly excuses, so for homework, do something more interesting with it. The request object passed into the message handling function contains the entire message in the text field, but it also has some other pieces of data for more complex work. The sender field will identify the user sending the message, so you can create threads of continuity and sessions. The type field will contain the identifier of the bot end-point that received the message (for example skype or facebook) so you can respond differently to different bot systems. The originalRequest field will contain the entire unparsed original message, so you can handle platform-specific requests and go beyond simple text. For example, check out the Fact Bot, which looks up facts about topics on WikiData and creates Facebook Messenger menus.

Although it’s enough to just return a string value for simple cases, and the Bot Builder will package it correctly for individual bot engines, you can just return a more complex object and get platform-specific features – for example Facebook buttons. In that case, make sure to use the type field of the request to decide on additional features.

For asynchronous workflows, just send back a Promise object, and resolve it with the response later. The convention is the same: if the promise gets resolved with a string, the Claudia Bot Builder will automatically package it into the correct template based on the bot endpoint that received a message. Reply with an object instead of a string, and the Bot Builder will not do any specific parsing, letting you take advantage of more advanced bot features for individual platforms. Remember to configure your Lambda function for longer execution if you plan to use asynchronous replies, AWS by default limits this to 3 seconds.

More information

See other tutorials and guides on this site for ideas on how to set-up the development environment and fine-tune the packaging.

Check out the API Documentation for more information on the request object and how to create more complex responses, such as receipts for Facebook or interactive buttons for Slack.

Did you like this tutorial? Get notified when we publish the next one.

Once a month, high value mailing list, no ads or spam. (Check out the past issues)