A chatbot is a computer program written to interact with humans. At present, chatbots are used in various business as an alternative to websites i.e. instead of searching information through website pages, chatbot can help customer through an interactive session which can orientate them towards the information they are seeking.  Chatbot not only provide information but also help customer to do transaction i.e. place order/submit lead, make payment and monitor shipping.
There are many platforms(free or paid) available to develop a chatbot. Here, I am developing chatbot i.e. HelloBot on AWS Lex platform with use of Java programming language.
AWS Lex
Lex is AWS service for building conversational interfaces for any applications using voice and text.
AWS Lambda
Lex is integrated with AWS Lambda, a service that lets you run code without provisioning or managing EC2 servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second and can be easily integrated with other available AWS Services like RDS, S3, SES etc.
Chatbot request will come from user via channel i.e. Facebook, Slack, Twillo etc to Lex. Lex will further pass request to Lambda for fulfillment. Lambda may use other AWS services to fulfill user’s request. Lambda sends back suitable response to Lex and Lex will send back response to user via channel.
Steps to build HelloBot on AWS Lex
Step-1 : AWS account is required. If you don’t have account, you can easily create with help of below link.  https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/
Step-2 : Go to Lambda service in AWS management console and click on “Create Function” button to create Lambda function.
Step-3 :Â At present, No blueprint sample code is available for Lex bot on Java 8 environment. So we have to choose “Author from scratch” option. We need to fill other attributes like function name, runtime environment and role to create lambda function. In our case, we are going to use Java 8 development environment for lambda function creation.
Step-4 : After creation of Lambda function, We need to write Java code which will service the HelloBot. Sample Handler code is as provided as below.
public class HelloHandler extends AbstractLexRequestHandler implements RequestStreamHandler{
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
byte[] requestBytes = IOUtils.toByteArray(inputStream);
byte[] responseBytes = null;
try {
LexRequest lexRequest = LexRequest.fromJson(requestBytes);
if(lexRequest != null)
{
// Get session
Map<String,String> sessionAttributes = null;
if(lexRequest.getSessionAttributes() != null)
sessionAttributes = new HashMap<>(lexRequest.getSessionAttributes());
else
sessionAttributes = new HashMap<>();
// Pull attribute name from Request
String firstName = lexRequest.getCurrentIntent().getSlots().get("firstname");
// Store is session for future use
saveObjectIntoSession(sessionAttributes,"firstname", firstName, new TypeReference<String>() {});
// Create Response
LexResponse lexResponse = new LexResponse(new CloseDialogAction("Fulfilled", new Message("PlainText","Hello "+ firstName + ", Hope your are doing great")),sessionAttributes);
responseBytes = lexResponse.toJson();
}
else
throw new RuntimeException("Request is null: ");
} catch (Exception exception) {
throw new RuntimeException(exception);
}
outputStream.write(responseBytes);
}
}
For complete implementation of Handler code and guide, Kindly refer https://github.com/mag1309/java-aws-lex-hellworld.
Step-5 : After successful creation of Lambda function (i.e. HelloWord) , now go to Lex service in AWS management console and click on “Create” button as below
Step-6 : Choose the “Custom bot†and fill details like Bot name, Output Voice, Session timeout etc and click “Create” button.
Step-7 : Customize Bot further by creating Intent, slots etc. Intent is unique goal or action, user want to achieve. For this bot, Let’s create “Hello” Intent.
Step-8 :Â Complete the attributes required for Hello Intent creation.
- Utterances : Text or voice input by user to invoke Intent is called utterances e.g. I would like to book a flight. Input “Hello” in utterance to initiate “Hello” intent.
- Lambda Initialization : Lambda function used to validate the user input. Lets ignore this for HelloBot.
- Slots: Slots collect user input to fulfill the intent. Lets use firstname as name, AMAZON.US_FIRST_NAME as slot type and “Kindly provide your first name” as prompt. Lex provides both Custom and Built In slot types. At present, Lets use build in slot type i.e. AMAZON.US_FIRST_NAME
- Confirmation Prompt: It ask user whether they wish to proceed with provided input or not. Prompt response accepts ‘Yes‘ or ‘No‘ as input from user. Bot will terminates the flow if user replies ‘No’.
- Fulfillment: Intent fulfillment can be achieved in two ways i.e. Lambda function or return the parameters. Lets use lambda function i.e. HelloWord created in Step-4 for fulfillment.
Step-9 :Â Save the Intent and build the bot. After build, We can test the bot in Test area provided in Lex console.
HelloBot is working and communicating with AWS Lambda.
Hope this blog is helpful to give you a quick start in bot development on AWS Lex platform. HelloBot can be published through various channels like Facebook Messenger, Slack, Twillo etc. Kindly refer following link for channel publishing. https://docs.aws.amazon.com/lex/latest/dg/example1.html