可以通过WhatsApp将同一封邮件发送到多个号码吗?
const accountSid = 'mySid';
const authToken = 'mytoken';
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
from: 'whatsapp:+14155238886',
body: 'Hello there!',
to: 'whatsapp:+xxxxx'
})
.then(message => console.log(message.sid));
这样尝试,但只能识别第一个数字
const accountSid = 'mySid';
const authToken = 'mytoken';
const client = require('twilio')(accountSid, authToken);
const numbers = ['whatsapp:+xxxxx','whatsapp:+xxxxxx'];
client.messages
.create({
from: 'whatsapp:+14155238886',
body: 'hello',
to: numbers
})
.then(message => console.log(message.sid));
module.exports =app;
答案 0 :(得分:2)
使用递归,您可以尝试执行以下操作:
const accountSid = 'mySid';
const authToken = 'mytoken';
const client = require('twilio')(accountSid, authToken);
const numbers = ['whatsapp:+xxxxx', 'whatsapp:+xxxxxx'];
sendMessage(numbers);
function sendMessage(numbers) {
// stop condition
if (!numbers.length) {
console.log("---------------------------------");
return;
}
const currentNumber = numbers.shift();
// send the message
client.messages
.create({
from: 'whatsapp:+14155238886',
to: currentNumber,
body: 'hello'
})
.then(function (message) {
console.log(message.sid + '\n');
sendMessage(numbers);
})
.catch(function (err) {
console.log(err);
});
}
module.exports = app;
通过这种方法,消息将被依次发送。