How to add/Method for adding Session Attribute in Amazon Lex bot Event towards Lambda Function

0

Hello, I hope you are fine and doing well.

I am following this tutorial integrating WhatsApp as a-channel for an-amazon-connect-contact-center for an-amazon-connect-contact-center/ for connecting WhatsApp to Amazon Lex.

All is set up and working, but I am facing one issue mentioned below:

Invalid Lambda Response: Received invalid response from Lambda: Cannot construct instance of IntentResponse, problem: The validated object is null at [Source: (String)"{"sessionAttributes": {}, "dialogAction": {"type": "Close", "fulfillmentState": "Fulfilled", "message": {"contentType": "PlainText", "content": "Okay, our representative will call you shortly. Thanks."}}}"; line: 1, column: 204] (Service: LexRuntimeV2, Status Code: 424, Request ID: c86e4fc1-4ef1-42d2-b059-af0798f87998)

This error occurs when I am trying to send an event back to Lex from Lambda because the session attribute in the Lex event sent to Lambda is always empty.

I read every solution On re:Post, stackoverflow, etc but I don't know how to pass the session attribute or set the session attribute so that it can pass to event that is sent to lambda. Other Everything is set up. One more thing is when i set the attributes

Attributes={ 'Customer Name': customerName })

in Below code it also create an error in lambda

def connect_outbound_api(customerName,customerphone): response = client.start_outbound_voice_contact( DestinationPhoneNumber=customerphone, ContactFlowId=contactflowid, InstanceId=connectinstanceid, SourcePhoneNumber=sourcephonenumber, Attributes={ 'Customer Name': customerName })

any help is appreciated. Thank you.

  • Can you elaborate a little more what you try to achieve? To note, that Amazon Connect Attributes for a Contact are not automatically Lex Session Attributes.

    E.g. where and how are you invoke Amazon Lex in that flow?

3 Answers
0

To send Session Attribute value from Connect to Lex, it needs to be done in the Contact Flow. So in the blog post, it will be in the Get Customer Input block in the flow that your chat will kick off, and you can add various values in the Session Attributes values. Then your DialogCodeHook Lambda will get them.

https://docs.aws.amazon.com/connect/latest/adminguide/how-to-use-session-attributes.html

https://docs.aws.amazon.com/connect/latest/adminguide/get-customer-input.html#get-customer-input-lex-tab-properties

profile pictureAWS
answered 2 months ago
profile picture
EXPERT
reviewed 25 days ago
0

Hi Mehar,

First of all, which tutorial are you using? because the link you mentioned is broken. Is it this one?

Next the code you have mentioned is passing the control to Amazon connect and not lex. Is this line of code creating the same error or a different one? The function is requesting amazon connect to create an outbound call. The "Attributes" option mentioned there contributes to Amazon connect's attribute and not lex's, hence, this will not help you set a Lex bot "session attribute".

Now, coming to the error. First I have a few questions:

  1. Is the message ""Okay, our representative will call you shortly. Thanks." your intented response to the user or is it something else?
  2. Have you passed the dialog action type as "close"?

If response for both your questions is no this means this error message will not help you much in detecting where exactly that null value is.

Here are some important points to remember:

  • Session Attributes are not mandatory to be set for lex. The response will be valid without it as well.
  • The "Invalid Lambda Response" error occurs only when you have missed a mandatory element in the response or the structure of the response being sent is incorrect.
  • To see the mandatory elements and the correct structure for your response JSON please see this doc[1]. you can find session attributes inside the "sessionState" field.
  • As it will be very difficult for me to give a solution without debugging the code itself. I request you to kindly follow this method while debugging.
  1. Identify the flow in which you have getting the error.
  2. trace the lambda function according to the flow( using print statements or logging) to find where exactly the error is being generated from.
  3. As the error is related to Response Json format issue. Try to print out the final response just before returning it and compare it with the format in the doc[1]. Check if you are missing anything, or if the format is incorrect.

Kindly let me know whatever you find here and I can help you further.

----- Reference ------

[1] https://docs.aws.amazon.com/lexv2/latest/dg/lambda-response-format.html

profile pictureAWS
answered 2 months ago
profile picture
EXPERT
reviewed 25 days ago
0

It looks like you are using the old Lex v1 response format. As has been mentioned above, if you are using Lex v2 the response should look something like this:

{
  "sessionState": {
    "sessionAttributes": {},
    "dialogAction": {
      "type": "Close"
    },
    "intent": {
      "confirmationState": <<AS PASSED INTO LAMBDA>>,
      "name": <<AS PASSED INTO LAMBDA>>,
      "slots": {<<AS PASSED INTO LAMBDA>>},
      "state": "Fulfilled"
    }
  },
  "messages": [
    {
      "contentType": "PlainText",
      "content": "Okay, our representative will call you shortly. Thanks."
    }
  ]
}
AWS
Gillian
answered 2 months ago
profile picture
EXPERT
reviewed 25 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions