You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Twilio向多个WhatsApp号码发送相同消息?

解决Twilio WhatsApp API批量发送消息的问题

Got it, let's tackle this bulk messaging issue with Twilio's WhatsApp API. The problem you're hitting is straightforward: Twilio's messages.create() method doesn't accept an array for the to parameter—it only processes a single number at a time, which is why only your first recipient gets the message.

Here are two reliable approaches to send the same message to multiple numbers:

方案1:逐个循环发送(简单易实现)

If your recipient list is small, just iterate through the number array and call Twilio's message API one by one. Use async/await to handle the asynchronous operations properly:

const accountSid = 'mySid';
const authToken = 'mytoken';
const client = require('twilio')(accountSid, authToken);

const sendWhatsAppMessage = async (toNumber) => {
  try {
    const message = await client.messages.create({
      from: 'whatsapp:+14155238886',
      body: 'Hello there!',
      to: toNumber
    });
    console.log(`Message sent to ${toNumber}, SID: ${message.sid}`);
  } catch (error) {
    console.error(`Failed to send to ${toNumber}:`, error.message);
  }
};

const bulkSend = async () => {
  const numbers = ['whatsapp:+xxxxx','whatsapp:+xxxxxx'];
  // Send messages one after another (serial execution)
  for (const number of numbers) {
    await sendWhatsAppMessage(number);
    // Optional: Add a short delay to avoid hitting Twilio's rate limits
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
};

bulkSend();
module.exports = app;

方案2:并行发送(效率更高)

For larger recipient lists, use Promise.allSettled to process requests in parallel—it's much faster, and won't let a single failed request break the whole batch:

const accountSid = 'mySid';
const authToken = 'mytoken';
const client = require('twilio')(accountSid, authToken);

const sendWhatsAppMessage = (toNumber) => {
  return client.messages.create({
    from: 'whatsapp:+14155238886',
    body: 'Hello there!',
    to: toNumber
  })
  .then(message => ({ number: toNumber, success: true, sid: message.sid }))
  .catch(error => ({ number: toNumber, success: false, error: error.message }));
};

const bulkSend = async () => {
  const numbers = ['whatsapp:+xxxxx','whatsapp:+xxxxxx'];
  // Send all requests in parallel
  const results = await Promise.allSettled(numbers.map(number => sendWhatsAppMessage(number)));
  
  // Process and log results
  results.forEach(result => {
    if (result.status === 'fulfilled') {
      const data = result.value;
      if (data.success) {
        console.log(`Successfully sent to ${data.number}, SID: ${data.sid}`);
      } else {
        console.log(`Failed to send to ${data.number}: ${data.error}`);
      }
    } else {
      console.error(`Request failed: ${result.reason.message}`);
    }
  });
};

bulkSend();
module.exports = app;

关键注意事项

  • Number Format: Make sure all target numbers follow the strict format whatsapp:+[country code][digits only], e.g., whatsapp:+1234567890
  • Rate Limits: Twilio enforces rate limits for WhatsApp API. For large batches, consider using Twilio's Messaging Service to handle rate control automatically, or add intentional delays between requests
  • Error Handling: Always include error catching for bulk sends—this helps you track which messages failed and why, without crashing the entire process

内容的提问来源于stack exchange,提问作者digital and olé

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:43:25