Home > Software design >  How to send SMS masive in Twilio with Nodejs?
How to send SMS masive in Twilio with Nodejs?

Time:03-17

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

CodePudding user response:

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");
  • Related