我已经写了5-6个插槽,并有调用第三方天气api的lambda函数,我正在从api获得响应。现在,我如何处理该响应,并将其与响应一起发送回lex插槽。
例如:在Lex中,slot Country我输入印度,然后City I输入Hyderabad.Here我正在调用lambda函数,并希望响应应该在lex插槽中包含温度详细信息。
我使用Lex控制台和Lambda函数作为内联代码编辑器。
发布于 2018-04-06 15:30:58
我将使用2个插槽,并处理代码中的空插槽(python)。
所以首先你必须在你的意图中定义两个槽city
和country
,机器人将检查槽的值是否填充在DialogCodeHook
中,如果验证成功,它将调用FulfillmentCodeHook
调用天气api将结果返回给用户。
注意:您必须检查Initialization and validation code hook
,这样它才会转到DialogCodeHook
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 city and country
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
if slots['city'] 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
'city', # slot name
'Please enter city name' # prompt the user to enter city name
)
if slots['country'] is None:
return elicit_slot(
intent_request['currentIntent']['name'], # current intent name
slots, # current intent slots
'country', # slot name
'Please enter country name' # prompt the user to enter country name
)
# 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'])
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 == 'GetWeather':
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)
希望能有所帮助。
发布于 2018-07-15 20:07:44
对Lex的Lambda响应应具有以下结构,其中dialogAction参数是必需的
"dialogAction":
{
"type": "Close",
"fulfillmentState": "Fulfilled or Failed",
"message": {
"contentType": "PlainText or SSML or CustomPayload",
"content": "Message to convey to the user. For example, Thanks, your pizza has been
ordered."
},
"responseCard": {
"version": integer-value,
"contentType": "application/vnd.amazonaws.card.generic",
"genericAttachments": [
{
"title":"card-title",
"subTitle":"card-sub-title",
"imageUrl":"URL of the image to be shown",
"attachmentLinkUrl":"URL of the attachment to be associated with the card",
"buttons":[
{
"text":"button-text",
"value":"Value sent to server on button click"
}
]
}
]
}
您可以使用以下代码作为示例,在回调中创建node.js响应。在这里,JSON对象是使用dialogAction强制参数创建的,并添加了其他属性。将“要返回的数据”更改为要返回到AWS Lex的值。
var obj = {};
var dialogAction = {};
dialogAction.type="Close";
dialogAction.fulfillmentState="Fulfilled";
var message = {};
message.contentType="PlainText";
message.content="Data to return";
dialogAction.message = message;
obj.dialogAction = dialogAction;
connection.end(function (err) { callback(err, obj)});
https://stackoverflow.com/questions/49676394
复制相似问题