Say Hello to Your Own Amazon Lex Chatbot

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

Sam Williams
Dev Tutorials

--

This tutorial will guide you through the whole process of making a chatbot using Amazon Lex. We will start by setting up some simple responses and then create some more complex ones.

Setup

As the chatbot is built on Amazon Web Services, you need to create an account. If you don’t have one, you can set one up here and click “Create a Free Account”.

Once you have your account set up, we can set up the chatbot. On the AWS Management Console, search for “Lex” or click on the Services drop down menu on the top left of the page and you’ll find it under the “Machine Learning” category.

The AWS Services List

Once on the Lex page, click “Get Started” to get onto the bot setup page. You’ll be presented with the option to use one of three sample bots or create a Custom bot. We’ll be creating a custom bot so select that option.

Bot creation options

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 minutes 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 below. I’ll explain what everything means as we go along.

Intents

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

Giving your Bot a Name

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 click on the + next to Intents on the menu at the left of the page. 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.

Sample Utterances

Utterances are the phrases that you want this intent to reply to. Add the following phrases as sample utterances:

  • “what is your name”
  • “what are you called”
  • “what do you like to be called”
  • “what should I call you”.

The smart bit about 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 theAdd Message” button in the response box. This creates a new message box for us to fill in.

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

Add the following as responses:

  • “My name is SamBot.”
  • “You can call me SamBot”

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 chatbot working we first need to build it. This allows Lex to take your sample utterances and put them all together. Click the “Build” button on the top right of the page (and click on “Build” once again if a pop-up is displayed). It can take a few minutes to finish building the bot 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 your newly created bot and test it out. Try asking your new bot it’s name.

Adding More Intents

Being able to tell you its name is cool, but for it to be useful, we’d want it to do more than that. You can add intents for a 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 of getting 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 that contain the following:

  • property name
  • slot type
  • prompt.

There are a few different ways to create new slots and I’ll discuss a few methods below.

In the Slots section, add the following information to create a new slot.

  • Name: “Name”
  • Slot type: “AMAZON.GB_FIRST_NAME”
  • Prompt: “Hi there, what’s your name?”

The prompt is sent if the Required checkbox 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 are the filler words. You define the variable with curly brackets around the variable.

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.

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

Other way to populate 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 says “Hi there, it’s Claire” and the bot asks them “what should I call you”?

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.

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.

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:

Thanks for following this tutorial. I hope you enjoyed it. If you did then please hit that clap button and follow me for more bot tutorials and JavaScript content.

--

--

I'm a software developer currently building Chat Bots for E-Commerce companies. Outside of coding I love to go Rock Climbing and Traveling.