Easy Steps to Develop a Bankbot with Amazon Lex

Let’s build a banking chatbot that greets users, checks account balances, and facilitates fund transfers all while ensuring a secure and interactive experience! In this article, we’ll walk through the process of creating a fully functional bankbot using Amazon Lex and AWS Lambda. Our bot will not only handle essential banking tasks but will also prioritize user security by requesting the date of birth for identity verification. By leveraging AWS services, we’ll empower our chatbot to manage banking needs effectively and securely. It's a lengthy process, but trust me, it's easy if you follow along. Join me as we embark on this journey to create a smarter, safer banking assistant!

Key components

  • Amazon Lex: Amazon Lex is an AWS AI service that enables you to build conversational interfaces using voice and text. It uses deep learning to understand natural language and manage dialogues, allowing you to create chatbots and virtual assistants that interact with users in real-time. It integrates with other AWS services for scalability and intelligence.

  • Intents: In Amazon Lex, intents represent the actions or tasks users want to accomplish through the chatbot, like checking balances or transferring funds. They include utterances (phrases users say to trigger an intent), slots (variables to capture user input), and fulfillment (the action taken when the intent is triggered).

  • Slots: In Amazon Lex, a slot is a variable used to capture specific information from the user, such as dates, locations, or other details required to fulfill an intent. Slots help the chatbot gather the necessary data to complete the user's request.

  • Aliases: They are used to create different versions of a bot for various environments, such as development, testing, or production. They allow you to manage and deploy multiple versions of a bot while pointing users to the appropriate one without altering the bot’s core configuration.

  • AWS Lambda: The serverless compute service that processes the logic behind the intents, executing code in response to user interactions and retrieving information as needed.

Prerequisites

Before diving into the development of our banking chatbot, ensure you have the following prerequisites in place:

  1. AWS Account: You need an Amazon Web Services (AWS) account to use both S3 and QuickSight. Signing up is free, and the services come with free tiers.

  2. Python Programming Skills: Understanding of Python, as we will be writing Lambda functions in this language.

Step 1: Creating Your AWS Lex BankBot

  1. Navigate to AWS Lex.

  2. Ensures you're using the Lex V2 console, if not click on the "Switch to the new Lex V2 console" link in the left-hand menu.

  3. Click on the Create Bot button.

  4. Choose the Blank Bot option.

  5. Enter a name for your bot, such as "sample-bank-bot," and add a description like "This bot helps customers check their balance and transfer funds."

  6. Under the IAM permissions section, select "Create a role with basic Amazon Lex permissions."

  7. For the COPPA option, select No (since this bot is not intended for children under 13).

  8. Keep the Idle session timeout at its default setting of 5 minutes.

  9. Click Next.

  10. On the Add languages page, select English (US) and pick any voice that you prefer.

  11. Set the Intent classification confidence threshold to 0.40 (meaning the bot will trigger intents when it’s at least 40% confident).

  12. Click Create to finish.

Wooo!!! We've successfully created a simple Amazon Lex V2 chatbot named 'sample-bank-bot' with basic settings. The bot is set to use the English (US) language, a chosen voice, and an intent classification threshold of 40%. We've also given it an IAM role with basic permissions to interact with Lex services.

Step 2: Creating a Welcome Intent

  1. Once your bot is created, you'll be directed to the Intent page.In Intent, Here, click on New Intent already available one.

  2. Rename the default New Intent to WelcomeNewIntent and provide a clear description, such as "This intent greets users when they start interacting with the bot," for better understanding and future reference.

  3. In the Sample utterances section, input phrases users are likely to say when they first interact with the bot. The sample utterances guide Lex in recognizing user intents by matching these predefined phrases with user inputs. Examples include:

    • "Hello"

    • "Hi"

    • "I need help"

    • "Can you help me?"

