1
votes

I have copied and editied the OrderFlowers Amazon Lex blueprint to ask for a person's first and last name. When testing the Lambda function, I get the correct response but when trying to invoke my intent in Lex I get a 424 error Failed Dependency in my broswer console, and my test bot reports "An error has occurred: Invalid Lambda Response: Received error response from Lambda: Unhandled". I have looked over the code a number of times and I cannot work out if it is a permission issue or, there is something wrong with the fulfillment. My code is below and thanks in advance.

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
have you checked the logs on Cloud Watch?sid8491

1 Answers

0
votes

I tried to reproduce your error and the only situations when the error occured in my case were when I had some syntax errors like improper indentation. Go to function logs (either: go to https://console.aws.amazon.com/cloudwatch, click Logs on the left pane, choose the logs group corresponding to your lambda function, choose log stream having the time when you saw the error and look for some messages or open your lambda function code, go to Monitoring tab and click View logs in CloudWatch)

I saw that the code you shared here has wrong indentation (functions body should have one more tab). Does your code in lambda function editor look the same or the indentaions disappeared while pasting the code here?