wechaty / go-wechaty

Go Wechaty is a Conversational SDK for Chatbot Makers Written in Go
https://github.com/wechaty/go-wechaty-getting-started
Apache License 2.0
489 stars 92 forks source link

使用OnRoomInvite监听入群邀请时报错 #149

Closed deanxv closed 1 year ago

deanxv commented 1 year ago

代码片段如下:

OnRoomInvite(func(context *wechaty.Context, roomInvitation *user.RoomInvitation) {
        //roomInvitation.Accept()
        fmt.Println("监听到入群邀请")
        inviter, _ := roomInvitation.ToJson()
        fmt.Println(inviter)
        contact, _ := roomInvitation.Inviter()
        fmt.Println(contact.Weixin())
    })

打印json正常输出,但获取Inviter报异常 日志如下:

监听到入群邀请
{"id":"3731937474839498588","inviterId":"axvjingdong","topic":"\"百草味十号大街店VIP群1\"","avatar":"http://wx.qlogo.cn/mmcrhead/xldb2qnhJvoffOUt6nianKWZUAksggkGjCdDP4IXPW9oAmTVjXENca3go1qBvt62R1NUARktticeuEzpTEBOSuJclJq5rVfAsZ/0","invitation":"https://support.weixin.qq.com/cgi-bin/mmsupport-bin/addopenimchatroombyinvite?ticket=uC6LJlVR7gAt5ASt","memberCount":0,"memberIdList":null,"timestamp":"2023-06-19T15:22:02+08:00","receiverId":"wxid_ijv94w2facaq12"}
time="2023-06-19 15:22:02.738" level=error msg="panic: runtime error: invalid memory address or nil pointer dereference" module=wechaty
time="2023-06-19 15:22:02.738" level=error msg="goroutine 96 [running]:\nruntime/debug.Stack()\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x7a\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).registerEvent.func1.1()\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:96 +0xf8\npanic({0x35c38e0, 0x49a7970})\n\t/usr/local/go/src/runtime/panic.go:890 +0x262\ngithub.com/wechaty/go-wechaty/wechaty/user.(*Contact).Weixin(0xc00037e660)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/user/contact.go:175 +0x23\nwxbot/wechaty/internal/svc.NewServiceContext.func3(0xc0005fef80, 0xc0003325e0)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/src/wxbot/wechaty/internal/svc/servicecontext.go:46 +0x24e\nreflect.Value.call({0x3595040, 0x39db920, 0x13}, {0x38d3718, 0x4}, {0xc0006b5ef0, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:586 +0xeb4\nreflect.Value.Call({0x3595040, 0x39db920, 0x13}, {0xc0006b5ef0, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:370 +0xb7\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).registerEvent.func1({0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:104 +0x2ef\ngithub.com/wechaty/go-wechaty/wechaty-puppet/events.(*emitter).Emit(0xc0004b03c0, {0x3563fe0, 0x4960728}, {0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet/events/events.go:150 +0x118\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).emit(0xc000000480, 0x5, {0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:205 +0x74\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).initPuppetEventBridge.func10({0xc0004a8760, 0x1, 0x1})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:374 +0x1ac\ngithub.com/wechaty/go-wechaty/wechaty-puppet/events.(*emitter).Emit(0xc000382078, {0x3563fe0, 0x4960728}, {0xc0004a8760, 0x1, 0x1})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet/events/events.go:150 +0x118\ngithub.com/wechaty/go-wechaty/wechaty-puppet-service.(*PuppetService).onGrpcStreamEvent(0xc000334300, 0xc0006b0600)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet-service/puppet_service.go:348 +0x657\ncreated by github.com/wechaty/go-wechaty/wechaty-puppet-service.(*PuppetService).startGrpcStream.func2\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet-service/puppet_service.go:313 +0x49e\n" module=wechaty
dchaofei commented 1 year ago

1、用的什么 puppet? 2、复线问题的操作步骤是什么~?

deanxv commented 1 year ago

