Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda