Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
My simple "send message" javascript program for Twilio is looping twice, but its only sending a message to the same number, What's missing?

I'm trying to create a simple Javascript program that will send a single message to an array of different numbers. I found a twilio page that got me almost there but it doesn't tell me how to loop. I'm admittedly a programming nubie and found a line of loop code. The program did loop, but it called the same number in the array twice instead of calling the two numbers in the array. Here is the code I was using:

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const twilio = require('twilio')(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers,
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}

Is there another function I need to add in to make it send the message to the second number the second time?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I've never used twilio but it looks like you give the array of numbers in the .create function instead of one number,

try switching "numbers" to "numbers[i]":

for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers[i],
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda