python - Lambda 和 Lex 集成没有从字符串值反序列化的字符串参数构造函数/工厂方法
问题描述
我是 Lex 与 Lambda 集成的新手,之前在 Connect 中使用过 lambda,但不确定为什么此示例代码在我身上失败并且不理解错误消息。
我在 Lex 中设置了两个插槽(empid 和 fever)。机器人的话语启动,然后我收到错误消息,而不是“请输入您的员工 ID?”的问题。
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]
解决方案
我想通了。消息响应必须采用特定格式。那么它在哪里
"message": message
应该是
"message":{
"contentType":"PlainText",
"content":message
}
推荐阅读
- python - Pandas - 比较两个日期时间列失败(同时检查两个列是否为空)
- java - 如何在spring-jpa中使用JPQL获取字节[]
- abap - SE16n 表编辑在 DEV 中有效,但在 QA 系统中无效
- javascript - 检查 `DragDropContext(SortableTreeWithoutDndContext)` 的渲染方法
- python - 构造函数中的 PyQt 实例变量
- php - POST "admin-ajax.php" 400 in WordPress Multisite User Frontend Pages
- python - 来自 Python websocket 的 Django 通道上的数据渲染
- amazon-web-services - 如何使用单个 cloudformation 模板创建多个 Elasticbeanstalk 环境
- javascript - 关于 JavaScript String.prototype.substring 的问题
- react-native - 将 TextInput 值传递给导入的组件