wechaty / puppet-supports

Wechaty Puppet Services (WPS)
https://wechaty.js.org/docs/puppet-services/
Apache License 2.0
32 stars 4 forks source link

[BUG]WorkPro:wechaty bot运行grpc报错GError: 1 CANCELLED: Call cancelled #536

Open ChuXiaoYi opened 1 month ago

ChuXiaoYi commented 1 month ago

Describe the bug 代码

import { ScanStatus, WechatyBuilder, types, log } from "@juzi/wechaty";
import QrcodeTerminal from "qrcode-terminal";
import readline from "readline"; // 引入 readline 模块

// 创建 readline 接口
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const token = "xxxxxxxxxxx"; // put your token here
const bot = WechatyBuilder.build({
  name: "wechaty-bot",
  puppet: "@juzi/wechaty-puppet-service",
  puppetOptions: {
    token,
    tls: {
      disable: true,
      // currently we are not using TLS since most puppet-service versions does not support it. See: https://github.com/wechaty/puppet-service/issues/160
    },
  },
});

const store = {
  qrcodeKey: "",
};

bot
  .on("scan", (qrcode, status, data) => {
    console.log(`
  ============================================================
  qrcode : ${qrcode}, status: ${status}, data: ${data}
  ============================================================
  `);
    if (status === ScanStatus.Waiting) {
      store.qrcodeKey = getQrcodeKey(qrcode) || "";
      QrcodeTerminal.generate(qrcode, {
        small: true,
      });
    }
  })
  .on(
    "verify-code",
    async (
      id: string,
      message: string,
      scene: types.VerifyCodeScene,
      status: types.VerifyCodeStatus
    ) => {
      // 需要注意的是,验证码事件不是完全即时的,可能有最多10秒的延迟。
      // 这与底层轮询二维码状态的时间间隔有关。
      if (
        status === types.VerifyCodeStatus.WAITING &&
        scene === types.VerifyCodeScene.LOGIN &&
        id === store.qrcodeKey
      ) {
        console.log(
          `receive verify-code event, id: ${id}, message: ${message}, scene: ${types.VerifyCodeScene[scene]} status: ${types.VerifyCodeStatus[status]}`
        );

        // 提示用户输入验证码
        rl.question(
          "Please enter the verification code: ",
          async (verifyCode) => {
            try {
              await bot.enterVerifyCode(id, verifyCode); // 如果没抛错,则说明输入成功,会推送登录事件
              rl.close(); // 关闭 readline 接口
            } catch (e) {
              console.log((e as Error).message);
              // 如果抛错,请根据 message 处理,目前发现可以输错3次,超过3次错误需要重新扫码。
              // 错误关键词: 验证码错误输入错误,请重新输入
              // 错误关键词:验证码错误次数超过阈值,请重新扫码'
              // 目前不会推送 EXPIRED 事件,需要根据错误内容判断
            }
          }
        );
      }
    }
  )
  .on("login", (user) => {
    console.log(`
  ============================================
  user: ${JSON.stringify(user)}, friend: ${user.friend()}, ${user.coworker()}
  ============================================
  `);
  })
  .on("message", (message) => {
    console.log(`new message received: ${JSON.stringify(message)}`);
  })
  .on("error", (err) => {
    console.log(err);
  })
  .on("room-announce", (...args) => {
    console.log(`room announce: ${JSON.stringify(args)}`);
  })
  .on("contact-alias", (...args) => {
    console.log(`contact alias: ${JSON.stringify(args)}`);
  })
  .on("tag", (...args) => {
    console.log(`tag: ${JSON.stringify(args)}`);
  });

bot.start();

const getQrcodeKey = (urlStr: string) => {
  const url = new URL(urlStr);
  return url.searchParams.get("key");
};

报错

meishubao-front git:(main) ✗ npm run build:bot && npm run start:bot

> meishubao-front@0.1.0 build:bot
> tsc -p tsconfig.bot.json

> meishubao-front@0.1.0 start:bot
> node dist/wechaty-bot.js

(node:32304) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
16:29:04 INFO PuppetSkeleton start()
16:29:04 INFO PuppetService start() instanciating GrpcManager ...
16:29:04 INFO PuppetService start() instanciating GrpcManager ... done
16:29:04 INFO PuppetService start() setting up bridge grpc event stream ...
16:29:04 INFO PuppetService start() setting up bridge grpc event stream ... done
16:29:04 INFO PuppetService start() starting grpc manager...
16:29:04 WARN last event was 1722587344.107 seconds ago, will not request event cache 
16:29:04 WARN GrpcManager initClient() TLS: disabled (INSECURE)
16:29:04 INFO GrpcManager startStream() connecting event stream with account 1688856850455358 and seq undefined
GError: 1 CANCELLED: Call cancelled
    at callErrorFromStatus (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
    at Object.onReceiveStatus (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/client.js:193:76)
    at Object.onReceiveStatus (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
    at Object.onReceiveStatus (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
    at /Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/resolving-call.js:129:78
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/client.js:161:32)
    at ServiceClientImpl.start (/Users/a08030288/workd/meishubao-front/node_modules/@grpc/grpc-js/build/src/make-client.js:105:19)
    at node:internal/util:430:21
    at new Promise (<anonymous>)
    at bound  (node:internal/util:416:12)
    at GrpcManager.start (/Users/a08030288/workd/meishubao-front/node_modules/@juzi/wechaty-puppet-service/dist/cjs/src/client/grpc-manager.js:135:32)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async PuppetService.onStart (/Users/a08030288/workd/meishubao-front/node_modules/@juzi/wechaty-puppet-service/dist/cjs/src/client/puppet-service.js:134:9)
    at async PuppetService.start (/Users/a08030288/workd/meishubao-front/node_modules/state-switch/dist/cjs/src/service-ctl/service-ctl.js:65:17)
    at async PuppetService.start (/Users/a08030288/workd/meishubao-front/node_modules/@juzi/wechaty-puppet/dist/cjs/src/mixins/service-mixin.js:21:13) {
  code: 1,
  details: 'Call cancelled'
}
16:29:06 WARN ServiceCtl<PuppetServiceMixin> stop() found that is starting...
16:29:06 WARN ServiceCtl<PuppetServiceMixin> stop() found that is starting, waiting stable ... (max 5 seconds)

Info

To Reproduce

Steps to reproduce the behavior:

  1. 复制下面的命令到package.json
    "build:bot": "tsc -p tsconfig.bot.json",
    "start:bot": "node dist/wechaty-bot.js"
  2. 运行npm run build:bot && npm run start:bot

Screenshots

image
huan commented 1 month ago

Can we take a look at this issue and see what’s the reason caused this issue and how to fix it?

thanks!