Creating a Chatbot using Amazon Lex Service: Day 2 (AWS Lambda Functions)

In this article we’ll learn how to create lambda functions in AWS and hook to the chatbot that we created in the last article.

Advertisements

Introduction

In the last article of this tutorial series, we learned what are chatbots and how we can create a basic chatbot using Amazon Lex service. We learned how to set up an AWS account and test the bot. In this article, we’ll learn how to create lambda functions in AWS and hook to the chatbot that we created in the last article.

Series Info

Following is the four-tutorial series to learn how to develop a chatbot using Amazon Lex service and deploy the same on Facebook Messenger.

  1. Creating a Chatbot using Amazon Lex Service: Day 1 (Initial Bot)
  2. Creating a Chatbot using Amazon Lex Service: Day 2 (AWS Lambda Functions)
  3. Creating a Chatbot using Amazon Lex Service: Day 3 (Slots in AWS Lex)
  4. Creating a Chatbot using Amazon Lex Service: Day 4 (Facebook Messenger Integration)

AWS Lambda Function

Let’s enable our bot to use a lambda function i.e the defined business logic that a bot executes for fulfillment i.e. for the user when he types “yes”. In the Fulfillment section of the intent, choose AWS Lambda function. This will ask you to provide a lambda function, but since we do not have one, we need to create it before we use it.

  1. Go to Services search option and type Lambda. You’ll get the suggestion options, choose the Lambda as shown.
  1. You’ll be landed to AWS Lambda page. Click on Create a function to create a new lambda function for fulfillment.
  1. Select the option to author from scratch as shown below.
  1. Provide the name to the function. You can choose any runtime you are comfortable with to write the code. I am choosing here Python 3.7 to write a simple python code and return the result.
  1. Click on Create function and you’ll see the option to write the code. Choose “Edit code inline”. The Handler details are pre-populated i.e. “function name.handler name“. Write the function to return the text as “Our working hours are 9 AM to 6 PM IST on weekdays” as shown below.
  1. Test the function implementation by clicking the Test button and if everything is fine the test execution will show the result as succeeded.
  1. Now go back to the Intent and under fulfillment when AWS Lambda option is chosen, the recently created function is available. Choose that function.
  1. Again, build your bot.
  1. Now when you test the bot and after the confirmation of the bot if you type yes that means the fulfillment needs to be called and the bot here returns the text that we provided as a return text in the lambda function. So, our fulfillment and the lambda function work fine.

Conclusion

In this article of the tutorial series, we learned how to create an AWS lambda function and hook to the existing chatbot that was created in the last article. You can choose the programming language of your choice to create a lambda function and provide custom functionality/business logic to be hooked to the bot. In the next article, we’ll discuss Slots and how to work with Slots in Amazon Lex.

Author: Akhil Mittal

Akhil Mittal is a Microsoft MVP(Most Valuable Professional), C# Corner MVP, Codeproject MVP, a blogger, author and likes to write/read technical articles. Akhil has an experience of around 11 years in developing, designing, architecting enterprises level applications primarily in Microsoft Technologies. Akhil enjoys working on technologies like MVC, Web API, Entity Framework, Angular, C# and BlockChain. Akhil is an MCP( Microsoft Certified Professional) in Web Applications (MCTS-70-528, MCTS-70-515) and .Net Framework 2.0 (MCTS-70-536). Visit Akhil Mittal’s personal blog CodeTeddy (https://codeteddy.com) for some good and informative articles.