Twillio 多个号码 WhatsApp [英] Twillio multiple numbers WhatsApp

查看:34
本文介绍了Twillio 多个号码 WhatsApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 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;

推荐答案

使用递归,你可以尝试这样的事情:

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.

这篇关于Twillio 多个号码 WhatsApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