【问题标题】:Unable to send button template as response on Facebook Messenger Platform (Node.js)无法在 Facebook Messenger 平台 (Node.js) 上发送按钮模板作为响应
【发布时间】:2017-06-06 20:24:07
【问题描述】:

我正在使用 Node.js 在 Facebook Messenger 平台上开发一个聊天机器人。这是我设置文本响应的功能代码:

const fbReq = request.defaults({
  uri: 'https://graph.facebook.com/me/messages',
  method: 'POST',
  json: true,
  qs: {
    access_token: Config.FB_PAGE_TOKEN
  },
  headers: {
    'Content-Type': 'application/json'
  },
});


const fbMessage = (recipientId, msg, cb) => {
  const opts = {
    form: {
      recipient: {
        id: recipientId,
      },
      message: {
        text: msg,
      },
    },
  };

  fbReq(opts, (err, resp, data) => {
    if (cb) {
      cb(err || data.error && data.error.message, data);
    }
  });
};

我也可以通过这种方式设置图像响应。但是,当我尝试将响应设为按钮模板 (https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template) 时,没有收到任何响应。也不会抛出任何错误。

const fbInfo = (recipientId, cb) => {
  const opts = {
    form: {
      recipient: {
        id: recipientId,
      },
      message: {
        attachment:{
          type:"template",
          text:"Check out our website",
          payload:{
            template_type:"button",
            buttons:[
              {
                type:"web_url",
                url:"https://some.website.com",
                title:"Website"
              }
            ]
          }
        }
      }
    }
  };

  fbReq(opts, (err, resp, data) => {
    if (cb) {
      cb(err || data.error && data.error.message, data);
    }
  });
};

【问题讨论】:

    标签: node.js facebook-messenger-bot


    【解决方案1】:

    您应该使用json,而不是form

    看看我写在glitch上的代码

    应该是这样的:

    request({
        uri: 'https://graph.facebook.com/v2.6/me/messages',
        qs: { access_token: <TOKEN> },
        method: 'POST',
        json: messageData}, ...)
    

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 2016-10-27
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多