【问题标题】:Facebook messenger chatbot with Flask and pymessenger带有 Flask 和 pymessenger 的 Facebook Messenger 聊天机器人
【发布时间】:2018-12-26 06:33:13
【问题描述】:

我用flask、pymessenger 和wit.ai 创建了一个Messenger 聊天机器人。

我想添加 facebook 提供的模板(如按钮、添加图像和声音媒体)(https://developers.facebook.com/docs/messenger-platform/reference/template/button/)

那里使用了一些我不明白的 curl 和 json 东西。有人可以帮助我吗,我应该将这些 sn-p 放在我的 python 代码中的哪里。

    @app.route('/', methods=['POST'])

定义 webhook(): 数据 = request.get_json() 日志(数据)

if data['object'] == 'page':
    for entry in data['entry']:
        for messaging_event in entry['messaging']:

            sender_id = messaging_event['sender']['id']
            recipient_id = messaging_event['recipient']['id']

            if messaging_event.get('message'):
                if 'text' in messaging_event['message']:
                    messaging_text = messaging_event['message']['text']
                else:
                    messaging_text = 'no text'

                response = None

                entity, value = wit_response(messaging_text)

                if entity == 'newstype':
                    response = "OK. I will send you {} news".format(str(value))
                elif entity == 'cust_greet':
                    response = get_message()
                elif entity == 'cust_bye':
                    response = "Bye! Have a Good Day!".format(str(value))
                elif entity == 'cust_option':
                    response = "Option 1: Option 2:"
                bot.send_text_message(sender_id, response)


return "ok", 200

def 日志(消息): 打印(消息) sys.stdout.flush()

【问题讨论】:

    标签: python-3.x curl flask wit.ai facebook-chatbot


    【解决方案1】:

    HTTP 请求使用以下两种格式之一:

    GET:全部请求信息在url中

    POST:一些信息通过 JSON 格式发送到 url

    我们在 Facebook API 中看到的是一个 POST 请求:url 定义为

    https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>

    ...在下面的JSON部分有POST请求信息

    Curl 是一个用于从终端发送 HTTP 请求的程序。如果您安装了 Curl,您可以填写 JSON/url 信息,运行命令(发送 POST 请求),然后查看为收件人弹出的按钮。就像您希望聊天机器人做的那样!

    但是,Curl 是一个工具,而不是 Python 库!

    要在 Python 中执行此操作,您可以通过 Python 的内置库发送请求,或者安装一个更容易的包(例如请求),查看“通过 python 发送 http 请求”。

    下面是一个例子(改编自this question):

        from urllib.parse import urlencode
        from urllib.request import Request, urlopen
    
        # the url we are sending the request to
        url = "https://graph.facebook.com/v2.6/me/..."
    
        # the POST request data
        request_data = {
                         "recipient": {
                           "id": "<PSID>"
                         },
                         "message": {
                           "attachment": {
                               ...
                           }
                         }
                       }
    
        # create the request with the url and the data
        post_request = Request(url, urlencode(request_data).encode())
    
        # send it to Facebook! Response is the API response from Facebook
        response = urlopen(post_request).read().decode()
    

    【讨论】:

    • 感谢您的快速回复。我已经添加了响应代码,假设我想添加 option1 和 option2 作为按钮,你能帮我写代码吗?
    • 您当前的代码正在使用 send_text_message() 函数发送消息,该函数在后台发送 POST 请求。这不适用于按钮,因为发布请求的结构不同。您需要创建一个单独的函数来发送带有 Facebook API 网站指定的 JSON 结构的 POST 请求。
    • 嘿,杰克,谢谢。你能给我一个上面发布的代码的例子吗,真的很新。
    • 我不会为您编写代码,但我确实在答案中使用了您的代码示例。您可以将该代码改编为函数或将其放入 if 语句中。
    猜你喜欢
    • 2016-08-14
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多