1、用的什么 puppet? 2、复线问题的操作步骤是什么~?

  1. padlocal 其中网关docker启动命令为

    docker run -it -d --name wechaty_test -e WECHATY_LOG="verbose" -e WECHATY_PUPPET="wechaty-puppet-padlocal" -e WECHATY_PUPPET_PADLOCAL_TOKEN="puppet_padlocal_602f7dc70c394db1a2fxxxxxxxxxxx" -e WECHATY_PUPPET_SERVER_PORT="8788" -e WECHATY_TOKEN="43b09121-c6b7-456a-a7e8-xxxxxxxxxxx" -e WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_CLIENT="true" -e WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER="true" -p "8788:8788" wechaty/wechaty:latest
  2. 复现步骤: 当群聊人数达到一定人数时,邀请机器人进群会以邀请入群消息的形式发送给机器人,此时OnRoomInvite会监听到并打印如下日志

    监听到入群邀请
    {"id":"3731937474839498588","inviterId":"axvjingdong","topic":"\"百草味十号大街店VIP群1\"","avatar":"http://wx.qlogo.cn/mmcrhead/xldb2qnhJvoffOUt6nianKWZUAksggkGjCdDP4IXPW9oAmTVjXENca3go1qBvt62R1NUARktticeuEzpTEBOSuJclJq5rVfAsZ/0","invitation":"https://support.weixin.qq.com/cgi-bin/mmsupport-bin/addopenimchatroombyinvite?ticket=uC6LJlVR7gAt5ASt","memberCount":0,"memberIdList":null,"timestamp":"2023-06-19T15:22:02+08:00","receiverId":"wxid_ijv94w2facaq12"}
    time="2023-06-19 15:22:02.738" level=error msg="panic: runtime error: invalid memory address or nil pointer dereference" module=wechaty
    time="2023-06-19 15:22:02.738" level=error msg="goroutine 96 [running]:\nruntime/debug.Stack()\n\t/usr/local/go/src/runtime/debug/stack.go:24 +0x7a\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).registerEvent.func1.1()\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:96 +0xf8\npanic({0x35c38e0, 0x49a7970})\n\t/usr/local/go/src/runtime/panic.go:890 +0x262\ngithub.com/wechaty/go-wechaty/wechaty/user.(*Contact).Weixin(0xc00037e660)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/user/contact.go:175 +0x23\nwxbot/wechaty/internal/svc.NewServiceContext.func3(0xc0005fef80, 0xc0003325e0)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/src/wxbot/wechaty/internal/svc/servicecontext.go:46 +0x24e\nreflect.Value.call({0x3595040, 0x39db920, 0x13}, {0x38d3718, 0x4}, {0xc0006b5ef0, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:586 +0xeb4\nreflect.Value.Call({0x3595040, 0x39db920, 0x13}, {0xc0006b5ef0, 0x2, 0x2})\n\t/usr/local/go/src/reflect/value.go:370 +0xb7\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).registerEvent.func1({0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:104 +0x2ef\ngithub.com/wechaty/go-wechaty/wechaty-puppet/events.(*emitter).Emit(0xc0004b03c0, {0x3563fe0, 0x4960728}, {0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet/events/events.go:150 +0x118\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).emit(0xc000000480, 0x5, {0xc000332600, 0x2, 0x2})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:205 +0x74\ngithub.com/wechaty/go-wechaty/wechaty.(*Wechaty).initPuppetEventBridge.func10({0xc0004a8760, 0x1, 0x1})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty/wechaty.go:374 +0x1ac\ngithub.com/wechaty/go-wechaty/wechaty-puppet/events.(*emitter).Emit(0xc000382078, {0x3563fe0, 0x4960728}, {0xc0004a8760, 0x1, 0x1})\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet/events/events.go:150 +0x118\ngithub.com/wechaty/go-wechaty/wechaty-puppet-service.(*PuppetService).onGrpcStreamEvent(0xc000334300, 0xc0006b0600)\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet-service/puppet_service.go:348 +0x657\ncreated by github.com/wechaty/go-wechaty/wechaty-puppet-service.(*PuppetService).startGrpcStream.func2\n\t/Users/edy/Desktop/develop/Golang/GolangProjects/pkg/mod/github.com/wechaty/go-wechaty@v0.4.9/wechaty-puppet-service/puppet_service.go:313 +0x49e\n" module=wechaty
