845614720
845614720

Reputation: 838

424 Error When Trying To Interact With Lex

I'm having a bit of difficulty with an AWS issue that doesn't seem to have that great of documentation.

I have my lambda function here:

public async Task<string> FunctionHandler(ConnectRequest request, ILambdaContext context)
        {
            AmazonLexClient lexClient = new AmazonLexClient();

            var response = new PostTextResponse();

            PostTextRequest postRequest = new PostTextRequest();

            postRequest.BotName = "X";
            postRequest.BotAlias = "X";
            postRequest.UserId = Guid.NewGuid().ToString();
            postRequest.InputText = "What Time Is My Appointment?";



            try
            {
                response = await lexClient.PostTextAsync(postRequest);
                context.Logger.Log(response.IntentName);
                context.Logger.Log(response.DialogState);
            }
            catch (Exception ex)
            {

                context.Logger.Log($"EXCEPTION CAUGHT: {Environment.NewLine} {ex.ToJson()} {Environment.NewLine} {response.Message} {response.IntentName} {response.SlotToElicit}");
                return "Error";
            }

            context.Logger.Log($"Success from lambda {Environment.NewLine}  Message: {response.Message} {Environment.NewLine} " +
                               $"Dialog State:{response.DialogState}");


            return "Success";
        }

I am invoking this from a connect flow like so: enter image description here

And what I'm getting back in return is:

"ErrorType": 2,
    "ErrorCode": "DependencyFailedException",
    "RequestId": "",
    "StatusCode": 424,
    "Message": "Invalid Lambda Response: Received error response from Lambda: Unhandled",
    "Data": {},
    "InnerException": {
        "Response": {
            "StatusCode": 424,
            "IsSuccessStatusCode": false,
            "ContentType": "application/json",
            "ContentLength": 85,
            "ResponseBody": {}
        },
        "Message": "Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown."

Which I read in the Amazon PostText Documentation could mean a couple things:

I have confirmed that my Lambda does have permission to use PostText and access Lex. I have tried changing my return type of my function to PostTextReponse with no luck, So I'm not sure where to go from here, there isn't much documentation for this kind of thing.

Any help is appreciated, thanks!

Upvotes: 0

Views: 811

Answers (1)

845614720
845614720

Reputation: 838

For anybody that is curious about this I have found an answer:

First off, when using a Lambda function like this it is a good idea to return an object of what you want. Which is what I ended up doing. You also need to set SessionAttributes in JSON format.

My code is working and is as follows now:

public async Task<LambdaResponseItem> FunctionHandler(ConnectRequest request, ILambdaContext context)
        {
            var client = new AmazonLexClient();
            var response = new PostContentResponse();
            var lambdaInfo = new Dictionary<string, string>();

            var contentRequest = new PostContentRequest();
            var postContentStream = new MemoryStream();
            var postContentWriter = new StreamWriter(postContentStream);

            try
            {
                var userInput = request.Details?.Parameters?.GetValueOrDefault("UserInput");

                postContentWriter.Write(userInput); // Grab user input (utterance) value from AWS Connect.
                postContentWriter.Flush();
                postContentStream.Position = 0;

                contentRequest.Accept = "text/plain; charset=utf-8";
                contentRequest.BotName = "IntroGreeting";
                contentRequest.BotAlias = EnvironmentVariables.IsProduction ? "Production" : "Development"; 
                contentRequest.ContentType = "text/plain; charset=utf-8";
                contentRequest.UserId = request.Details?.ContactData?.ContactId;
                contentRequest.InputStream = postContentStream;
                contentRequest.SessionAttributes = request.Details?.Parameters?.ToJson(); // * Must be in Json format or request will return error *



                // POST to Lex
                response = await client.PostContentAsync(contentRequest);
                return new LambdaResponseItem(){
                    Content = ""
                     }

            }
            catch (Exception ex)
            {
                context.Logger.Log($"POST Request to Amazon Lex Failed {ex.ToJson()}");
                }

Upvotes: 1

Related Questions