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

PHP对接WhatsApp API云消息:列表交互无响应问题求助

WhatsApp API列表选项触发位置消息异常排查

问题现象

使用PHP开发WhatsApp API功能时,多数功能正常,但列表消息存在异常:用户选择列表中的地点选项时,无法触发后续位置消息发送;但手动输入地点名称能正常触发。部署在共享cPanel环境,多次重构代码未解决。

相关代码片段

$entry = $req["entry"][0];
$changes = $entry["changes"][0];
$value = $changes["value"];
$objMessage = $value["messages"];
$message = $objMessage[0];
$text = $message["text"]["body"];
$number = $text["from"];


}else if (strpos($text, "list") !== false) {
  $data = json_encode([
    "messaging_product" => "whatsapp",
    "recipient_type" => "individual",
    "to" => $number,
    "type" => "interactive",
    "interactive" => [
      "type" => "list",
      "body" => [
        "text" => "BODY TEXT"
      ],
      "action" => [
        "button" => "BUTTON TEXT",
        "sections" => [
          [
            "title" => " LIST SECTION 1 TITLE",
            "rows" => [
              [
                "id" => "LIST SECTION 1 ROW 1 ID",
                "title" => "location1",
                "description" => "SECTION 1 ROW 1 DESC"
              ],
              [
                "id" => "LIST SECTION 1 ROW 2 ID",
                "title" => "location2",
                "description" => "SECTION 1 ROW 2 DESC"
              ]
            ]
          ],
        ]
      ]
    ]
  ]);
}else if (strpos($text, "location1") !== false) {
  $data = json_encode([
    "messaging_product" => "whatsapp",
    "recipient_type" => "individual",
    "to" => $number,
    "type"=> "location",
    "location"=> [
        "latitude"=> "41.89808194473385",
        "longitude"=> "12.49813575202307",
        "name"=> "Location1 Name",
        "address"=> "Location description"
    ]
  ]);
}else if (strpos($comentario, "location2") !== false) {
  $data = json_encode([
    "messaging_product" => "whatsapp",
    "recipient_type" => "individual",
    "to" => $number,
    "type"=> "location",
    "location"=> [
        "latitude"=> "17.96589758757642",
        "longitude"=> "-76.79451717970731",
        "name"=> "Location2 name",
        "address"=> "Location description"
    ]
  ]);
}

排查修复步骤

  • 区分消息类型处理:用户选择列表选项时,WhatsApp推送的是interactive类型消息,而非text类型。当前代码仅读取$message["text"]["body"],完全忽略了列表交互消息的结构。需先判断消息类型:
    $messageType = $message["type"];
    $triggerValue = '';
    if ($messageType === "text") {
        $triggerValue = $message["text"]["body"];
    } elseif ($messageType === "interactive") {
        // 读取列表选择的标题或ID
        $listReply = $message["interactive"]["list_reply"];
        $triggerValue = $listReply["title"]; // 或使用$listReply["id"]
    }
    
  • 修正变量名错误:最后一个判断分支使用了未定义的$comentario变量,应改为$text(或上面定义的$triggerValue),否则location2的判断永远无法触发。
  • 匹配列表选择的字段:如果使用列表选项的id做判断,需确保代码中判断的值与列表定义的id完全一致(比如"LIST SECTION 1 ROW 1 ID"),避免空格或拼写偏差。
  • 日志调试:在cPanel中开启PHP错误日志,或在代码中添加日志记录,输出完整的$message数据结构,对比列表选择和手动输入消息的格式差异,确认字段读取是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 17:55:56