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

基于Wechaty的微信机器人WatchdogAgent重置报错求助

Wechaty机器人WatchdogAgent重置报错解决方法

当使用Wechaty开发的微信机器人长时间无操作时,会触发WatchdogAgent重置并抛出未处理错误,导致程序崩溃,报错信息如下:

WARN PuppetWatchdogAgent start() reset() reason: {"data":"heartbeat@browserbridge ding","timeoutMilliseconds":60000}
node:events:491
      throw er; // Unhandled 'error' event
      ^

GError: WatchdogAgent reset: lastFood: "{\"data\":\"heartbeat@browserbridge ding\",\"timeoutMilliseconds\":60000}"
    at Watchdog.reset (file:///opt/web/wechat-gpt/node_modules/wechaty-puppet/dist/esm/src/agents/watchdog-agent.js:45:39)
    at Watchdog.emit (node:events:513:28)
    at Watchdog.emit (node:domain:489:12)
    at Timeout._onTimeout (file:///opt/web/wechat-gpt/node_modules/watchdog/dist/esm/src/watchdog.js:81:18)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)
Emitted 'error' event on WechatyImpl instance at:
    at WechatyImpl.emit (file:///opt/web/wechat-gpt/node_modules/wechaty/dist/esm/src/wechaty-mixins/gerror-mixin.js:45:26)
    at PuppetWeChat.<anonymous> (file:///opt/web/wechat-gpt/node_modules/wechaty/dist/esm/src/wechaty-mixins/puppet-mixin.js:155:34)
    at PuppetWeChat.emit (node:events:513:28)
    at PuppetWeChat.emit (node:domain:489:12)
    at PuppetWeChat.emit (file:///opt/web/wechat-gpt/node_modules/wechaty-puppet/dist/esm/src/puppet/puppet-skeleton.js:107:22)
    at Watchdog.reset (file:///opt/web/wechat-gpt/node_modules/wechaty-puppet/dist/esm/src/agents/watchdog-agent.js:45:25)
    at Watchdog.emit (node:events:513:28)
    [... lines matching original stack trace ...]
    at listOnTimeout (node:internal/timers:559:17) {
  code: 2,
  details: 'Error: WatchdogAgent reset: lastFood: "{\"data\":\"heartbeat@browserbridge ding\",\"timeoutMilliseconds\":60000}"
' +
    '    at Watchdog.reset (file:///opt/web/wechat-gpt/node_modules/wechaty-puppet/dist/esm/src/agents/watchdog-agent.js:45:39)
' +
    '    at Watchdog.emit (node:events:513:28)
' +
    '    at Watchdog.emit (node:domain:489:12)
' +
    '    at Timeout._onTimeout (file:///opt/web/wechat-gpt/node_modules/watchdog/dist/esm/src/watchdog.js:81:18)
' +
    '    at listOnTimeout (node:internal/timers:559:17)
' +
    '    at processTimers (node:internal/timers:502:7)'
}

解决方法:

  • 调整Watchdog超时阈值
    在初始化Puppet时,通过watchdogTimeoutMilliseconds参数延长超时时间,比如设置为120秒:

    const { PuppetWeChat } = require('wechaty-puppet-wechat');
    const puppet = new PuppetWeChat({
      watchdogTimeoutMilliseconds: 120000
    });
    
  • 定时发送主动心跳
    每隔一段时间执行一个轻量操作(比如获取当前用户信息),让Watchdog检测到机器人活动:

    // 每5分钟触发一次心跳检测
    setInterval(async () => {
      try {
        await bot.currentUser.ready();
        console.log('发送心跳,保持连接活跃');
      } catch (err) {
        console.error('心跳检测失败:', err);
      }
    }, 300000);
    
  • 捕获并处理错误事件
    监听Wechaty的error事件,避免程序崩溃,同时可尝试自动恢复连接:

    bot.on('error', async (err) => {
      console.error('机器人错误:', err);
      if (err.message.includes('WatchdogAgent reset')) {
        // 尝试重启Puppet恢复连接
        await bot.puppet.stop();
        await bot.puppet.start();
        console.log('已尝试重启Puppet恢复连接');
      }
    });
    
  • 更换稳定的Puppet实现
    如果使用的是PuppetWeChat(基于网页版微信),网页版微信本身稳定性较差,可考虑更换为PuppetPadlocal等更稳定的第三方Puppet,这类实现通常有更可靠的心跳机制。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 20:53:18