Create and deploy a Chatbot using Amazon Lex, AWS Amplify, Express

In this tutorial, we want to create a chatbot that can work as a microservice for other applications use, through an API interface. This tutorial can also be applied to those who want to integrate their Amazon Lex chatbot to their React, Angular, React Native app (javascript) and deploy the app. The chatbot is created using Amazon Lex. Then AWS Amplify is used to integrate the chatbot to an Express App which creates the REST API that would be deployed using Serverless.

Create a chatbot with Amazon Lex by AWS

Video Tutorial from Edureka!:

Hands-on step by step tutorial from the Amazon Lex Developer Guide:

Create a basic Express App for the integration of the chatbot later

Use Amazon Amplify and Amazon Cognito to integrate chatbot into Express App

Once AWS Amplify is initialised in the project folder, go to the aws-exports.js file and remove all the code before pasting the following code in. Replace the “your-bot-name” with your Amazon Lex chatbot’s name.

awsconfig = {   
Auth: {
identityPoolId: 'us-east-1:xxx-xxx-xxx-xxx-xxx',
region: 'us-east-1'
},
Interactions: {
bots: {
"your-bot-name": {
"name": "your-bot-name",
"alias": "$LATEST",
"region": "us-east-1",
},
}
}
};
module.exports = awsconfig;

For the Auth, we need to set up an Amazon Cognito identity pool. To set up the Amazon Cognito, just follow this tutorial page. Take note of the identity pool ID and use it to replace ‘us-east-1:xxx-xxx-xxx-xxx-xxx’ in the code snippet above.

Once all that is done we just need to integrate the Chatbot to the Express App. This would require us to edit the app.js file. A code snippet of how this can be done is shown below. You can customise the routes and the return values or even add more chatbots to this project. (app.js and aws-exports.js are in the same directory). If you are integrating your Chatbot to an existing app, you can use this method to create an API which can be called by your app.

const Amplify = require('aws-amplify');
const { Interactions } = require('aws-amplify');
const awsconfig = require('./aws-exports')
Amplify.default.configure(awsconfig);const serverless = require('serverless-http');
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/info', (req, res) => {
res.send({ application: 'Book Trip Bot ', version: '1' });
});
app.post('/api/intent', async (req, res) => {
const userInput = req.body.userInput;
const response = await Interactions.send("Your-bot-name", userInput);
res.send(response);
});
app.listen(port, () => console.log(`Listening on: port ${port}`));
//module.exports.handler = serverless(app);

To test if your API works, use insomnia/postman or similar web apps to send HTTP API requests to your running Express App.

Now all that is left is to deploy it! You can simply continue the tutorial above that included deployment of Express App to AWS Lambda. Do take note that to input your credentials do the following:

serverless config credentials -p aws -k ACCESS_KEY_ID -s ACCESS_SECRET_KEY

You may also have to edit some parameters in the .yml file if any error occurs. For the runtime, I had to use nodejs14.x. I hope this was helpful to you and I guess it was rather simple now that it’s done. Personally, I was slightly confused at the beginning on how to do this and I tried using ‘amplify add api’, ‘amplify add interactions’ to try to integrate the Amazon Lex chatbot into the Express App but then I found this way to be much simpler and there are way fewer files created. Credits to all the different tutorials, documentations I linked in this post.

Aspiring Software Developer who is currently in the last year of my Computer Science major at the National University of Singapore