Twillio multiple numbers WhatsApp

与世无争的帅哥 提交于 2019-12-13 03:55:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!