Интеграция Lambda и Lex без конструктора / фабричного метода String-аргумента для десериализации из значения String - PullRequest
0 голосов
/ 17 июня 2020

Я новичок в интеграции Lex с Lambda, раньше использовал лямбда с Connect, но не уверен, почему этот пример кода не работает, и не понимаю сообщения об ошибке.

У меня в Лексе установлено два слота (empid и fever). Бот начинает произносить звуки, а затем я получаю сообщение об ошибке вместо вопроса «введите свой идентификатор сотрудника?».

import json

def build_response(message):
    return {
        "dialogAction":{
            "type":"Close",
            "fulfillmentState":"Fulfilled",
            "message":{
                "contentType":"PlainText",
                "content":message
            }
        }
    }

def elicit_slot(intent_name, slots, slot_to_elicit, message):
    return {
        'dialogAction': {
            'type': 'ElicitSlot',
            'intentName': intent_name,
            'slots': slots,
            'slotToElicit': slot_to_elicit,
            'message': message
        }
    }

def delegate(session_attributes, slots):
    return {
        'sessionAttributes': session_attributes,
        'dialogAction': {
            'type': 'Delegate',
            'slots': slots
        }
    }

def perform_action(intent_request):
    source = intent_request['invocationSource']   # DialogCodeHook or FulfillmentCodeHook
    slots = intent_request['currentIntent']['slots']  # your slots 
    if source == 'DialogCodeHook':
        # Perform basic validation on the supplied input slots.
        if slots['empid'] is None:  # or any other validation that you want to perform
            return elicit_slot(
                intent_request['currentIntent']['name'], # current intent name
                slots, # current intent slots
                'empid',  # slot name
                'Please enter your employee id'  # prompt the user to empid
            )
        if slots['fever'] is None:
            return elicit_slot(
                intent_request['currentIntent']['name'], # current intent name
                slots, # current intent slots
                'fever',  # slot name
                'Do you have a fever?' # prompt the answer do you have a fever
            )
        # delegate means all slot validation are done, we can move to Fulfillment
        return delegate(output_session_attributes, slots) 
    if source == 'FulfillmentCodeHook':
        #result = your_api_call(slots['city'], slots['country'])
        result = "Your employee id is you answered to having a fever."
        return build_response(result)  # display the response back to user

def dispatch(intent_request):
    intent_name = intent_request['currentIntent']['name']
    # Dispatch to your bot's intent handlers
    if intent_name == 'covidqs':
        return perform_action(intent_request)
    raise Exception('Intent with name ' + intent_name + ' not supported')

def lambda_handler(event, context):
    #logger.debug(event)
    return dispatch(event)

Получаю эту ошибку:

An error has occurred: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of Message: no String-argument constructor/factory method to deserialize from String value ('Please enter your employee id') at [Source: {"dialogAction": {"type": "ElicitSlot", "intentName": "covidqs", "slots": {"empid": null, "fever": null}, "slotToElicit": "empid", "message": "Please enter your employee id"}}; line: 1, column: 143]

1 Ответ

0 голосов
/ 17 июня 2020

Я понял это. Ответ на сообщение должен быть в формате c. Итак, где это

"message": message 

, должно было быть

"message":{
    "contentType":"PlainText",
    "content":message
 }
...