How to integrate Amazon Lex ChatBot with Java aws sdk library
Posted By : Amarjeet Kumar | 09-Mar-2021
Introduction
This post is all about having a clear idea about interaction with the Amazon Lex ChatBot using Java library.
In this post, we will be having step by step explanation of how to do it.
Pre-requisite
- You just need to arrange the Java aws SDK library for interacting with the Amazon Lex ChatBot.
(a) For the non-maven application, you just need to download the jar file using the following link
and keep it in the project directory where all other jars are kept.
(b) For maven application just add the following dependency to the pom.xml file
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.505</version>
</dependency
Implementation
- Here, a service is created named ChatBotService containing all the required methods to achieve it.
@Service
public class ChatBotService {
public String chatWithChatBot(String requestText) throws ChatException {
AmazonLexRuntime amazonLexClient = ChatBotConnector.createAmazonLexClient();
PostTextRequest textRequest = ChatBotConnector.createTextRequest();
String resultText = "";
textRequest.setInputText(requestText);
PostTextResult textResult = amazonLexClient.postText(textRequest);
// Amazon Lex Pre-defined Dialog State Type
if (textResult.getDialogState().startsWith("Elicit"))
resultText = textResult.getMessage();
// Intent + User-defined intent name + is ReadyForFulfillment
else if (textResult.getDialogState().equals("Intent VitilHelpDeskChatBot
is ReadyForFulfillment"))
resultText = (String.format("%s: %s", textResult.getIntentName(),
textResult.getSlots()))
// Amazon Lex Pre-defined Dialog State Type
else if (textResult.getDialogState().equals("Fulfilled"))
resultText = textResult.getMessage();
else
resultText = textResult.toString();
return resultText;
}
}
- Just modify the Service method chatWithChatBot as per your requirement or input-output type and use this Service where ever you need.
- Amazon Lex ChatBot has its predefined input event and output response format.
- ChatBot takes input in its predefined input format only and it returns response only in its predefined response format.
Conclusion
This post will be helpful in integration of the Amazon Lex ChatBot using Java aws SDK.
Reference:
https://aws.amazon.com/lex/
https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk/1.11.505
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amarjeet Kumar
He is working as Associate Consultant-Development on BlockChain Project.