Aws lambda AWS lambda函数问题
我试图用lambda构建一个简单的聊天机器人,但是,我无法找出我的lambda代码哪里出错了。据推测,我只允许硬编码三个名称,其中不包含名称“peter”。但当我输入peter时,它没有经过验证,似乎我的“身份验证”功能有问题。任何人都可以帮忙吗,非常感谢。Aws lambda AWS lambda函数问题,aws-lambda,Aws Lambda,我试图用lambda构建一个简单的聊天机器人,但是,我无法找出我的lambda代码哪里出错了。据推测,我只允许硬编码三个名称,其中不包含名称“peter”。但当我输入peter时,它没有经过验证,似乎我的“身份验证”功能有问题。任何人都可以帮忙吗,非常感谢。 import math import datetime import time import os import logging logger = logging.getLogger() logger.setLevel(logging.
import math
import datetime
import time
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
""" --- Helpers to build responses which match the structure of the necessary dialog actions --- """
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
}
}
""" --- Helper Functions --- """
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_name(patientName):
names = ['jason', 'jane', 'joe']
if patientName is not None and patientName.lower() not in names:
return build_validation_result(False,
'patientName',
'I cannot find your name, please enter again'.format(patientName))
return build_validation_result(True, None, None)
""" --- Functions that control the bot's behavior --- """
def authentication(intent_request):
"""
Performs dialog management and fulfillment for ordering flowers.
Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action
in slot validation and re-prompting.
"""
slot = get_slots(intent_request)
patientName =slot["patientName"]
source = intent_request['invocationSource']
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
# Use the elicitSlot dialog action to re-prompt for the first violation detected.
slots = get_slots(intent_request)
validation_result = validate_name(patientName)
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 {}
return delegate(output_session_attributes, get_slots(intent_request))
return close(intent_request['sessionAttributes'],
'Fulfilled',
{'contentType': 'PlainText',
'content': 'Thank you {}, we have verified your credentials.'.format(patientName)})
""" --- Intents --- """
def dispatch(intent_request):
"""
Called when the user specifies an intent for this bot.
"""
logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name']))
intent_name = intent_request['currentIntent']['name']
# Dispatch to your bot's intent handlers
if intent_name == 'Authentication':
return authentication(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
""" --- Main handler --- """
def lambda_handler(event, context):
"""
Route the incoming request based on intent.
The JSON body of the request is provided in the event slot.
"""
# Singapore time zone
os.environ["TZ"] = "Asia/Singapore"
time.tzset()
logger.debug('event.bot.name={}'.format(event['bot']['name']))
return dispatch(event)