我目前正在尝试将快速回复按钮添加到我使用Amazon Lambda中使用Amazon Lex和Python开发的messenger聊天机器人。我在Facebook的开发者网站上发现了这个:
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"<PSID>"
},
"message":{
"text": "Here's a quick reply!",
"quick_replies":[
{
"content_type":"text",
"title":"Search",
"payload":"<POSTBACK_PAYLOAD>",
"image_url":"http://example.com/img/red.png"
},
{
"content_type":"location"
},
{
"content_type":"text",
"title":"Something Else",
"payload":"<POSTBACK_PAYLOAD>"
}
]
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"
我尝试过寻找解决方案,但他们需要fbmessenger库。我希望使用urllib库而不是开源库。有解决方案吗?
Quick Reply Button in Messenger
def helpMe(intent_request):
session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
return close(
session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': 'It seems like you need help, let me save you from your misery.\n\n'
+ '- If you want to find '
+ 'out what insurance plan is suitable for your dear self, try asking me "Which integrated Shield Plan is right for me?".\n\n '
+ '- If you want me to explain about your current plan, you could try asking me '
+ '"Explain my current plan".\n\n '
+ '- If you wanna listen to my extremely hilarious puns, just type in "joke" and you will not regret it hehe. '
},
'responseCard': {
'version': '0',
'contentType': 'application/vnd.amazonaws.card.generic',
'genericAttachments': [
{
'title': 'Help',
'subTitle': 'Select button of choice',
'imageUrl': '',
"buttons":[
{
"text":"recommend plan",
"value":"Which integrated Shield Plan is right for me"
},
{
"text":"current plan",
"value":"Explain my current plan"
},
{
"text":"tell me a joke",
"value":"joke"
}
]
}
]
}
)
答案 0 :(得分:0)
这大致是你可以获取和发送的方式。
获取消息:
data = request.get_json()
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
if messaging_event.get("message"):
sender_id = messaging_event["sender"]["id"]
recipient_id = messaging_event["recipient"]["id"]
message_text = messaging_event["message"]["text"]
send_message(sender_id, "roger that!")
if messaging_event.get("delivery"):
pass
if messaging_event.get("optin"):
pass
if messaging_event.get("postback"):
pass
return "ok", 200
发送消息:
您可以在
中包含快速回复按钮代码“消息”
def send_message(recipient_id, message_text):
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"]
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
"quick_replies":[{
"content_type":"text",
"title":"Search",
"payload":"<POSTBACK_PAYLOAD>",
"image_url":"http://example.com/img/red.png"
},
{
"content_type":"location"
},
{
"content_type":"text",
"title":"Something Else",
"payload":"<POSTBACK_PAYLOAD>"
}
]
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
事实上,您可以根据此处显示的curl示例自定义您想要的内容。
答案 1 :(得分:0)
由于您正在使用Lex和Lambda,我认为您希望将响应按钮发送给用户,以便他可以继续进行对话。
以下是在Lambda函数中执行此操作的方法:
'responseCard': {
'version': '0',
'contentType': 'application/vnd.amazonaws.card.generic',
'genericAttachments': [
{
'title': 'title1',
"buttons":[
{
"text":"button_1",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_2",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_3",
"value":"value_to_be_sent_to_server_on_click"
}
]
}
]
}
您也可以在关闭消息和ElicitSlot消息中使用这些按钮,此外,您还可以将图像与按钮一起使用。
以下是关闭消息中按钮和图像的代码。
'dialogAction': {
'type': 'Close',
'fulfillmentState': 'Fulfilled',
'message': {
'contentType': 'PlainText',
'content': message
},
'responseCard': {
'version': '0',
'contentType': 'application/vnd.amazonaws.card.generic',
'genericAttachments': [
{
'title': 'title1',
'subTitle': 'subtitle1',
'attachmentLinkUrl': 'link_that_will_open_on_click',
'imageUrl': 'link_of_image_to_display',
"buttons":[
{
"text":"button_1",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_2",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_3",
"value":"value_to_be_sent_to_server_on_click"
}
]
},
{
'title': 'title2',
'subTitle': 'subtitle2',
'attachmentLinkUrl': 'link_that_will_open_on_click',
'imageUrl': 'link_of_image_to_display',
"buttons":[
{
"text":"button_1",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_2",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_3",
"value":"value_to_be_sent_to_server_on_click"
}
]
},
{
'title': 'title3',
'subTitle': 'subtitle3',
'attachmentLinkUrl': 'link_that_will_open_on_click',
'imageUrl': 'link_of_image_to_display',
"buttons":[
{
"text":"button_1",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_2",
"value":"value_to_be_sent_to_server_on_click"
},
{
"text":"button_3",
"value":"value_to_be_sent_to_server_on_click"
}
]
}
]
}
}
注意:虽然您可以在单个消息中添加最多10个轮播,但您可以在一个轮播中包含最多3个按钮。如果您有超过3个按钮,则不会出现错误,但只会显示前3个。
您需要检查Facebook应用中信使设置中messaging_postbacks
的{{1}}个事件,以制作轮播按钮。
希望它有所帮助。