Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 在Facebook Messenger机器人程序中通过一次回发发送多条回复消息_Json_Node.js_Facebook_Facebook Messenger Bot_Facebook Chatbot - Fatal编程技术网

Json 在Facebook Messenger机器人程序中通过一次回发发送多条回复消息

Json 在Facebook Messenger机器人程序中通过一次回发发送多条回复消息,json,node.js,facebook,facebook-messenger-bot,facebook-chatbot,Json,Node.js,Facebook,Facebook Messenger Bot,Facebook Chatbot,我想在Messenger上为单个用户触发的回发发送多个回复。我一直在关注Messenger,但真的找不到如何做到这一点 我的代码结构与他们在网站上给出的教程非常相似,我有一个“handlePostback”函数,它识别收到的回发,并将其与一组预定义的有效负载进行比较,以找到“response”JSON对象。此响应发送给“callSendAPI”,它将此JSON对象转换为将消息发送回Messenger API的基本格式 function handlePostback(sender_psid,rec

我想在Messenger上为单个用户触发的回发发送多个回复。我一直在关注Messenger,但真的找不到如何做到这一点

我的代码结构与他们在网站上给出的教程非常相似,我有一个“handlePostback”函数,它识别收到的回发,并将其与一组预定义的有效负载进行比较,以找到“response”JSON对象。此响应发送给“callSendAPI”,它将此JSON对象转换为将消息发送回Messenger API的基本格式

function handlePostback(sender_psid,receivedPostback)
{ if(payload== 'defined_payload') {
  response = {
  text: 'Some text'
  };
callSendAPI(sender_psid,response);
}

function callSendAPI(sender_psid,response) {
let body = {
recipient: {
id= sender_psid
},
message: response
};
// Followed by code for POST request to the webhook
}
这是基本结构,现在我想发送多条消息作为对一次回发的回复。我做了一些挖掘,发现解决方案可能是创建一个message[]数组。但是我该怎么做呢?因为我的“响应”是通过该函数生成的,消息结构应该是这样的(我认为):


我希望我能解释我的问题,如果我能提供更多细节,请让我知道

不要修改
callSendAPI
函数。在您的
handlePostback
函数调用
callSendAPI
中多次

callsendAPI(sender_psid,response1);
callsendAPI(sender_psid,response2);

问得好。如果您不熟悉Node.js,那么这样做的方式就不太明显,而且Facebook的Send API文档中也没有很好地记录这一点

首先,您可能已经观察到,使用数组发送多条消息的方法行不通。Facebook有一个解决方案,可以通过一个请求发送多达100个API调用,但在我看来,在您的情况下不需要这样做。如果您想了解更多关于它的信息,请查看,您会发现实现与您的不同

一个可行的解决方案是多次调用
callSendAPI
函数但此解决方案有一个主要缺点:无法控制发送消息的实际顺序。例如,如果要发送两条单独的消息,您无法保证哪个消息将首先发送给用户

要解决此问题,您需要链接
callSendAPI
函数,以确保下一个
callSendAPI
调用仅在第一条消息发送后发生。您可以在NodeJS中通过使用回调或承诺来实现这一点。如果你对其中任何一个都不熟悉,你可以阅读回电和承诺

您需要修改
callSendAPI
函数,尤其是将POST请求发送到Facebook的部分。我将使用承诺和模块为您的问题提供解决方案

const fetch=require('node-fetch');
函数handlePostback(发送方_psid,接收方Postback){
如果(有效载荷==“已定义的有效载荷”){
答复={
文本:“一些文本”
};
response2=/…另一个响应
response3=/…另一个响应
callSendAPI(发送方\ psid,响应)。然后(()=>{
返回callSendAPI(发送方\ psid,response2)。然后(()=>{
return callSendAPI(sender_psid,response3);//您可以添加任意数量的调用
});
});
}
}
函数callSendAPI(发送方\ psid,响应){
让主体={
收件人:{
id=发送者\u psid
},
信息:答复
};
const qs='access_token='+encodeURIComponent(FB_PAGE_token);//这里您需要从Facebook添加页面标记
返回获取('https://graph.facebook.com/me/messages?“+qs{
方法:“POST”,
标题:{'Content-Type':'application/json'},
body:JSON.stringify(body),
});

}
我发现下面的链接有助于理清在一次回帖中实现多个响应的方法

就像你说的,数组应该可以工作。创建包含多个响应消息的数组变量

var multipleResponse = {
   messages: [{
      response1
   },
   {
      response2
   }]
};
并将数组变量推送到函数中

function callSendAPI(sender_psid,response) {

    let body = {
         recipient: {
            id= sender_psid
         },
         message: []
    };
    // Followed by code for POST request to the webhook
}
最后将数组推送到函数数组中

body.message.push(multipleResponse.messages);

@克里斯托斯·帕纳吉奥塔科普洛斯。我没有得到我的主要反应,这是链式使用当时。相反,我得到了三次回应。 handlePostback函数=>

// Handles messaging_postbacks events
function handlePostback(sender_psid, received_postback) {
  let response;

  // Get the payload for the postback
  let payload = received_postback.payload;

  // Set the response based on the postback payload
  if (payload === 'fashionTip') {
    response = { "text": getFashionTip() } // calls a function which gives a fashion-tip

  // Send the message to acknowledge the postback
  callSendAPI(sender_psid, response).then(() => {
    return callSendAPI(sender_psid, mainMenuResponse)
  });
// Sends response messages via the Send API
function callSendAPI(sender_psid, response) {
  // construct the message body
  let request_body = {
    "recipient": {
      "id": sender_psid
    },
    "message": response
  }

  // Send the HTTP request to the messenger platform
  request({
    "uri": "https://graph.facebook.com/v2.6/me/messages",
    "qs": {"access_token": PAGE_ACCESS_TOKEN},
    "method": "POST",
    "json": request_body
  }, (err, res, body) => {
    if (!err) {
      console.log("Message sent!");
    } else {
      console.error("Unable to send mesage:" + err);
    }
  });
}
callSendAPI函数=>

// Handles messaging_postbacks events
function handlePostback(sender_psid, received_postback) {
  let response;

  // Get the payload for the postback
  let payload = received_postback.payload;

  // Set the response based on the postback payload
  if (payload === 'fashionTip') {
    response = { "text": getFashionTip() } // calls a function which gives a fashion-tip

  // Send the message to acknowledge the postback
  callSendAPI(sender_psid, response).then(() => {
    return callSendAPI(sender_psid, mainMenuResponse)
  });
// Sends response messages via the Send API
function callSendAPI(sender_psid, response) {
  // construct the message body
  let request_body = {
    "recipient": {
      "id": sender_psid
    },
    "message": response
  }

  // Send the HTTP request to the messenger platform
  request({
    "uri": "https://graph.facebook.com/v2.6/me/messages",
    "qs": {"access_token": PAGE_ACCESS_TOKEN},
    "method": "POST",
    "json": request_body
  }, (err, res, body) => {
    if (!err) {
      console.log("Message sent!");
    } else {
      console.error("Unable to send mesage:" + err);
    }
  });
}

虽然这也许能回答这个问题。链接往往会在一段时间后消失,使得答案毫无用处。请在此说明要点,并将该链接用作参考。嗨,当我实现上述方法时,我得到“TypeError:无法读取未定义的'then'属性”。响应2从未出现,响应1被发送给用户三次。如果我没有看到您的代码,我无法确切知道出了什么问题。我很乐意帮忙:)嗨,我在答案中添加了下面的代码。让我知道这是否足以让您回答。与我的示例不同,我使用“节点获取”来执行POST请求,您使用的是“请求”。两者的区别在于,“节点获取”返回一个承诺,“请求”与回调一起工作并返回void。您需要使用与promise兼容的函数来执行请求,或者promisify“request”函数。不过,如果您想保留相同的api,我建议您转换为“node fetch”或使用“request-promise-native”包。谢谢您,先生,我们能够使用node fetch。虽然不能让请求为它工作。非常感谢。