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
- Node.js 6
- NPM
- An AWS account with the permissions to create Lambda functions, API Gateway end-points and IAM roles.
- Claudia.js 1.4.0 or later
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:
Then, add the claudia-bot-builder
library as a project dependency:
For this particular bot, we’ll generate some dynamic content using the huh
excuse generator. So add that as a project dependency as well:
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:
That’s pretty much it.
Deploying the bot
You can now deploy the bot to AWS:
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
- Create a new bot page in Facebook and a messenger app, as explained in the Facebook Messenger Getting Started Guide.
- Use
claudia update --configure-fb-bot
to get the Webhook URL and the verification token, which you can copy to your Facebook Messenger configuration. - Generate the page access token from Facebook, and copy that back to Claudia when asked.
- If you want to make the bot public, submit the app for App Review
Creating a Slack Slash command for your channel
- Follow the instructions from Slack API Docs to creeate a slash command for your team.
- Use
claudia update --configure-slack-slash-command
to configure the token.
Creating a Slack Slash Command Application
- Follow the instructions from Slack API Docs to set up an app with a slash command.
- Use
claudia update --configure-slack-slash-app
to configure the access tokens. - Create a Slack Button so people can add your app to their channels.
Viber bot configuration
- Create a Viber public account, get the token
- Use
claudia update --configure-viber-bot
to configure your bot with it’s Viber access token
Telegram bot configuration
- For getting a Telegram bot access token - use their BotFather bot for creating bots.
- Use
claudia update --configure-telegram-bot
to configure the access token in your bot.
Skype bot configuration
- Create a Microsoft App and get its App ID and App password at Microsoft Apps
- Use
claudia update --configure-skype-bot
to configure the access details in your bot - Create the Skype bot using the webhook URL printed by the installer at the Skype Bots Page
Twilio bot configuration
- Create a Twilio account, buy a Twilio virtual number and create a Messaging Service with your virtual number at Twilio Dashboard Messaging Service
- Get your Twilio SID, Account Number from your Twilio Dashboard - Show API Credentials
- Get the Virtual Number you connected to the Messaging Service
- Use
claudia update --configure-twilio-sms-bot
to configure the access details in your bot - Update your Twilio messaging service webhook URL by the previous command and choose
HTTP POST
from the dropdown;
Kik bot configuration
- Create a Kik account, scan the Kik code with your Kik app
- Setup your bot with Botsworth (Kik Bot) and get your bot’s username
- Get your Api Key at Dev Kik
- Use
claudia update --configure-kik-bot
to configure your bot with it’s username and Api Key
GroupMe bot configuration
- Register for a GroupMe account and their developer account
- Create your group within GroupMe
- Create your bot on the GroupMe Developer site (without the CallbackURL), assign it to the created Group and copy its bot id
- Invoke
claudia create
orclaudia update
with the--configure-groupme-bot
flag - Enter the copied bot id, when Claudia requests
- Claudia Bot Builder will respond with your AWS Lambda incoming webhook address, copy that webhook URL
- Edit your bot and paste the copied webhook URL into Callback URL field for your bot
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.