如何通过Twilio向多个WhatsApp号码发送相同消息?
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é

