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

遵循官方文档用whatsapp-web-js发送按钮消息失败求助

问题排查与修复方案

以下是针对你代码中可能导致消息无法发送的问题及修复方式:

  • 变量未声明引发的作用域问题
    代码里button和chatId未用let/const声明,可能造成作用域冲突或全局变量污染,需补充声明:

    const button = new Buttons('Button body',[{body:'bt1'},{body:'bt2'},{body:'bt3'}],'title','footer');
    const chatId = number.substring(1) + "@c.us";
    
  • 未处理异步操作
    client.sendMessage是异步函数,必须用await等待执行完成,否则可能在操作未结束时就跳过流程,导致消息发送失败。同时要包裹try/catch捕获错误,便于排查具体问题:

    client.on('ready', async () => {
        console.log('Client is ready!');
        try {
            const button = new Buttons('Button body',[{body:'bt1'},{body:'bt2'},{body:'bt3'}],'title','footer');
            console.log(button);
            const chatId = number.substring(1) + "@c.us";
            await client.sendMessage(chatId, button);
            console.log("消息发送成功");
        } catch (error) {
            console.error("消息发送失败:", error);
        }
    });
    
  • 号码格式验证
    确保number参数是有效的WhatsApp号码格式(比如带国家码的完整号码,如+1234567890),substring(1)后能得到正确的纯数字部分,避免chatId格式错误。

  • 依赖版本检查
    确认whatsapp-web-js版本支持Buttons组件,若版本过旧可能存在兼容性问题,建议升级到最新稳定版。

内容的提问来源于stack exchange,提问作者E. Zahra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:15:44