Reputation: 2606
My goal is to answer on user message with some delay - 1-5 minutes. But in docs i don't see any ability to set the timeout. Here is my code:
app.post('/sms', async (req, res) => {
const twiml = new MessagingResponse();
const msg = req.body.Body;
const toroMsg = await toroProcess(msg);
twiml.message(toroMsg);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
Upvotes: 2
Views: 335
Reputation: 73047
Twilio developer evangelist here.
There is no way to delay the response to a message within Twilio when responding with TwiML.
Instead you need to control the delay in your application, and use the Twilio REST API to send the message later.
It looks like you are using Express and Node in your question. The simplest way to do this would be to use a setTimeout
like this:
const twilioClient = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
app.post('/sms', async (req, res) => {
const msg = req.body.Body;
const toroMsg = await toroProcess(msg);
setTimeout(() => {
twilioClient.messages.create({
to: req.body.From,
from: req.body.To,
body: toroMsg
})
}, 60 * 1000)
const twiml = new MessagingResponse();
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
Since this relies on the currently running process, you probably want to use something a bit more resilient that won't lose the messages if the process is restarted or crashes. Something like Agenda or Bull.
Let me know if this helps at all.
Upvotes: 6