问题
Is it possible to send the same message to multiple numbers by 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));
This trying like this, but only recognize the first number
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;
回答1:
Using recursion you could try something like this:
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;
Messages will be sent one after another with this approach.
来源:https://stackoverflow.com/questions/58933140/twillio-multiple-numbers-whatsaap