Add more utterances if needed to cover various ways users might greet the bot.

  1. Next, scroll down to Closing response section, and expand the Response sent to the user after the intent is fulfilled here you can provide the response you want the bot to display when one of the sample utterances is detected. This is the message the user will see when they greet the bot.

    • For this intent, you could use a message like: "Hi, I'm Keerthi, the banking bot. How can I assist you today?"

  1. Click the Save Intent button once you're done.

  2. Now, click the Build button at the top-right of the console. Building the bot integrates all the intents, utterances, and responses into a deployable model. Depending on the complexity of your bot, this may take a minute or two.

  3. After the build is complete, test your bot by opening the Test window. Enter some of the sample utterances you provided (e.g., "Hello" or "Can you help me?") to see if the bot responds correctly with the message you specified. If needed, adjust the utterances or responses and rebuild the bot.

Here's the fun part!🤖🎉 Use voice messages to talk to the bot. Click the microphone icon and start speaking. The bot will process your voice input and respond just like it does with text. This is perfect for hands-free use or accessibility needs. Test it thoroughly to make sure it works well with different accents and speech patterns.

In this step,we have successfully created a new intent called WelcomeNewIntent, defined sample utterances for user greetings, and set a custom response. We built the bot to integrate these updates and tested it to ensure it responds correctly to user inputs.

Step 3: Creating Fallback intent

In the previous snippet, you can see the fallback intent message as "Intent Fallback intent is fulfilled". In this step, let's rephrase that message to make it clearer to the user that your chatbot doesn't understand the user's request.

  1. In the Intents section of your bot, locate the default Fallback Intent. This intent is triggered when the bot cannot match the user's input to any other intent.

  2. Click on that default Fallback Intent, and under the Closing response section, add a message that will be displayed when the bot doesn’t understand the user’s input. For example, you can use: "Sorry! I'm having trouble understanding. Can you describe it in a different way?"

  3. To make the bot's responses more conversational, add response variations. This ensures that the bot doesn't always give the same reply. For example, you could add variations like:

    • "Ahh sorry!! I didn’t get it. Are you trying to get your account balance?"

    • "I’m not sure I understand. Can you explain it differently?"

    • "Oops! That was unclear. Can you say it another way?"

  1. Add the variations, click Save Intent, then click Build to integrate the fallback intent into the bot, and

  2. Finally test the bot using the Test window with random, unrecognized input to see the different responses.

Step 4: Create a slot

A slot is a variable that represents a piece of information that the bot needs to fulfill a user’s request. Slots are used to capture user input for specific data points, allowing the bot to understand and process requests more effectively. Here’s a detailed breakdown of slots:

  1. In the Amazon Lex console, choose Slot types in your left hand navigation panel.

  2. click on Add slot type and choose Add blank slot type.

  3. Enter accountType for the Slot type name and Choose Add.

  4. After successfully adding, you will see the Slot value resolution panel. Choose "Restrict to slot values." This ensures that only the values you specify will be considered valid for accountType. Otherwise, Amazon Lex will use machine learning to accept other values that users frequently enter.

  5. To add these three account types, click on the value fields and start adding Checking, Savings, and Credit using the Add value button. For the credit value, you can specify synonyms that users might enter, such as credit card, amex, American Express, Visa, and MasterCard.

  6. After adding values, click on the save slot type to save your changes. That’s it we are done with the slot creation.

In this step, we created a slot type called accountType to capture specific user input about their account type (e.g., Checking, Savings, Credit). By restricting slot values, we ensure that only predefined options are valid. We added account types and their synonyms, which helps the bot accurately understand user requests. Finally, we saved the slot type to complete the setup, enabling the bot to use this information to fulfill user queries.

Step 5: Create a check balance intent

