具有Lambda函数的Amazon Lex-错误424依赖关系失败(Python)

时间:2018-09-28 09:42:24

标签: amazon-web-services aws-lambda amazon-lex

我已经复制并编辑了OrderFlowers Amazon Lex蓝图,以询问某人的名字和姓氏。测试Lambda函数时,我得到正确的响应,但是当尝试在Lex中调用我的意图时,我在broswer控制台中收到424错误失败依赖关系,并且我的测试机器人报告“发生了一个错误:无效的Lambda响应:从Lambda收到错误响应:未处理”。我已经看过代码很多次了,如果是权限问题,或者实现有问题,我将无法解决。我的代码在下面,在此先感谢。

import math
import dateutil.parser
import datetime
import time
import os
import logging

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def get_slots(intent_request):
    return intent_request['currentIntent']['slots']

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

def close(session_attributes, fulfillment_state, message):
response = {
    'sessionAttributes': session_attributes,
    'dialogAction': {
        'type': 'Close',
        'fulfillmentState': fulfillment_state, 
        'message': message
    }
}

return response

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

def parse_int(n):
try:
    return int(n)
except ValueError:
    return float('nan')

def build_validation_result(is_valid, violated_slot, message_content):
if message_content is None:
    return {
        "isValid": is_valid,
        "violatedSlot": violated_slot,
    }

return {
    'isValid': is_valid,
    'violatedSlot': violated_slot,
    'message': {'contentType': 'PlainText', 'content': message_content}
}

def validate_get_name(first_name, last_name):
first_name = ['john', 'ben']
if first_name is None and first_name not in first_name:
    return build_validation_result(False,
                                   'FirstName',
                                   'Sorry please state your first name.  ')
last_name = ['smith', 'doran']
if last_name is None and last_name not in last_name:

    return build_validation_result (False,
                                    'LastName',
                                    'Sorry please state your last name. ')

return build_validation_result(True, None, None)

def get_name(intent_request):

first_name = get_slots(intent_request)["FirstName"]
last_name = get_slots(intent_request)["LastName"]
source = intent_request['invocationSource']

if source == 'DialogCodeHook':

    slots = get_slots(intent_request)

    validation_result = validate_get_name(first_name, last_name)
    if not validation_result['isValid']:
        slots[validation_result['violatedSlot']] = None
        return elicit_slot(intent_request['sessionAttributes'],
                           intent_request['currentIntent']['name'],
                           slots,
                           validation_result['violatedSlot'],
                           validation_result['message'])


    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
    if get_name is not None:

        return delegate(output_session_attributes, get_slots(intent_request))


return close(intent_request['sessionAttributes'],
             'Fulfilled',
             {'contentType': 'PlainText',
              'content': 'Thanks, we have now preformed your intent'})


def dispatch(intent_request):

logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name']))

intent_name = intent_request['currentIntent']['name']


if intent_name == 'GetName':
    return get_name(intent_request)

raise Exception('Intent with name ' + intent_name + ' not supported')

def lambda_handler(event, context):

os.environ['TZ'] = 'America/New_York'
time.tzset()
logger.debug('event.bot.name={}'.format(event['bot']['name']))

return dispatch(event)

1 个答案:

答案 0 :(得分:0)

我试图重现您的错误,并且在我的情况下发生错误的唯一情况是当我遇到一些语法错误(例如缩进不当)时。转到功能日志(或者:转到https://console.aws.amazon.com/cloudwatch,单击左窗格上的Logs,选择与您的lambda函数相对应的日志组,选择具有看到错误的时间的日志流并查找一些消息或打开您的lambda函数代码,请转到Monitoring标签,然后单击View logs in CloudWatch

我看到您在此处共享的代码缩进错误(函数主体应再有一个制表符)。在lambda函数编辑器中粘贴代码时,代码看起来是否一样,或者缩进消失了?