Code samples related to Building Better Bots published on the AI Blog
CoffeeBot is a transactional chat bot that can help one order a mocha (relies on AWS Mobile Hub and Android).
Consider this conversation:
User: May I have a mocha?
CoffeeBot: What size? small, medium, large?
User: small
CoffeeBot: Would you like that iced or hot?
User: hot
CoffeeBot: You'd like me to order a small mocha. Is that right?
User: Make it a large mocha
CoffeeBot: You'd like me to order a large mocha. Is that right?
User: yeah
CoffeeBot: Great! Your mocha will be available for pickup soon. Thanks for using CoffeeBot!
Let's build this voice bot, an Android App that talks to you using Amazon Polly and Amazon Lex. You'll need the following in addition to your AWS account:
- Android development environment (download)
- To test voice (you can use the simulator for text)
- An Android device
- A USB cable for USB debugging (more info for Amazon Fire tablets)
First, we'll create the Lex bot. Then, we'll add some Lambda Functions to bring it to life. Finally, we'll put it all together with Mobile Hub and the Lex Android SDK.
- From the Amazon Lex console, create a Custom bot with these settings (you can see these in the "Settings" tab later)
- Bot name:
CoffeeBot
- Output voice:
Salli
- Session timeout:
10 min
- IAM role: (accept the default)
AWSServiceRoleForLexBots
- Bot name:
- Review the Error handling settings
- Prompts: (one prompt)
Sorry, can you please repeat that?
- Maximum number of retries:
3
- Hang-up phrase: (one phrase)
Sorry, I could not understand. Goodbye.
- Prompts: (one prompt)
Add the following Slot types (each value should be a separate entry) so they show up on the left (remember to "Save slot type" as you go along).
Slot type name | Description | Values |
---|---|---|
cafeBeverageType |
Slot types are shared at the account level so text would help other developers determine if they can reuse this Slot type. | coffee ; cappuccino ; latte ; mocha ; chai ; espresso ; smoothie |
cafeBeverageSize |
kids ; small ; medium ; large ; extra large ; six ounce ; eight ounce ; twelve ounce ; sixteen ounce ; twenty ounce |
|
cafeCreamerType |
two percent ; skim milk ; soy ; almond ; whole ; skim ; half and half |
|
cafeBeverageTemp |
kids ; hot ; iced |
|
cafeBeverageStrength |
single ; double ; triple ; quad ; quadruple |
|
cafeBeverageExtras |
half sweet ; semi sweet |
From the left, add a new Intent called cafeOrderBeverageIntent
with the following settings and click "Save" to save the Intent
- Options (leave options unchecked)
- Fulfillment: choose "Return parameters to client" for now
- Confirmation prompt:
You'd like me to order a {BeverageSize} {BeverageType}. Is that right?
to confirm andOkay. Nothing to order this time. See you next time!
to cancel. - Sample Utterances: add these to the list of sample utterances so the bot recognizes similar phrases
I would like a {BeverageSize} {BeverageType}
Can I get a {BeverageType}
May I have a {BeverageSize} {Creamer} {BeverageStrength} {BeverageType}
Can I get a {BeverageSize} {BeverageTemp} {Creamer} {BeverageStrength} {BeverageType}
Let me get a {BeverageSize} {Creamer} {BeverageType}
- Slots
Required | Name | Slot type | Prompt |
---|---|---|---|
Yes |
BeverageType |
cafeBeverageType |
What kind of beverage would you like? For example, mocha, chai, etc. |
Yes |
BeverageSize |
cafeBeverageSize |
What size? small, medium, large? |
Creamer |
cafeCreamerType |
What kind of milk or creamer? |
|
BeverageTemp |
cafeBeverageTemp |
Would you like that iced or hot? |
|
BeverageStrength |
cafeBeverageStrength |
Single or double? |
|
BeverageExtras |
cafeBeverageExtras |
extras? |
Build the app and test some of the Utterances in the Test Bot dialog at the bottom right of the Lex Console. For example, if you say May I have a chai?
, does Lex correctly map chai
to the BeverageType
slot?
- Create the
cafeOrderCoffee
function by savingcafeOrderCoffee_lambda.js
as a Node.js 4.3 function- You can get the function source here
- (No need to set up a trigger; you can accept default values for most of the configuration)
- Choose an IAM role that includes the
AWSLambdaBasicExecutionRole
Managed Policy. If no such role exists, you can create a new IAM Role using one of these approaches:- Choose "Create new role from template(s)", provide a role name, and choose
Basic Lambda permissions
from the "Policy templates" dropdown - Choose "Create a Custom role", which should open up a new tab where an IAM role is shown; review the policy document and click "Allow"
- Choose "Create new role from template(s)", provide a role name, and choose
- Configure the Test event and test to confirm the function works as expected (see
cafeOrderCoffee_test.json
)- you can get the event source here
- You'll notice that the function checks the bot name it receives (
if (event.bot.name !== 'CoffeeBot')
); remember to change this value in the function and in the test event to match the name you used for your bot
- From the Lex Console, select the
CoffeeBot
bot and chooseLatest
from the version drop down to make changes - Modify the
cafeOrderBeverageIntent
Intent to associate it with the newcafeOrderCoffee
Lambda function (select "AWS Lambda function" in the "Fulfillment" area); remember to click "Save"- The Lambda function overrides the "Goodbye message" (so we don't configure it here)
- Build the app
- Test using Lex Console; do you see any responses when you ask
May I have a mocha?
- From the Mobile Hub console, create a new project called
CoffeeBot
. - Add the "Conversational Bots" feature to the project. When prompted, import
CoffeeBot
. Mobile Hub takes care of a number of important details behind the scenes. A new Amazon Cognito Federated Identity Pool is created for this new app along with roles so that the users can interact with Lex (using voice and text). - Source code for the new app is immediately available for download.
- Follow the instructions in the
READ_ME/index.html
file to setup, compile, and run the app.