【问题标题】:Facebook Messenger Quick Reply Button in PythonPython 中的 Facebook Messenger 快速回复按钮
【发布时间】:2018-01-22 12:20:34
【问题描述】:

我目前正在尝试向我在 Amazon Lambda 中使用 Amazon Lex 和 Python 开发的信使聊天机器人添加快速回复按钮。我在 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"
             }
            ]
        }
    ]
    }
)

【问题讨论】:

    标签: python amazon-web-services aws-lambda facebook-messenger amazon-lex


    【解决方案1】:

    这是一个大致的获取和发送方式。

    获取消息:

    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 示例自定义你想要的内容。

    【讨论】:

    • 您好 Salehin,感谢您的回复。我有一个问题,我应该在我的代码中哪里使用这个函数来检索槽回复?是这样吗? 当前: 'def insurance_bot(intent_request): nationality = try_ex(lambda: intent_request['currentIntent']['slots']['nation'])' 现在: ' def insurance_bot(intent_request): 国籍 = r'
    • 您可以查看我的简单聊天机器人如何获取意图值:github.com/salehinRafi/chatbot 但我使用的是 3rd 方库。但我相信它没有什么大的不同。我更喜欢单独的文件以获得更好的可读性。
    • 你知道如何获取recipient_id吗?
    • Salehin,不使用request库,可以使用python中的urllib库吗?
    • @CJSee 当然可以。你可以参考这篇文章 (stackoverflow.com/q/47384859/4216992) 我如何使用 urllib 并且你可以操作你的代码。忽略未回答的问题。我已经找到了解决方案。
    【解决方案2】:

    由于您正在使用 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"
                 }
                ]
            }
        ]
        }
    }
    

    注意:您可以在单个轮播中最多有 3 个按钮,尽管您可以在单个消息中添加最多 10 个轮播。如果按钮超过 3 个,则不会出现错误,但只会显示前 3 个。

    您需要在 Facebook 应用的 Messenger 设置中检查 Webhooks 中的 messaging_postbacks 事件,才能使轮播按钮正常工作。

    您将在 Messenger 中收到这样的回复:

    希望对你有帮助。

    【讨论】:

    • 您好 sid8491,感谢您的回复。实际上,我指的是Messenger中的快速回复按钮。我在我的问题中添加了一个示例“Messenger 中的快速回复按钮”。你有解决办法吗?
    • 这只会在信使中添加快速回复按钮,我也用图片更新了答案,请检查。这些按钮可用于显示信息、获取插槽信息和其他内容。
    • 我已更改图像以显示更好的示例。响应卡方法也可以吗?
    • @CJSee 是的,它会起作用。但是如果我们使用 Lex,按钮在 FB 上的显示方式会有所不同。尝试我在ElicitSlot 代码中发布的第一个代码以获取大小或颜色
    • 嗨 sid8491,我在我的问题中添加了代码。这是你的意思吗?
    猜你喜欢
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多