1

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;
Alex Baban
  • 11,312
  • 4
  • 30
  • 44

1 Answers1

2

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.

Alex Baban
  • 11,312
  • 4
  • 30
  • 44