Twilio Whatsapp:发送多条媒体消息
Twilio Whatsapp: Sending Multiple Media Messages
我正在尝试循环遍历数组以在 Twilio 中按顺序发送多条消息。但在我在 Whatsapp 中的最终输出中,顺序不是连续的。例如,它按以下顺序显示:Image 2 -> Image 1 -> Image 3。我尝试使用 async / await 库,但没有帮助。
我已经尝试了 .reduce,以及带有 await[=27= 的普通 for 循环 ] 在循环内部。
数组:
str.text = ["Image 1", "Image 2", "Image 3"]
str.images = ["https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg", "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", "https://www.computerhope.com/jargon/r/random-dice.jpg", ]
代码:
function sendMsg(img, txt) {
context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: txt,
mediaUrl: img
}).then(message => {
callback();
}).catch(err => callback(err));
}
async function test(str) {
(str.text).reduce(async (previousPromise, value, i) => {
await previousPromise;
return sendMsg(str.images[i], str.text[i])
}, Promise.resolve());
}
request.post({
...
}, function (err, res, body) {
var str = body.data.message;
test(str);
}
看起来你在混合回调,没有正确 returning 事情的承诺。我会改变以下事情
- return 发送消息的承诺
- 在您的
request.post
中添加 await
- 使用
for..of
循环顺序处理它们
像这样
str.text = ["Image 1", "Image 2", "Image 3"]
str.images = ["https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg", "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", "https://www.computerhope.com/jargon/r/random-dice.jpg"];
function sendMsg(img, txt) {
return context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: txt,
mediaUrl: img
}).catch(err => console.log(err));
}
async function test(str) {
for (const [index, value] of str.text) {
await sendMsg(str.images[index], str.text[index]);
}
}
request.post({
...
}, async function (err, res, body) {
var str = body.data.message;
await test(str);
}
var items = [
{
text: "Image 1",
image: "https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg"
},
{
text: "Image 2",
image: "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80"
},
{
text: "Image 3",
image: "https://www.computerhope.com/jargon/r/random-dice.jpg"
}
];
sendMessage(items);
function sendMessage(items) {
// stop condition
if (!items.length) {
// return;
callback(null, '');
}
const currentItem = items.shift();
context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: currentItem.text,
mediaUrl: currentItem.image
})
.then(message => {
setTimeout(sendMessage, 1500, items);
})
.catch(err => {
// return;
callback(err, null);
});
}
如果您需要更多消息间隔时间,请使用 setTimeout(sendMessage, 1500, items);
. 中的 1500
我正在尝试循环遍历数组以在 Twilio 中按顺序发送多条消息。但在我在 Whatsapp 中的最终输出中,顺序不是连续的。例如,它按以下顺序显示:Image 2 -> Image 1 -> Image 3。我尝试使用 async / await 库,但没有帮助。
我已经尝试了 .reduce,以及带有 await[=27= 的普通 for 循环 ] 在循环内部。
数组:
str.text = ["Image 1", "Image 2", "Image 3"]
str.images = ["https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg", "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", "https://www.computerhope.com/jargon/r/random-dice.jpg", ]
代码:
function sendMsg(img, txt) {
context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: txt,
mediaUrl: img
}).then(message => {
callback();
}).catch(err => callback(err));
}
async function test(str) {
(str.text).reduce(async (previousPromise, value, i) => {
await previousPromise;
return sendMsg(str.images[i], str.text[i])
}, Promise.resolve());
}
request.post({
...
}, function (err, res, body) {
var str = body.data.message;
test(str);
}
看起来你在混合回调,没有正确 returning 事情的承诺。我会改变以下事情
- return 发送消息的承诺
- 在您的
request.post
中添加 await
- 使用
for..of
循环顺序处理它们
像这样
str.text = ["Image 1", "Image 2", "Image 3"]
str.images = ["https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg", "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", "https://www.computerhope.com/jargon/r/random-dice.jpg"];
function sendMsg(img, txt) {
return context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: txt,
mediaUrl: img
}).catch(err => console.log(err));
}
async function test(str) {
for (const [index, value] of str.text) {
await sendMsg(str.images[index], str.text[index]);
}
}
request.post({
...
}, async function (err, res, body) {
var str = body.data.message;
await test(str);
}
var items = [
{
text: "Image 1",
image: "https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg"
},
{
text: "Image 2",
image: "https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80"
},
{
text: "Image 3",
image: "https://www.computerhope.com/jargon/r/random-dice.jpg"
}
];
sendMessage(items);
function sendMessage(items) {
// stop condition
if (!items.length) {
// return;
callback(null, '');
}
const currentItem = items.shift();
context.getTwilioClient().messages.create({
to: event.From,
from: 'whatsapp:' + context.WHATSAPP_NUMBER,
body: currentItem.text,
mediaUrl: currentItem.image
})
.then(message => {
setTimeout(sendMessage, 1500, items);
})
.catch(err => {
// return;
callback(err, null);
});
}
如果您需要更多消息间隔时间,请使用
setTimeout(sendMessage, 1500, items);
. 中的 1500