我用Amazon Lex创建了一个聊天机器人,并将其与Facebook Messenger集成在一起。在Lex Console中定义的响应( Response 部分)返回到Messenger(请参见图1的右上图),但是没有收到由lambda函数(用Python编写)生成的响应(请参见右下)图片上的图片1)-三点图标显示30秒,然后消失。
我使用this video和AWS documentation将机器人与FB集成在一起。在Webhooks订阅中(在FB开发人员面板上),我选择了以下事件:消息,messaging_postbacks,messaging_optins,message_deliveries,message_reads,messagement_payments,message_echoes,standby,messagings_handovers 。该应用尚未获得FB的批准,但应用状态为 Live 。
图片1:
图片2:
import json
user_invoices = ['1/2019', '2/2019', '3/2019']
invoice_status = {'1/2019' : 'Paid', '2/2019' : 'Unpaid', '3/2019' : 'Unpaid'}
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):
return {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Close',
'fulfillmentState': fulfillment_state,
'message': message
}
}
def handle(event, context):
intent_request = event
session_attributes = intent_request['sessionAttributes']
slots = intent_request['currentIntent']['slots']
if intent_request['currentIntent']['name'] == 'CheckInvoiceStatus':
session_attributes['lastIntent'] = intent_request['currentIntent']['name']
try:
inv_no = slots['invoiceNo']
if inv_no == None and intent_request['invocationSource'] == 'DialogCodeHook':
return elicit_slot(
session_attributes,
intent_request['currentIntent']['name'],
slots,
'invoiceNo',
{
'contentType': 'PlainText',
'content': 'Please enter invoice number.'
}
)
session_attributes['invoiceNo'] = inv_no
if not inv_no in user_invoices:
return delegate(session_attributes, intent_request['currentIntent']['slots'])
inv_status = invoice_status[inv_no]
return close(
session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': 'Invoice no. {} is {}.'.format(inv_no, inv_status.lower())
}
)
except KeyError as e:
return close(
session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': 'error: {}, input: {}.'.format(e, event)
}
)
elif intent_request['currentIntent']['name'] == 'AutoWelcomeMessage':
if 'userName' in session_attributes:
content = 'Hello ' + session_attributes['userName'] + '. How can I help you?'
else:
content = 'Hello. How can I help you?'
return close(
session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': content
}
)
我希望Lex能够执行lambda函数并将其值返回给用户,就像在Amazon Lex控制台中设置机器人时一样。
答案 0 :(得分:0)
已解决 。
我的代码使用了sessionAttributes
中的intent_request
,问题是当没有会话属性时,相应的JSON元素被设置为空,即"sessionAttributes" : {}
(如您所见)图片1),但如果来自Facebook的消息,则此元素设置为None
,即"sessionAttributes" : None
(如您在图片2上看到的那样)。
这样,我会收到如下错误:
[ERROR] TypeError: argument of type 'NoneType' is not iterable
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 104, in handle
if 'userName' in session_attributes:
或
[ERROR] TypeError: 'NoneType' object does not support item assignment
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 54, in handle
session_attributes['lastIntent'] = intent_request['currentIntent']['name']
我通过查看lambda函数日志发现了这一点(在lambda函数设计网页上,单击Monitoring
标签和View logs in CloudWatch
)。
图片1:
图片2: