DEV Community

Cover image for Build Chatbot with Amazon Lex
Debabrata Halder
Debabrata Halder

Posted on

Build Chatbot with Amazon Lex

Why talk to humans when you can build a bot that does it for you?

This quote summarizes my experience of diving into Amazon Lex, AWS's superpower for building chatbots. I began on a journey to create my own bot: BankerBot. What a rollercoaster ride this was, with its learning curves, errors, and eureka moments! More on that below.

How It All Started

I've always wanted to learn AWS, and when one day, I found a website called Nextwork. It had a tutorial wherein they explained how to build chatbots with Amazon Lex. Curiosity gripped me, and I plunged into action.

Lex looked like a youthful, fun playground for chatbot enthusiasts. It demanded you to reduce the complexity of a conversational interface into easy clicks and configuration steps, a task in itself for a newcomer in chatbots! Armed with the tutorial, I hoped to develop a bot capable of greeting users, fetching bank balance, and following up with subsequent queries.

Image description

What is Amazon Lex?

We can think of Amazon Lex as that friend you have who just makes your life easier. It helps in creating chatbots that would just chat with users via text or by talking with them via voice. You could think of it as a bridge between you and artificial intelligence. It is pretty intuitive, integrates seamlessly with other AWS services, and allows automating the mundane conversations. This is great for applications like banking, customer service, or even ordering pizza.

I initially planned to create a bot where the back-and-forth with the customer on banking queries would be minimized. Pretty simple, right? Now what "Spoiler" it wasn't! At least not right at the beginning.

Step 1: Set-up of BankerBot

To start with, I created a bot named BankerBot on the Amazon Lex. Here is what I did:

  1. Created Intents: Intents are like aims of a bot. I created WelcomeIntent, to greet users, CheckBalance, to fetch account balance, and FollowupCheckBalance, to respond to a follow-up query.
  2. Added Slots: Slots are allotted to take input from the user. For example, I added two slots - accountType (Savings, Checking, Credit) and dateOfBirth - to personalize the response.
  3. Designed Responses: I put in the dynamic responses, using placeholders. For instance: ""Your balance in the {accountType} account is $XYZ."

The entire setup process was quite smooth, thanks to Lex's user-friendly interface. At least so I thought.

Image description

The Mistake That Taught Me a Lesson πŸ€¦β€β™‚οΈβŒ

Everything was going just fine until I came across a very frustrating roadblock. The bot had been working like a charm in English (India) (en_IN) for basic tasks-greet the user, take input from the user, and check the balance. When the FollowupCheckBalance intent was designed, it got stuck.

Picture yourself in this situation:

  1. To know your balance in your savings bank account, you are required to provide your date of birth.
  2. You next wanted to check the balance in your credit account.
  3. The bot asks for your date of birth again. Very frustrating, isn't it?

My intention was to have the user provide the date of birth once (through context tags), and continue using the same for further queries. But the fatal flaw was that context tags were unsupported in en_IN.

Initially, I thought it was something that I am missing. I went through every little setting and Googled like a maniac until I finally discovered that in order to use context tags, the bot must be created in English (US) (en_US). Talk about a facepalm moment but a valuable learning experience, nonetheless. I rebuilt the bot using en_US and, voila, the context feature just popped up like magic.

Step 2: Building a Smarter Bot with AWS Lambda

Setup AWS Lambda to process data, fetch information or provide randomly generated responses. With Lambda, it's almost like the brain of your bot-I used it to simulate reporting on current bank balances.

What I did was as follows:

  1. Wrote a Lambda function that generates a random number that acts as a simulated balance.
  2. The function was connected to the bot through code hooks.
  3. Test it by saying, β€œWhat’s my balance in the savings account?”

The bot responded, β€œYour balance in savings is $423.” Success! Truly the thrill of watching a baby take their first step.

Image description

Step 3: Adding Multiple Slots

In the upcoming stage, I established a TransferFunds intent for BankerBot. Three slots will help gather information:

  • Source Account Type
  • Target Account Type
  • Amount to Transfer

I used the same slot type accountType two times with different names sourceAccountType and targetAccountType to ensure clarity in the understanding of which account to be used. I inserted confirmations like, "Are you sure you want to transfer β‚Ή500 from your Savings account to your Credit account?"

Step 4: Automating Setup with CloudFormation πŸ“„

After building my bot several times (bless you, en_IN!), I chanced upon AWS CloudFormation. It's sort of a magic wand to automate setups. Instead of going through the old-fashioned way of creating the bot, intents, and Lambda functions by hand, CloudFormation allowed you to define everything inside a YAML file and deploy it all as one stack.

Using CloudFormation, I set up the entire infrastructure for BankerBot within minutes. This saved me hours of manual configuration and spared me further human error.

Challenges and Fixes

It was not one joyous flying leap completing the building of BankerBot. Part of the unfortunate situations I found with the bot and how I solved them is listed below.

  1. Error: Missing context tag in en_IN ❌
    Fix: The bot was rebuilt in en_US for context support πŸ”„

  2. Error: Lambda function is not returning the responses ⚑
    Fix: Adjusted the IAM permissions for the function to be able to speak to Lex πŸ”

  3. Error: Slot values not occurring correctly πŸ›‘
    Fix: Utilized detailed utterances and a validation prompt to guide user input πŸ—£οΈ

Key Takeaways

  1. Start Simple: 🌊❌ Begin with the basicsβ€”no need to boil the ocean! Add complexity gradually.
  2. Read the Docs: πŸ“œπŸ” Small details (like language settings) can make or break your bot.
  3. Embrace Errors: βŒβž‘οΈπŸ’‘ Treat errors as stepping stones to learning.
  4. Use CloudFormation: β˜οΈπŸ€– Automation = saved time + happier you! πŸŽ‰

Image description

Conclusions

It was fine learning to build BankerBot; at first, it was not easy to handle, but it became quite fun after I learned it. With Amazon Lex and lots of trial and error, I have my bot that is interacting just like a pro. If I can do it, you can, too.

What are you waiting for? πŸš€ Dive into Lex, embrace the mistakes, and craft something amazing. πŸ€–βœ¨ Have a blast building your dream bots! πŸŽ‰πŸ’»

Top comments (0)