在Facebook Messenger Bots的一次回发中发送多个回复消息 [英] Sending multiple reply messages on single postback in Facebook Messenger Bots

查看:206
本文介绍了在Facebook Messenger Bots的一次回发中发送多个回复消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对Messenger上单个用户触发的回发发送多个回复.我一直在关注Messenger的开发者文档,却找不到真正的解决方法.

I want to send multiple replies for a single user-triggered postback on Messenger. I've been following Messenger's developer documentation and couldn't really find how to do this.

我的代码结构与他们在网站上提供的教程非常相似,我有一个' handlePostback '函数,该函数标识收到的回发并将其与一组预定义的有效负载进行比较以查找"响应" JSON对象.此响应将提供给" callSendAPI ",该响应会将此JSON对象置于将消息发送回Messenger API的基本格式中.

My code structure is very similar to the tutorials they've given on the site, I have a 'handlePostback' function which identifies the received postback and compares it to a set of predefined payloads to find the 'response' JSON object. This response is given to 'callSendAPI' which puts this JSON object into the basic format of sending the message back to the 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 []数组.但是我该怎么做呢?因为我的响应"是通过该函数生成的,并且消息结构应如下所示(我认为):

This being the basic structure, now I want to send multiple messages as a reply to one postback. I did some digging, and I found that the solution might be to create a message [] array. But how do I do this? Because my 'response' is being generated through that function, and the messages structure should look like this (I think):

let body = {
 recipient: {
 id=sender_psid
 },
 messages: [ {
  response1
  },
  {
  response2
  }
 ]
};

希望我能解释我的问题,如果可以提供更多详细信息,请告诉我!

I hope I could explain my question, please let me know if I can provide more details!

推荐答案

很好的问题.如果您不熟悉Node.js,那么做起来的方法就不太明显了,Facebook的Send API文档中对此也没有很好的记录.

Nice question. If you are not familiar with Node.js the way to do it is not too obvious and this is not documented well on Facebook's Send API Documentation.

首先,您可能已经观察到,使用数组发送多条消息的方法行不通. Facebook提供了一种解决方案,可通过一个请求发送多达100个API调用,但我认为您的情况无需这样做.如果您想了解更多信息,请查看批量请求文档,您会发现实现与您的实现不同.

Firstly, your approach of sending multiple messages, using an array, as you may have observed won't work. Facebook has a solution for sending up to 100 API Calls with one request but in my opinion this is not needed in your situation. If you want to find out more about it check out the Batched Request Documentation, you'll find out that the implementation is different than yours.

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

One solution that will work is to call the callSendAPI function multiple times. But this solution has one major drawback: You won't be able to control the actual sequence of the messages sent. For example if you want to send two separate messages, you cannot guarantee which will be sent first to the user.

要解决此问题,您需要以一种方式链接您的callSendAPI函数,以确保仅在已发送第一条消息之后才进行下一个callSendAPI调用.您可以通过使用回调或Promise 在NodeJS中执行此操作.如果您不熟悉它们中的任何一个,则可以阅读进行回调,并进行承诺.

To solve this issue you need to chain your callSendAPI functions in a way that guarantees that the next callSendAPI call will happen only after the first message is already sent. You can do this in NodeJS by using either callbacks or promises. If you are not familiar with either of them, you can read this for callbacks and this for promises.

您需要修改您的callSendAPI函数,尤其是将POST请求发送到Facebook的部分.我将使用 promises 和模块 node-提取.

You'll need to modify your callSendAPI function and especially the part that sends the POST request to Facebook. I will present a solution to your issue by using promises and the module node-fetch.

const fetch = require('node-fetch');

function handlePostback(sender_psid,receivedPostback){ 
  if (payload == 'defined_payload') {
    response = {
      text: 'Some text'
    };
    response2 = //... Another response
    response3 = //... Another response
  callSendAPI(sender_psid,response).then(() => {
    return callSendAPI(sender_psid, response2).then(() => {
      return callSendAPI(sender_psid, response3); // You can add as many calls as you want
      });
   });
  }
}

function callSendAPI(sender_psid,response) {
  let body = {
    recipient: {
      id= sender_psid
    },
    message: response
  };
  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN); // Here you'll need to add your PAGE TOKEN from Facebook
  return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(body),
  });
}

这篇关于在Facebook Messenger Bots的一次回发中发送多个回复消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