deanxv commented 1 year ago

使用roomInvitation.Accept()自动接收群邀请也会报错,是我哪里配置的问题吗? 代码片段如下:

.OnRoomInvite(func(context *wechaty.Context, roomInvitation *user.RoomInvitation) {
        //roomInvitation.Accept()
        fmt.Println("监听到入群邀请")
        inviter, _ := roomInvitation.ToJson()
        fmt.Println(inviter)
        err := roomInvitation.Accept()
        if err != nil {
            fmt.Println(err)
        }
        //contact, _ := roomInvitation.Inviter()
        //fmt.Println(contact.Weixin())
    })

日志内容如下:

监听到入群邀请
{"id":"1841222569338534363","inviterId":"axvjingdong","topic":"\"百草味十号大街店VIP群1\"","avatar":"http://wx.qlogo.cn/mmcrhead/xldb2qnhJvoffOUt6nianKWZUAksggkGjCdDP4IXPW9oAmTVjXENca3go1qBvt62R1NUARktticeuEzpTEBOSuJclJq5rVfAsZ/0","invitation":"https://support.weixin.qq.com/cgi-bin/mmsupport-bin/addopenimchatroombyinvite?ticket=v0r2noOCSUEpKU4Q","memberCount":0,"memberIdList":null,"timestamp":"2023-06-19T16:20:01+08:00","receiverId":"wxid_ijv94w2facaq12"}
rpc error: code = Unknown desc = VError: [tid:a012f48d] request has been cancelled for reason: SERVER_ERROR: 2 UNKNOWN: [tid:a012f48d] wechat bad request error
    at Request._failAllPendingRequest (/wechaty/node_modules/padlocal-client-ts/src/Request.ts:334:15)
    at ClientDuplexStreamImpl.<anonymous> (/wechaty/node_modules/padlocal-client-ts/src/Request.ts:82:12)
    at ClientDuplexStreamImpl.emit (node:events:527:28)
    at ClientDuplexStreamImpl.emit (node:domain:475:12)
    at Object.onReceiveStatus (/wechaty/node_modules/@grpc/grpc-js/src/client.ts:689:18)
    at Object.onReceiveStatus (/wechaty/node_modules/@grpc/grpc-js/src/client-interceptors.ts:424:48)
    at /wechaty/node_modules/@grpc/grpc-js/src/call-stream.ts:330:24
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
dchaofei commented 1 year ago

我使用这段代码进行测试,一切正常,能成功自动入群并打印微信号

.OnRoomInvite(func(context *wechaty.Context, roomInvitation *user.RoomInvitation) {
        if err := roomInvitation.Accept(); err != nil {
            fmt.Println(err)
            return
        }
        contact, _ := roomInvitation.Inviter()
        fmt.Println(contact.Weixin())
    })

你的场景是用 A 账号邀请机器人 B 进入一个群聊,并且 A 和 机器人 B 是好友关系,我怀疑是 padlocal 还没有把好友 A 账号加载到内存,你试试用 A 账号主动给机器人 B 发送一条普通消息,让机器人 B 收到消息后自动回复一下,看是否成功,如果能成功就再重复下邀请进群操作试试

deanxv commented 1 year ago

我使用这段代码进行测试,一切正常,能成功自动入群并打印微信号

.OnRoomInvite(func(context *wechaty.Context, roomInvitation *user.RoomInvitation) {
      if err := roomInvitation.Accept(); err != nil {
          fmt.Println(err)
          return
      }
      contact, _ := roomInvitation.Inviter()
      fmt.Println(contact.Weixin())
  })

你的场景是用 A 账号邀请机器人 B 进入一个群聊,并且 A 和 机器人 B 是好友关系,我怀疑是 padlocal 还没有把好友 A 账号加载到内存,你试试用 A 账号主动给机器人 B 发送一条普通消息,让机器人 B 收到消息后自动回复一下,看是否成功,如果能成功就再重复下邀请进群操作试试

感谢您的回答,确实是需要先对话一次才可以,问题已解决,多谢!