twilio - 如何使用短信 Hook 延迟对用户消息的响应?

标签 twilio twilio-api

我的目标是延迟 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());
});

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

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

关于twilio - 如何使用短信 Hook 延迟对用户消息的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669902/

相关文章:

twilio - 如何将 SMS 消息转发到 URL

node.js - 如何将多个媒体 URL 的数组传递到 Twilio 消息中

ssl - 获取 "EIdHTTPProtocolException with message ' HTTP/1.1 400 BAD REQUEST'”异常

node.js - Twilio 可编程 SMS 未在部署的 Lambda 函数中发送

python - 使用 raspberry-pi3 和 Bluemix 的 IoT 项目。错误: TwilioRestClient has been removed from this version of the library

java - 如何使用 Spark 在服务器上运行 java 程序?

c# - Twilio API - 发送短信时请求过多

twilio - 是否可以将 URL 附加到 MMS 中的 Twilio 图像?

添加 Studio Flow 后,Twilio REST 客户端停止工作?

php - 未捕获的异常 ‘Twilio\Exceptions\TwilioException’,消息为 ‘Unknown domain sid’