You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WhatsApp Cloud API webhook回调延迟问题求助

WhatsApp Cloud API Webhook 延迟触发问题

我正在使用WhatsApp Cloud API,已配置有效且功能正常的webhook URL,但遇到以下问题:

  • 收到商家消息后立即回复时,callback URL无法即时触发,需等待3分钟
  • 回复后等待3分钟再发送消息,webhook则会即时触发

我已按照官方文档要求对所有请求返回200 OK,但未在文档中找到该问题原因,希望实现所有消息都能即时触发webhook。


服务器初始化代码

const fs = require('fs');
const token = process.env.ACCESS_TOKEN;

// Imports dependencies and set up http server
const request = require("request"),
  express = require("express"),
  body_parser = require("body-parser"),
  axios = require("axios").default,
  app = express().use(body_parser.json()); // creates express http server
// Sets server port and logs message on success
var https = require('https');
var credentials = {
    key: fs.readFileSync(process.env.key),
    cert: fs.readFileSync(process.env.cert),
    ca: fs.readFileSync(process.env.ca, 'utf-8')
}
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(process.env.PORT, async () => {
  console.log("Whatsapp server listening to port: "+process.env.PORT);
  // await mysqlConnector.initializeMySql(app);
  // await redis.initializeRedis();
});

Webhook POST 处理方法

app.post("/inbound", async (req, res) => {
  // Parse the request body from the POST
  let body = req.body;

  // Check the Incoming webhook message
  // console.log(JSON.stringify(req.body, null, 2));

  // info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
  if (req.body.object) {
    if (
      body.entry &&
      body.entry[0].changes &&
      body.entry[0].changes[0] &&
      body.entry[0].changes[0].value.messages &&
      body.entry[0].changes[0].value.Messages[0]
    ) {

      res.sendStatus(200);

       //if the business needs to send a text message
      //async function call to send a text message (It is a promise function that makes API call to the /message endpoint of WhatsApp business API)


       // if the business needs to send an interactive message
      //async function call to send interactive message(promise function)


    //if the business needs to send a template
    //async function call to send message template(Promise function)

}
}
}

文本消息发送函数

sendMessage: async function (message, incomingMessageFrom, outgoingMessageFrom, accessToken) {
    return new Promise(async function (resolve, reject) {
      try {
        axios({
          method: "POST",
          url: "https://graph.facebook.com/v16.0/" +
            outgoingMessageFrom +
            "/messages?access_token=" +
            accessToken,
          data: {
            messaging_product: "whatsapp",
            "to": incomingMessageFrom,
            "type": "text",
            "text": {
              "body": String(message)
            }
          },

          headers: { "Content-Type": "application/json" }
        }).then(response => {
          resolve(response.data)
        });
      } catch (error) {
        //log Error
        resolve("error: " + JSON.stringify(error.message));
      }
    })
  }

模板消息发送函数

sendMessageTemplate: async function (templateName, params, incomingMessageFrom, outgoingMessageFrom, accessToken) {
    await this.bindParams(params, templateName);
    return new Promise(async function (resolve, reject) {
      try {
        //call bind params
        let data = {
          "messaging_product": "whatsapp",
          "to": incomingMessageFrom,
          "type": "template",
          "template": templates[templateName]
        }
        let url = "https://graph.facebook.com/v12.0/" +
        outgoingMessageFrom +
        "/messages?access_token=" +
        accessToken;
        axios({
          method: "POST", // Required, HTTP method, a string, e.g. POST, GET
          url:url,
          data: data,
          headers: { "Content-Type": "application/json" },
        }).then(response => {
          resolve(response.data)
        }).catch(err =>{
          appLogger.logMessage("error","Failed to call template api due to: "+JSON.stringify(err.message),className,"sendMessageTemplate","ADMIN","Server",moduleName);
        });

        appLogger.logMessage("debug","The request data send is: "+JSON.stringify(data),className,"sendMessageTemplate","ADMIN","SERVER",moduleName);
        appLogger.logMessage("debug","The request url send is: "+url,className,"sendMessageTemplate","ADMIN","SERVER",moduleName);
      } catch (error) {
        appLogger.logMessage("error","error occured while sending message template: "+JSON.stringify(error.message),"sendMessageTemplate","ADMIN","SERVER",moduleName);
        //logError
        resolve("error: " + JSON.stringify(error.message));
      }
    })
  }

排查与解决方案建议

  1. 统一API版本
    文本消息使用v16.0,模板消息使用v12.0,版本差异可能导致平台处理逻辑异常,建议统一升级到最新稳定版API(如v18.0)。

  2. 检查节流与重试机制
    WhatsApp Cloud API存在消息频率限制,短时间内双向消息过于频繁会触发临时延迟推送。控制回复频率,避免收到消息后立即发送多条内容。

  3. 启用Webhook签名验证
    未验证签名可能降低平台对请求的信任度,触发延迟。按照官方文档配置签名验证,确保请求来自官方平台。

  4. 优化Webhook响应逻辑
    当前先返回200再处理消息的逻辑没问题,但需确保后续消息发送逻辑完全异步,避免任何阻塞操作影响平台后续推送。

  5. 服务器网络排查
    测试服务器到WhatsApp API服务器的网络延迟,排查是否存在丢包、高延迟问题,必要时切换服务器节点。

  6. 详细日志分析
    记录所有webhook请求的时间戳、payload内容,对比延迟触发和正常触发的请求差异,定位特定触发条件。

内容的提问来源于stack exchange,提问作者prince_29

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:17:01