In this step, we will create the CheckBalance intent in AWS Lex, allowing users to easily ask for their account balances. We'll define the intent, add example phrases users might say, and set up slots to gather user information for accurate and secure answers. By the end, our bot will be ready to assist users with their balance inquiries.

  1. Navigate to the Intents page in your AWS Lex console. Click on Create Intent to initiate the process of creating a new intent.

  2. Name your intent for example "CheckBalance". This name should clearly reflect the purpose of the intent, which is to handle user requests related to checking their account balance.

  3. In the Description field, provide an appropriate description for the intent. For example:
    "This intent allows users to check the balance in their bank accounts."

  4. Under the Sample utterances section, add several phrases that users might say when they want to check their balance. These utterances help Lex understand user intent. Include phrases like:

    • "What's the balance in my account?"

    • "Check my balance."

    • "What’s the balance in my {accountType} account?"

    • "Can you tell me my account balance?"

    • "How much money do I have in my account?"

    • "Show me my balance."

  5. Next, move to the Slots section. Here, you will add the necessary slots to capture user input.

  6. Click on Add slot to create a new slot named "accountType," select the previously created slot type (e.g., AccountType), and enter the prompt "For which account would you like to check your balance?" to guide the user in specifying the account type.

  7. After adding the accountType slot, create another slot named dateOfBirth with the built-in slot type Amazon.Date and use the prompt "For verification purposes, what's your date of birth?" to verify the user's identity before sharing sensitive information.

  8. After setting up both slots, click Save Intent to save your changes.

  9. Click the Build button to compile the new intent and slot configurations into your bot.

  10. After building, use the Test window to enter sample phrases and ensure the bot correctly requests the account type and date of birth while accurately responding to balance inquiries.

Our bot is now equipped to assist users with their account balance inquiries through the CheckBalance intent and its associated slots. We can proceed to enhance its capabilities by showing the numbers instead of “intent checkBalance is fulfilled message

Step 6: Create Your AWS Lambda Function

In this step, we will create an AWS Lambda function named BankingBotEnglish. This function will handle requests from our Amazon Lex chatbot, using the CheckBalance and FollowupCheckBalance intents. It will retrieve session attributes and slot values, generate a random account balance, and respond to user inquiries.

  1. Go to Lambda in your AWS Management Console

  2. choose "Author from scratch".

  3. name the function "BankingBotEnglish".

  4. set runtime to Python 3.12 or later.

  5. create the function.

  6. you will enter into your function here in the code source pane. click on the lambda_function.py and replace the existing code with this code.

  7. click "Deploy" to complete the setup.

Step 7: Connect the lambda to amazon lex

  1. Go to amazon lex page and click on the bot that we created in step 1. In my case, it’s ‘sample-bank-bot’.

  2. choose Aliases, and click on the default TestBotAlias.

  3. From the Languages panel, select English (US).

  4. Click on English (US) to open the Lambda function panel, where you can link a Lambda function to the TestBotAlias version.

  5. Here choose source as the lambda function the one we created in step 2 BankingBotEnglish and set the version field to $LATEST.

  6. click save.

The Lambda function is ready for the BankingBot intents, but we need to specify which intent will use it in Amazon Lex. Let’s do it!!

Step 8: Connecting CheckBalance intent to Lambda Function

  1. Click on the CheckBalance intent from your intents page.

  2. Scroll down to fulfilment pane,here expand the On successful fulfilment bubble and choose advanced options.

  3. Here in Fulfilment Lambda code hook panel, check the checkbox next to Use a Lambda function for fulfilment.

  4. choose update options, followed by save intent, build and test.

Now you can see that when a user successfully enters the account type and date of birth, our chatbot can provide the account balance successfully!!. Great what is missing here? the chatbot now will not be able share the information or carrry forward the information from one intent to another intent. let’s work on this in next step.

Step 6: Setting up context carryover

This technique helps your chatbot remember things it's learned about the user (like their birthday) from one intent and share them with other intents!

  1. Go to the CheckBalance intent we created earlier.

  2. Scroll down to the contexts panel to see the input and output contexts options.

  3. Click on the Output contexts drop-down and then click the New context tag button. Context tags in Amazon Lex store and check specific information across a conversation, preventing users from repeating themselves.

  4. Name your new context contextCheckBalance and set the timeout for 5 turns or 90 seconds to limit how long the chatbot remembers a user's birthday for security reasons.

  5. Choose Add, then Save intent, Build, and Test to ensure the bot works as usual without errors from the context tag.

Step 7: Create FollowupCheckBalance intent

