Python 3.x Facebook messenger聊天机器人与Flask和pymessenger

Python 3.x Facebook messenger聊天机器人与Flask和pymessenger,python-3.x,curl,flask,wit.ai,facebook-chatbot,Python 3.x,Curl,Flask,Wit.ai,Facebook Chatbot,我用flask、pymessenger和wit.ai创建了一个messenger聊天机器人 我想添加facebook提供的模板(如按钮、添加图像和声音媒体)() 有一些使用curl和json的东西我不明白。有人能帮助我吗?我应该把这些代码片段放在python代码的什么地方 @app.route('/', methods=['POST']) def webhook(): data=request.get_json() 日志(数据) def日志(消息): 打印(信息) sys.stdout

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

我想添加facebook提供的模板(如按钮、添加图像和声音媒体)()

有一些使用curl和json的东西我不明白。有人能帮助我吗?我应该把这些代码片段放在python代码的什么地方

    @app.route('/', methods=['POST'])
def webhook(): data=request.get_json() 日志(数据)

def日志(消息): 打印(信息)
sys.stdout.flush()HTTP请求使用以下两种格式之一:

GET:所有请求信息都在url中

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

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

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

…下面的JSON部分中有POST请求信息

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

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

要在Python中实现这一点,您可以通过Python的内置库发送请求,或者安装一个软件包(例如请求),查看“通过Python发送http请求”

下面是一个示例(改编自):

从urllib.parse导入urlencode
从urllib.request导入请求,urlopen
#我们将请求发送到的url
url=”https://graph.facebook.com/v2.6/me/..."
#POST请求数据
请求\u数据={
“收件人”:{
“id”:”
},
“信息”:{
“附件”:{
...
}
}
}
#使用url和数据创建请求
post\u请求=请求(url,urlencode(请求\u数据).encode())
#发送到Facebook!响应是来自Facebook的API响应
response=urlopen(post_请求).read().decode()

感谢您的快速响应。我已经添加了响应代码,假设我想添加选项1和选项2作为按钮,您可以帮助我编写代码吗?您当前的代码正在使用send_text_message()函数发送消息,该函数在后台发送POST请求。这对按钮不起作用,因为post请求的结构不同。您需要创建一个单独的函数,该函数使用Facebook API网站指定的JSON结构发送POST请求。嘿,杰克,谢谢。你能给我一个例子,上面贴着我的代码,真的很新。我不会为你写代码,但我在答案中用你的代码做了一个例子。您可以将该代码改编为函数或将其放入if语句中。
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
    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()