How to use quick replies (FB) in a python chatbot?

我是研究僧i 提交于 2019-12-12 04:38:55

问题


I'm building a Facebook Messenger bot in Python and my script for quick replies is not working event though I already know how to send simple text messages and generic templates. This is my code so far:

This one works:

text = {
    "recipient": {
        "id": user_id
    },
    "message": {
        "text": "some text"
    }
}

This one doesn't:

question = {
    "recipient": {
        "id": user_id
    },
    "message": {
        "quick_replies": [{
            "content_type": "location"
            # "title": "RED",
            # "text": "red",
            # "payload": "red"
         }]
    }
}

POST for both:

headers={"Content-Type": "application/json"}
requests.post('url', data = json.dumps(text), headers=headers)
requests.post('url', data = json.dumps(question), headers=headers)

In the example, title, text and payload are commented since I'm trying to make the script work with something as basic as location... but I keep receiving a response 400 for 'question'; 'text' works just fine.


回答1:


“text”: “some string” or ”attachment”: [] is required when using quick replies.

Check the fields required in the FB Docs




回答2:


The solution is putting "text" key alongside "quick_replies"; otherwise - inside list/dict "quick_replies" as mentioned in FB Docs (for JavaScript) - it won't work:

question = {
    "recipient": {
        "id": user_id
    },
    "message": {
        "text": "<THIS_IS_WHERE_THE_ACTUAL_TEXT_GOES>,
        "quick_replies": [{
            "content_type": "text"
            "title": "Option 1",
            "payload": "option1"
         }]
    }
}


来源:https://stackoverflow.com/questions/45899936/how-to-use-quick-replies-fb-in-a-python-chatbot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!