Now that you are familiar with intent creation. Let’s go in step wise pattern

  1. Name your intent “FollowupCheckBalance” with a meaningful description like “Intent to allow a follow-up balance check request without authentication”.

  2. choose input context from context panel and add contextCheckBalance.

  3. Add some sample utterances like:

    • How about my {accountType} account?.

    • What about {accountType} ?.

    • And in {accountType} ?

  4. Add a new slot with name:accountType, prompt: "For which account would you like your balance?" and slot type: accountType.

  5. Add another slot with name:dateOfBirth, type:AMAZON.Date and prompt: “For verification purposes, what is your date of birth?”.

  6. After adding, expand the dateOfBirth slot, select Advanced options, scroll to the Default values panel, and enter #contextCheckBalance.dateOfBirth to set default values for the intent's slots.

  7. Choose "Add default value," then "Update slot,"

  8. Go to the Fulfillment intent pane to ensure the Lambda function is connected to this intent, expand "On successful fulfillment," choose "Advanced options," go to the Fulfillment Lambda code hook panel, and select the checkbox to enable a fulfillment Lambda for this intent.

  9. Choose Add, then Save intent, Build, and Test. Then try to trigger the new FollowupCheckBalance intent without triggering CheckBalance first, for example, by asking your chatbot, "What about checking?"

    Everything is going well so far! But wait, besides checking the balance, we also perform transfers through bankingbots, right? Let's make our chatbot handle transfer operations too.

Step 8: Create Transfer intent

let’s do it super quick, as you would have become pro in creating intents by now!!

  1. Create a intent with the name “TransferFunds”

  2. Add proper description for it eg: “Help user transfer funds between bank accounts”

  3. Sample utterances:. Can I make a transfer?.

    • I want to transfer funds.

    • I'd like to transfer {transferAmount} from {sourceAccountType} to {targetAccountType}.

    • Can I transfer {transferAmount} to my {targetAccountType}.

    • Would you be able to help me with a transfer?.

    • Need to make a transfer

  4. Add a three new slots called with the below information

    • sourceAccountType with the prompt "Which account would you like to transfer from?" and the slot type accountType.

    • targetAccountType with the prompt "Which account are you transferring to?" and the slot type accountType.

    • transferAmount with the prompt "How much money would you like to transfer?" and the slot type AMAZON.Number.

  5. In the Confirmation panel, expand Prompts, and enter the Confirmation prompt as “Got it. So we are transferring {transferAmount} from {sourceAccountType} to {targetAccountType},” with the Decline response: “The transfer has been cancelled.” like shown in below snippet.

  6. Scroll to the Closing response pane and add the message: “The transfer is complete. {transferAmount} should now be available in your {targetAccountType} account.”

Now again save, build and test the intent.

Visual representation

Visual representation of our intents and slots. you switching from editor to visual builder mode.

Warning!!! Make sure to delete the resources you created to avoid unnecessary charges.

Congratulations!!!🎉 You've sucessfully created a fully functional banking chatbot that can greet users, check balances, and transfer funds! Keep building on these skills for even more advanced features! 🚀. Check out the network.org platform for more interesting projects and keep learning. In our upcoming AWS series article, let's see how we can create all of this in less than 10 minutes.

10
Subscribe to my newsletter

Read articles from Keerthi Ravilla Subramanyam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Keerthi Ravilla Subramanyam
Keerthi Ravilla Subramanyam

Hi, I'm Keerthi Ravilla Subramanyam, a passionate tech enthusiast with a Master's in Computer Science. I love diving deep into topics like Data Structures, Algorithms, and Machine Learning. With a background in cloud engineering and experience working with AWS and Python, I enjoy solving complex problems and sharing what I learn along the way. On this blog, you’ll find articles focused on breaking down DSA concepts, exploring AI, and practical coding tips for aspiring developers. I’m also on a journey to apply my skills in real-world projects like predictive maintenance and data analysis. Follow along for insightful discussions, tutorials, and code snippets to sharpen your technical skills.