如何使用短信挂钩延迟对用户消息的响应?

How to delay response on user message with sms hooks?

我的目标是延迟 1-5 分钟回复用户消息。 但是在文档中我看不到任何设置超时的能力。 这是我的代码:

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());
});

这里是 Twilio 开发人员布道者。

responding with TwiML.

时,无法在 Twilio 中延迟对消息的响应

相反,您需要在应用程序中控制延迟,use the Twilio REST API to send the message 稍后。

您的问题中似乎使用了 Express 和 Node。最简单的方法是使用这样的 setTimeout

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());
});

由于这依赖于当前的 运行 进程,您可能希望使用更具弹性的东西,如果进程重新启动或崩溃时不会丢失消息。类似于 Agenda or Bull.

如果这有帮助,请告诉我。