How to integrate Amazon Lex ChatBot with Java aws sdk library

Posted By : Amarjeet Kumar | 09-Mar-2021
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.
  • 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

<!-- -->


  • Here, a service is created named ChatBotService containing all the required methods to achieve it.

public class ChatBotService {

	public String chatWithChatBot(String requestText) throws ChatException {

	AmazonLexRuntime amazonLexClient = ChatBotConnector.createAmazonLexClient();
		PostTextRequest textRequest = ChatBotConnector.createTextRequest();
		String resultText = "";
		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(),
                      // Amazon Lex Pre-defined Dialog State Type
		else if (textResult.getDialogState().equals("Fulfilled"))
			resultText = textResult.getMessage();
			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.
This post will be helpful in integration of the Amazon Lex ChatBot using Java aws SDK.

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..