Dialogflow自定义有效载荷未在Facebook Messenger中发布消息

时间:2019-11-28 05:35:51

标签: node.js json dialogflow chatbot messenger

我有一个自定义的有效负载消息轮播,这是我在Dialogflow中的意图响应之一。看起来像这样。

{ "facebook":{ 
"attachment":{
 "type":"template",
 "payload":{
    "template_type":"generic",
    "elements":[
       {
          "title": "Welcome!",
"subtitle": "We have the right hat for everyone.We have the right hat for everyone.We have the right hat for everyone.",
"imageUrl": "https://www.stepforwardmichigan.org/wp-content/uploads/2017/03/step-foward-fb-1200x628-house.jpg",
"buttons": [
  {
    "postback": "https://f1948e04.ngrok.io",
    "text": "View Website"
  }, {
    "text": "Start Chatting",
    "postback": "PAYLOAD EXAMPLE"
  }
]
}, {
"title": "Welcome!",
"imageUrl": "https://www.stepforwardmichigan.org/wp-content/uploads/2017/03/step-foward-fb-1200x628-house.jpg",
"subtitle": "We have the right hat for everyone.We have the right hat for everyone.We have the right hat for everyone.",
"buttons": [
  {
    "postback": "https://f1948e04.ngrok.io",
    "text": "View Website"
  }, {
    "text": "Start Chatting",
    "postback": "PAYLOAD EXAMPLE"
  }
]
}
    ]
 }
}
}
}

我在node.js Webhook中使用以下代码来处理消息。

function handleCardMessages(messages, sender) {
let elements = [];
      for (var m = 0; m < messages.length; m++) {
          let message = messages[m];

          let buttons = [];
          for (var b = 0; b < message.card.buttons.length; b++) {
              let isLink = (message.card.buttons[b].postback.substring(0, 4) === 'http');
              let button;
              if (isLink) {
                  button = {
                      "type": "web_url",
                      "title": message.card.buttons[b].text,
                      "url": message.card.buttons[b].postback
                  }
              } else {
                  button = {
                      "type": "postback",
                      "title": message.card.buttons[b].text,
                      "payload": message.card.buttons[b].postback
                  }
              }
              buttons.push(button);
          }


          let element = {
              "title": message.card.title,
              "image_url":message.card.imageUri,
              "subtitle": message.card.subtitle,
              "buttons": buttons
          };
          elements.push(element);
      }
sendGenericMessage(sender, elements);
}

function sendGenericMessage(recipientId, elements) {
var messageData = {
recipient: {
  id: recipientId
},
message: {
  attachment: {
    type: "template",
    payload: {
      template_type: "generic",
      elements: elements
    }
  }
}
};
callSendAPI(messageData);
}

  function callSendAPI(messageData) {
request({
  uri: 'https://graph.facebook.com/v3.2/me/messages',
  qs: {
    access_token: config.FB_PAGE_TOKEN
  },
  method: 'POST',
  json: messageData

}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var recipientId = body.recipient_id;
    var messageId = body.message_id;

    if (messageId) {
      console.log("Successfully sent message with id %s to recipient %s",
      messageId, recipientId);
    } else {
      console.log("Successfully called Send API for recipient %s",
      recipientId);
    }
  } else {
    console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
  }
});
}

因此,每次调用意图时。 Dialogflow应该将自定义有效负载消息发布到用户的Messenger应用。但是,我不知道为什么该消息未发布。

如果我在它的Facebook Messenger响应选项下使用Dialogflow的quickreply / card消息响应选项。一切正常。但是,如果我想使用自定义有效负载发送快速回复/卡片消息,则调用该意图时,该消息不会显示在用户的Messenger中。日志看起来不错。不知道我在做什么错。帮助将不胜感激。

0 个答案:

没有答案