Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
How to send SMS masive in Twilio with Nodejs?

I would like to know how I can send a message to different numbers. I mean, send SMS as notifications to different numbers in the same String Array. Something like:

body: "Hello Word!"
number:["+2222", "+2222", "+2222"]

Is it possible to do this with twilio?

It should be possible, if it is possible with mail, how is it done with telephone numbers?

I am using nodeJs and had something like:

updated code

    const sendBulkMessages = async(req, res) => {
    let messageBody = req.body;
    let numberList = req.body;
    var numbers = [];
    for (i = 0; i < numberList.length; i++) {
        numbers.push(JSON.stringify({
            binding_type: 'sms',
            address: numberList[i]
        }))
    }


    const notificationOpts = {
        toBinding: numbers,
        body: messageBody,
    };

    const response = await client.notify
        .services(SERVICE_SID)
        .notifications.create(notificationOpts)
        .then(notification => console.log(notification.sid))
        .catch(error => console.log(error));

    console.log(response);

    res.json({
        msg: 'Mensaje enviado correctamente'
    });
}

But it tells me an error that I did not send the body, when clearly I do.

Someone could help me? Please

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It looks like you are trying to send these messages via a post request to an Express application. The issue is that you are not extracting the data from the body of the request correctly.

If your POST request body is a JSON object that looks like this:

{
  "toBinding": ['+222', '+222'],
  "body": 'Hello'
}

Then you will need to make sure you are parsing the body of the request as JSON, normally by adding the Express body parser JSON middleware, and then extract the data from the request body like this:

let messageBody = req.body.body;
let numberList = req.body.toBinding;

Here's the full script:

const sendBulkMessages = async(req, res) => {
    let messageBody = req.body.body;
    let numberList = req.body.toBinding;
    var numbers = [];
    for (i = 0; i < numberList.length; i++) {
        numbers.push(JSON.stringify({
            binding_type: 'sms',
            address: numberList[i]
        }))
    }


    const notificationOpts = {
        toBinding: numbers,
        body: messageBody,
    };

    const response = await client.notify
        .services(SERVICE_SID)
        .notifications.create(notificationOpts)
        .then(notification => console.log(notification.sid))
        .catch(error => console.log(error));

    console.log(response);

    res.json({
        msg: 'Mensaje enviado correctamente'
    });
}

Having inspected your repo, it appears that your function was actually correct, but the way you were exporting and requiring functions wasn't.

You can't export functions like this:

 module.exports = sendMessage, sendMessageWhatsapp, sendBulkMessages;

That only exports the first function, the others are ignored. So I updated it to this:

module.exports = { sendMessage, sendMessageWhatsapp, sendBulkMessages };

This exports an object containing the three functions. Then, when you require the functions, you can't do this:

const sendMessage = require('./message');
const sendMessageWhatsapp = require('./message');
const sendBulkMessages = require('./message');

That requires the same function three times. With the update above, you can now do this:

const {
  sendMessage,
  sendBulkMessages,
  sendMessageWhatsapp,
} = require("./message");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!