Wednesday, 19 January 2022

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



from Twilio: Delay SMS message while reacting to call

No comments:

Post a Comment