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

whatsapp-web.js开发机器人:扫码后无法自动打开聊天

WhatsApp机器人扫码验证成功但无法进入聊天界面的问题排查与解决

我使用whatsapp-web.js开发WhatsApp机器人,扫码验证成功后无任何报错,但机器人无法自动进入聊天界面。以下是我的实现代码和package.json配置:

机器人代码

import pkg from 'whatsapp-web.js';
import qrCode from 'qrcode-terminal';

const { Client, LocalAuth } = pkg;

const client = new Client({
  authStrategy: new LocalAuth(),
});

client.on('qr', (qr) => {
  // Generate and scan this code with your phone
  console.log('QR RECEIVED', qr);
  qrCode.generate(qr, { small: true });
});

client.on('ready', () => {
  console.log('Client is ready!');
});

client.on('message', (msg) => {
  const content = msg.body;
  if (content == '!ping') {
    msg.reply('pong');
  }
});

client.initialize();

package.json配置

{
  "name": "leadway_bot",
  "version": "1.0.0",
  "description": "WhatsApp bot by Leadway",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "whatsapp",
    "bot"
  ],
  "type": "module",
  "author": "AbdulAzeez",
  "license": "MIT",
  "dependencies": {
    "nodemon": "^2.0.22",
    "puppeteer": "^20.5.0",
    "qrcode-terminal": "^0.12.0",
    "whatsapp-web.js": "^1.21.0"
  }
}

问题排查与解决方向

  • 版本兼容性调整:whatsapp-web.js v1.21.0与Puppeteer v20.5.0可能存在适配问题,尝试降级Puppeteer到兼容版本:
    npm install puppeteer@19.11.1
    
  • 启用浏览器可视化调试:修改Client配置,显示浏览器窗口,观察页面加载状态:
    const client = new Client({
      authStrategy: new LocalAuth(),
      puppeteer: {
        headless: false, // 显示浏览器窗口
        args: ['--no-sandbox', '--disable-setuid-sandbox']
      }
    });
    
    启动后检查是否有未处理的弹窗、条款确认等操作需要手动完成。
  • 清理会话缓存:删除项目根目录下的.wwebjs_auth文件夹,重新扫码登录,避免旧缓存数据干扰。
  • 网络环境检查:确保网络可正常访问WhatsApp Web服务,部分地区需调整网络配置。
  • 依赖版本更新:尝试升级whatsapp-web.js到最新版本,修复已知兼容性问题:
    npm update whatsapp-web.js
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 10:17:44