【问题标题】:Indicating a WhatsApp message is received, via the Twilio API, in NodeJS?指示在 NodeJS 中通过 Twilio API 接收到 WhatsApp 消息?
【发布时间】:2020-06-09 16:35:03
【问题描述】:

我们正在创建一项服务,该服务将通过 Twilio 服务接收 WhatsApp 消息。这行得通,但我们的问题是我们不知道如何告诉发件人我们的服务器已经“读取”了消息。消息始终显示为“已发送”且从未“阅读”,即使在回复消息后也是如此。我们查看了documentation,但似乎看不到如何执行此操作。

我们的服务器是用 NodeJS 编写的,并且在 HTTP 端使用 Express。

下面是我们正在使用的代码的等效代码(不是运行示例):

import { twiml } from 'twilio';

const { MessagingResponse } = twiml;

async receiveMessage(req: Request, res: Response, next: NextFunction) {

   const message = req.body;

   // Send back an empty response, we will process asynchronously
   const immediateResponse = new MessagingResponse();
   res.setHeader('content-type', 'text/xml');
   res.send(immediateResponse.toString());

   // TODO indicate message as read

   // Do what ever logic is needed for given message
   const replyMessage = await processMessage(message);

   const messageToTwilio = {
     body: replyMessage,
     from: message.To,
     to: message.From
   };

   const twilioResponse = await this.client.messages.create(messageToTwilio);

   // Record value of twilioResponse in DB
}

谁能建议我应该在 API 中使用什么?

【问题讨论】:

  • 您是否使用 Twilio 标记语言 (TwiML) 进行响应? twilio.com/docs/sms/twiml。我似乎无法复制。
  • res.send 发送一个空的MessagingResponse,然后我们使用 twilio 客户端发回。我们这样做是为了避免对我们的 Twilio 调用超时,因为处理中可能存在延迟。MessagingResponse 没有我们可以看到的任何状态字段。至于“状态回调”,它们仅适用于发送到客户端的消息。

标签: node.js twilio whatsapp


【解决方案1】:

我就这个问题联系了 Twilio,结果发现目前不可能。虽然他们认为这是一个有用的功能,但它目前不是实施的优先事项。

注意,可以通过 status webhook 获取传出消息的传递状态,但无法向远程方指示传入消息已“已读”。

【讨论】:

    猜你喜欢
    • 2020-12-22
    • 2019-09-26
    • 2023-01-24
    • 2015-12-31
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多