1
votes

I am currently trying to send multiple messages from AWS Lambda to Lex in Python 3.6, but Lex considers my answers as incorrect.

I got multiple errors such as :

  • Received invalid response from Lambda: Can not construct instance of Message, problem: contentType must not be null
  • "content" can not be blank
  • Syntax error in module 'lambda_function': unexpected character after line continuation character [after trying to escape my JSON]
  • Received invalid response from Lambda: Can not deserialize instance of java.lang.String out of START_OBJECT token at...

So basically, I am doing this like this :

    messages = [
        {
            'contentType': 'PlainText',
            'group': 0,
            'value': 'Applying this criteria, you have %d result(s) left.' % len(json.loads(session_attributes['results']))
        },
        {
            'contentType': 'PlainText',
            'group': 1,
            'value': 'What do you want to do next ?'
        }
    ]

format_message('PlainText', messages)

with format_message which TEMPORARILY looks like this (because I've tried many MANY things to make it work... without any success) -- but this one does not work either :

def format_message(message_type, content):
return {'messages': content}

In the end, it gives this format of response (with other lex pre-requisites such as slots etc. but I won't display them here because I don't think it is relevant) :

{'message': {'messages': [{'group': 0, 'contentType': 'PlainText', 'value': 'Applying this criteria, you have 1 result(s) left.'}, {'group': 1, 'contentType': 'PlainText', 'value': 'What do you want to do next ?'}]

I have tried to convert 'messages' array into a string, send it as a JSON etc. but nothing seems to work.

I read all of the documentation listed in this issue...

Have someone already found a solution please ?

Thank you,

1

1 Answers

2
votes

As far as I know, this is not possible from Lambda. Lex only allows one return message. There is something called a Message Group that can be created when you build the Lex bot with JSON from the command-line aws management utility, e.g.:

{
    "metadata": {
        "importFormat": "JSON",
        "importType": "LEX",
        "schemaVersion": "1.0"
    },
    "resource": {
        "abortStatement": {
            "messages": [
                {
                    "content": "Sorry, I could not understand. Goodbye.",
                    "contentType": "PlainText"
                }
            ]
        },
        "childDirected": false,
        "clarificationPrompt": {
            "maxAttempts": 5,
            "messages": [
                {
                    "content": "Sorry, can you please repeat that?",
                    "contentType": "PlainText"
                }
            ]
        },
        "idleSessionTTLInSeconds": 300,
        "intents": [
            {
                "conclusionStatement": {
                    "messages": [
                        {
                            "content": "Hello",
                            "contentType": "PlainText"
                        },
                        {
                            "content": "World",
                            "contentType": "PlainText"
                        }
                    ]
                },
                "fulfillmentActivity": {
                    "type": "ReturnIntent"
                },
                "name": "test",
                "sampleUtterances": [
                    "hello"
                ],
                "slots": [],
                "version": "1"
            }
        ],
        "locale": "en-US",
        "name": "Test",
        "version": "1",
        "voiceId": "Matthew"
    }
}

I think your best bet from a Lambda function is to concatenate the strings together in Python like this:

'message': {
    'contentType': 'PlainText',
    'value': ('Applying this criteria, you have %d result(s) left.' % len(json.loads(session_attributes['results']))) + 'What do you want to do next ?'
}

Please pardon the formatting. I don't normally work in Python.