Facebook Messenger bot通用模板不起作用

时间:2017-04-22 07:57:28

标签: python facebook facebook-graph-api heroku facebook-messenger

我创建了一个可以正常工作的Facebook Messenger机器人。我使用了Button Template和Image模板,两者都完美无缺。但是当我尝试通用模板时,我得不到任何回应。我只需通过执行适当的修改,从here复制粘贴代码。

我不知道如何调试。 Facebook Messenger在消息框上没有输出。我目前正在通过Heroku运行应用程序。

这是我的代码:

def send_message(token, recipient):
    r = requests.post("https://graph.facebook.com/v2.6/me/messages",
     params={"access_token": token},
     data=json.dumps({
      "recipient":{
        "id":recipient
      },
      "message":{
        "attachment":{
          "type":"template",
          "payload":{
            "template_type":"generic",
            "elements":[
               {
                "title":"Welcome to Peter\'s Hats",
                "image_url":"http://www.godominion.com/content/images/feature-img-small-appliance-electronics.png",
                "subtitle":"We\'ve got the right hat for everyone.",
                "default_action": {
                  "type": "web_url",
                  "url": "https://peterssendreceiveapp.ngrok.io/view?item=103",
                  "messenger_extensions": true,
                  "webview_height_ratio": "tall",
                  "fallback_url": "https://peterssendreceiveapp.ngrok.io/"
                },
                "buttons":[
                  {
                    "type":"web_url",
                    "url":"https://petersfancybrownhats.com",
                    "title":"View Website"
                  }           
                ]      
              }
            ]
          }
        }
      }
    }),
     headers={'Content-type': 'application/json'})
    if r.status_code != requests.codes.ok:
      print r.text 

我将不胜感激。

谢谢。

编辑1:解决方案

我通过评论来摆脱这个问题:

"messenger_extensions": true,

"fallback_url": "https://peterssendreceiveapp.ngrok.io/"},

我确定这不是正确的方法。但是当我创建机器人时,如果没有实际的链接,这就可以了。

2 个答案:

答案 0 :(得分:1)

在第二个按钮上,“url”:“https://petersfancybrownhats.com”已损坏。

答案 1 :(得分:1)

尝试这样 首先创建一个函数

def function():

 extra_data = {

            "attachment": {
                "type": "template",
                "payload": {
                    "template_type": "generic",
                    "elements": [
                        {
                            "title": "Any Title",
                            "image_url": "https://mbtskoudsalg.com/images/road-clipart-journey-3.png",
                            "subtitle": "Subtitle.",
                            "buttons": [
                                {
                                    "type": "web_url",
                                    "title": "View",
                                    "url": "**MAKE SURE TO WHITELIST THIS URL**", # URL
                                    "messenger_extensions": "true",
                                    "webview_height_ratio": "full"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    # w_message = "Hi there! How may I help you?"

    fb_message_template(extra_data["attachment"], "****RECIEVER ID****")

启用其他功能

import requests

# // Importing User Defined Modules // #
from get_environ_var import get_environ_var

# // Global vars // #
ACCESS_TOKEN = "FB_ACCESS_TOKEN"


def fb_message_template(extra_data, sender_id):
    """This function sends template message to facebook"""

    data = {
        'recipient': {'id': sender_id},
        'message': {
            "attachment": extra_data
        }
    }

    qs = 'access_token=' + ACCESS_TOKEN

    resp = requests.post('https://graph.facebook.com/v2.6/me/messages?' + qs, json=data)

    print(resp.content)