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
 
 
 
 
 

About Author

Author Image
Amarjeet Kumar

He is working as Associate Consultant-Development on BlockChain Project.

Request for Proposal

Name is required

Comment is required

Sending message..