【发布时间】: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 没有我们可以看到的任何状态字段。至于“状态回调”,它们仅适用于发送到客户端的消息。