Say Hello to Your Own Amazon Lex Chat Bot
Tags: chatbots, javascript, amazonlex, ai
Published: 2018-03-05 06:28:52

---
title: "Say Hello to Your Own Amazon Lex Chat Bot"
published: true
tags: chatbots,javascript,amazon-lex,ai
canonical_url: https://medium.freecodecamp.org/say-hello-to-your-own-amazon-lex-chat-bot-9f22e7a0f9b0
---

#### Build your own chat bot using Amazon Lex and teach it to say whatever you want.

This tutorial will guide you through the whole process of making a chat bot. We will start by setting up some simple responses and then use AWS Lambda for more complex responses.

### Set Up

As all of this is built on Amazon Web Services you need to have an account. If you don’t have one, you can set one up [here](https://aws.amazon.com/) and clicking **Create an AWS Account**.

Once you have your account set up, we can set up the bot. Search for _Lex_ or find it in the services drop down.

![](https://cdn-images-1.medium.com/max/1024/1*GrZ1JfkXxInFf10AKXkSvA.png)<figcaption>The AWS Services List</figcaption>

Once on the Lex page, click **_Get Started_** to get onto the bot setup page. Here is the option to use one of three sample bots or create a _Custom Bot._ We’ll be creating a completely custom bot so select that option.

You now get to name your bot and select an output voice. The voice will be used if you ever want to make a voice chat version of your bot. We will also set the session timeout to 5 min as default and select **no** for the COPPA (unless you intend to include non PG replies!).

With that all set up, you can click **Create**. You’ll be taken to a new dashboard like this and I’ll explain what everything means as we go along.

![](https://cdn-images-1.medium.com/max/915/1*bytzXWvlXP6iZPzryN82Lg.png)

### Intents

A chat bot is a set of responses that it gives to a certain message. These are stored in _Intents_ which are like talking points.

#### Naming your Bot

We’ll keep our first intent simple, if someone asks what our bot is called we will reply with a name.

Create a new intent by clicking **_Create Intent_** or clicking the **+** next to Intents on the left. A menu will pop up and we want to choose **_Create intent_** again. We have to name the intent so call it something like **_WhatAreYouCalled._**

This puts us through to the Intent screen. For now the important sections are _Sample utterances_ and _Response._ The rest of the settings we will cover later.

![](https://cdn-images-1.medium.com/max/903/1*WZ1wjkHeDo-5ne6p8U5jXA.png)

#### Sample Utterances

Utterances are the phrases that you want this intent to reply to. For us those are phrases like “What is your name” and “What are you called”. Add each of the phrases to the sample utterances; I also added “what do you like to be called” and “what should I call you”.

![](https://cdn-images-1.medium.com/max/469/1*Wg-tdra9fS_Xu1OZ6Rr4rA.png)

The smart bit of Amazon Lex is that it uses Natural Language Understanding (NLU) to work out what the user is trying to say. If they say “What’s your name” instead of “What is your name”, Lex will still match the phrases. Pretty smart!

#### Response

No we need to reply to this message. Click the **_Add Message_** button in the response box. This creates a new message box for us to fill in.

![](https://cdn-images-1.medium.com/max/635/1*C6BMDFckK9qbV3E5Yb-lLA.png)

In here you can type whatever you want to respond. You can enter multiple answers so the user can get a varied and more natural responses.

Now click **_Save Intent_** at the bottom of the page and you’ve created your first intent.

### Building and Testing the Bot

To get your new chat bot working we first need to build it. This allows Lex to take your sample utterances and put it all together. Click the _Build_ button in the top right of the page, it can take a minute or so to finish building so be patient.

When it’s finished you get a new area on the right called _Test Bot (latest)_. This is where you can try chatting to you bot and testing it out. Try asking your new bot it’s name.

![](https://cdn-images-1.medium.com/max/398/1*hyhj5biPiXiBBXuKqrhwiQ.png)

### Adding More Intents

Being able to tell you its name is cool, but we want it to do more than that. You can add intents for load of things, just repeat the process you’ve done with different utterances and responses.

What does your bot say if the user says Hi or Hello. We’re going to expand on this later so make sure you get it working!

You can also have a go at making your bot answer these questions:   
Who made you?   
What is your favourite colour?   
What’s it like being a robot?

What other questions can you think to get your bot to answer?

#### Quick Tip

When you are creating your utterances, type them in all lower case with no punctuation. The NLU program gets rid of the punctuation and capitals so using them can break the system.

### Improving the Hello Intent

Having your bot just reply with _Hi_ or _Hello_ is pretty cool but it isn’t much of a conversation. We’re going to change that.

#### New Hello Intent

This is the design for the new intent

Customer — Hi  
Bot — Hi there, what’s your name?  
Customer — my name is Dave  
Bot — Hi Dave, it’s nice to meet you. Is there anything I can help you with today?

#### Slots

In Lex, variables are stored in _Slots._ These contain a property name, slot type and a prompt.

There are a few different ways to create new slots and I’ll talk through them.

The first method is the most expected way. In the _Slots_ section, type the a name for your variable, choose a slot type and write a prompt. In this example I’m calling my variable _Name_ choosing _AMAZON.GB\_FIRST\_NAME_ and saying _Hi there, what’s your name?_ as the prompt.

![](https://cdn-images-1.medium.com/max/1024/1*3K9mmmFMGNqkqc4kqRS26g.png)

The prompt is sent if the _Required_ box is ticked and the intent doesn’t know the value for the variable. Clicking the settings cog opens up a new menu where you can set multiple prompt messages and list some replies.

The user can reply with just the answer to the question but what if they say something like _My name is David_? Lex needs to know which bit of the reply is the the variable and what is just filler words. You define the variable with curly brackets around the variable.

![](https://cdn-images-1.medium.com/max/587/1*j9Ig6l7QiE9aDfParU0hkg.png)

The last bit is to change the final response. You can include any of the variables in the final message using the same {variableName} syntax as in the prompt utterances.

![](https://cdn-images-1.medium.com/max/564/1*LO3KHaJqaaVOyHWwMbN1kQ.png)

With all of this updated it’s time to build again and try it out!

![](https://cdn-images-1.medium.com/max/496/1*6YXcz3DooqcYzTXeosnsNg.gif)

### Other way to populate the Slots

So far, the user says a phrase which gets the bot to reply with a prompt for the slot value. This is great but there’s another way. What if a user say “_Hi there, it’s Claire”_?

![](https://cdn-images-1.medium.com/max/487/1*bMDo2ioK0MuGMqycsM6WQw.png)

This is just wrong. They’ve told you their name and then you ask them for their name. Luckily we can sort this out.

We can add a new utterance that includes the slot name. This is the same as writing a prompt utterance, including {SlotName} in the utterance.

![](https://cdn-images-1.medium.com/max/650/1*GcooD5h4Qv-N5Yo3lUgktA.png)

This now fills the slot with their name from the first message. The slot is filled so the prompt never has to fire and the final message is sent.

![](https://cdn-images-1.medium.com/max/597/1*G4FXISqnYwEpSentmhc-dg.png)

### Writing Your Own Intents

With what you’ve learnt so far you can create intents that create a very unique conversation with your users. You can have intents with multiple slots that create very custom messages.

Try creating an intent that works like this:

![](https://cdn-images-1.medium.com/max/398/1*j2Lb-EfPzLDC2waiHtDFbg.gif)

_Thanks for following this tutorial, I hope you enjoyed it. If you did then please react and follow me for more bot tutorials and JavaScript content._

_You can also check out some of my most popular articles [here](https://medium.com/free-code-camp/@samwsoftware)!_

* * *