Say Hello to Your Own Amazon Lex Chat Bot

Sam Williams - Mar 5 '18 - - Dev Community

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 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.

The AWS Services List

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.

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.

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”.

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.

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.

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.

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.

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 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”?

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 react and follow me for more bot tutorials and JavaScript content.

You can also check out some of my most popular articles here!


. . . . . . . . . . . . . . . . . . . . . . . .