首页 文章

无法在Facebook Messenger平台(Node.js)上发送按钮模板作为响应

提问于
浏览
0

我正在使用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);
    }
  });
};

1 回答

  • 0

    而不是 form ,你应该使用 json .

    看看我在glitch上写的代码

    应该是这样的:

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

相关问题