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

214
Visualizações
Twilio: Delay SMS message while reacting to call

I'm building a twilio function that reacts to incoming phone calls. After a call is received the function should send a SMS to the caller. However, I would like the SMS to be delayed by a couple minutes. Currently, my function looks like this:

exports.handler = function (context, event, callback) {
  // Create a new voice response object
  const client = context.getTwilioClient();
  const client_number = event.From;
  const twilio_number = +xxxx;

    client.messages.create({  // Send SMS
        to: client_number,
        from: twilio_number,
        body: "Hello on SMS"
    }).then(() => {  // When request to send SMS is complete, deal with the caller
        let twiml = new Twilio.twiml.VoiceResponse();
        twiml.say("");   // respond to voice caller
        callback(null, twiml);
    })
  
};

Right now the SMS is sent and then the response to the call is sent. I'm not sure how to delay the message.create. I've been reading this but it doesn't work because I can't deal with the call first and then send the message. https://www.twilio.com/docs/runtime/quickstart/add-delay

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This doesn't work with Twilio Functions check @philnash answer

You have to return the callback first to answer the call, and then in a delayed promise send the SMS

exports.handler = function (context, event, callback) {
    const delay = (delayMS) => new Promise((res) => {
        setTimeout(res, delayMS)
    })

    // Create a new voice response object
    const client = context.getTwilioClient();
    const client_number = event.From;
    const twilio_number = +xxxx;

    let twiml = new Twilio.twiml.VoiceResponse();
    twiml.say("");   // respond to voice caller
    callback(null, twiml);

    return (
        delay(10000)
            .then(() =>
                client.messages.create({  // Send SMS
                    to: client_number,
                    from: twilio_number,
                    body: "Hello on SMS"
                })
            )
            .then(() => console.log("SMS sent"))
    )
}

Hopefully this helps.

Note: delay is an utility function where you pass the amounts of milliseconds you want to wait.

about 4 years ago · Juan Pablo Isaza Relatório

0

Twilio developer evangelist here.

This is not something you can do with just a Twilio Function. While aLittleSalty's answer will work in a situation where your application and event loop continues to run, like in an Express app that you run, but in a Twilio Function once you call the callback method the event loop is terminated so the delay will be cancelled.

Similarly, Twilio Function execution time is limited to 10 seconds, so you can't delay a message by a couple of minutes even if the above would work.

One idea could be that you send the message once the call has completed. Your example code only includes a <Say> so I will work with that, but you could do this in other ways if the call continues differently.

First, update your initial Function to return just the TwiML, but add a <Redirect> after the <Say> so that once the message completes Twilio sends a new webhook.

exports.handler = function (context, event, callback) {
  let twiml = new Twilio.twiml.VoiceResponse();
  twiml.say("");   // respond to voice caller
  twiml.redirect("/after_say");
  callback(null, twiml);
};

Then create another Function at the /after_say path that sends the message and hangs up the call.

exports.handler = function (context, event, callback) {
  // Create a new voice response object
  const client = context.getTwilioClient();
  const client_number = event.From;
  const twilio_number = +xxxx;

  let twiml = new Twilio.twiml.VoiceResponse();
  twiml.hangup();

  client.messages.create({  // Send SMS
    to: client_number,
    from: twilio_number,
    body: "Hello on SMS"
  }).then(() => {
    callback(null, twiml);
  }).catch(error => {
    console.error(error);
    callback(null, twiml); 
  });
}

That's still not going to give you a couple of minutes between the call coming in and the message being sent, but it will hopefully send once the call is complete and be a better experience.

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