Skip to content

Commit

Permalink
refactor: 对适配器及其依赖进行重构,适配wcf-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Clov614 committed Jan 17, 2025
1 parent ac9fd8b commit 39ff3ab
Show file tree
Hide file tree
Showing 38 changed files with 368 additions and 758 deletions.
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module wechat-demo
module github.com/Clov614/rikka-bot-wechat

go 1.22

Expand All @@ -7,13 +7,13 @@ toolchain go1.22.5
require (
github.com/Clov614/bilibili v0.1.2
github.com/Clov614/go-ai-sdk v0.4.2
github.com/Clov614/wcf-rpc-sdk v0.2.3
github.com/eatmoreapple/openwechat v1.4.10
github.com/gin-gonic/gin v1.10.0
github.com/go-ego/gse v0.80.3
github.com/google/uuid v1.6.0
github.com/robfig/cron/v3 v3.0.0
github.com/rs/zerolog v1.33.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/zwgblue/yaml-encoder v0.0.0-20221226083717-a0bdbda0d998
go.etcd.io/bbolt v1.3.11
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -22,16 +22,19 @@ require (
//replace github.com/eatmoreapple/openwechat => github.com/Clov614/openwechat v1.4.8

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/bytedance/sonic v1.11.9 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/eatmoreapple/env v0.0.0-20230613094802-da1bd2d529d4 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand All @@ -43,10 +46,11 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vcaesar/cedar v0.20.2 // indirect
go.nanomsg.org/mangos/v3 v3.4.2 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.36.2 // indirect
)
31 changes: 21 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/Clov614/bilibili v0.1.2 h1:vi1Ykn5+xgrwfpR/xLgdXvoNdulaZitXP9BOgdy0n+
github.com/Clov614/bilibili v0.1.2/go.mod h1:th+qNf6jfSDH03oCM4s347a+i7IpCgZyNRIHY8AAXe8=
github.com/Clov614/go-ai-sdk v0.4.2 h1:4krlgGbgP4UDBl6YYIc8YBJUWH54q46LOzpWhErSdhA=
github.com/Clov614/go-ai-sdk v0.4.2/go.mod h1:S/GDuJSLlnNU47uEpY2FrN1GdP+A4mDXBQZQ2i6sCzk=
github.com/Clov614/wcf-rpc-sdk v0.2.3 h1:A6M60Z5kB1pJTZeufFakjQJfLob0xEYjZA1wUKUbfh4=
github.com/Clov614/wcf-rpc-sdk v0.2.3/go.mod h1:knw4SYFua+ZlWJI5KmWafx4I5GkC27IQUvuFxOBrKxY=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
Expand All @@ -14,10 +18,13 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eatmoreapple/env v0.0.0-20230613094802-da1bd2d529d4 h1:7OCnZ5Nr7dXrE2A3UGK/E3Y6uDYDvISAwLQZuxtWjLU=
github.com/eatmoreapple/env v0.0.0-20230613094802-da1bd2d529d4/go.mod h1:6FwoAYtdFyNxe5UfWjmRui6WWt3CaRglffRFHCaGTIQ=
github.com/eatmoreapple/openwechat v1.4.10 h1:Wx1+Eulb8yXY7t9J8FCzaLu2tvRPT0leTskdNOsUXj0=
github.com/eatmoreapple/openwechat v1.4.10/go.mod h1:h4m2N8m0XsUKlm7UR8BUGkV89GNuKHCnlGV3J8n9Mpw=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/gdamore/optopia v0.2.0/go.mod h1:YKYEwo5C1Pa617H7NlPcmQXl+vG6YnSSNB44n8dNL0Q=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
Expand All @@ -35,11 +42,13 @@ github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaC
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down Expand Up @@ -69,12 +78,12 @@ github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -95,6 +104,8 @@ github.com/zwgblue/yaml-encoder v0.0.0-20221226083717-a0bdbda0d998 h1:nfgqxY/ewt
github.com/zwgblue/yaml-encoder v0.0.0-20221226083717-a0bdbda0d998/go.mod h1:gDS9Ro20YdMC2SY41VMVcy6PqyVCseFPIX1+symaFww=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
go.nanomsg.org/mangos/v3 v3.4.2 h1:gHlopxjWvJcVCcUilQIsRQk9jdj6/HB7wrTiUN8Ki7Q=
go.nanomsg.org/mangos/v3 v3.4.2/go.mod h1:8+hjBMQub6HvXmuGvIq6hf19uxGQIjCofmc62lbedLA=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
Expand All @@ -104,18 +115,18 @@ golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
77 changes: 16 additions & 61 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package main

import (
"context"
"flag"
"fmt"
"github.com/eatmoreapple/openwechat"
"github.com/Clov614/rikka-bot-wechat/rikkabot"
"github.com/Clov614/rikka-bot-wechat/rikkabot/adapter"
"github.com/Clov614/rikka-bot-wechat/rikkabot/logging"
"github.com/Clov614/rikka-bot-wechat/rikkabot/onebot/httpapi"
wcf "github.com/Clov614/wcf-rpc-sdk"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/skip2/go-qrcode"
"time"
"wechat-demo/rikkabot"
"wechat-demo/rikkabot/adapter"
"wechat-demo/rikkabot/logging"
"wechat-demo/rikkabot/onebot/httpapi"
)

func main() {
// todo 支持自动获取sdk.dll 自动完成注入
// 是否开启调试模式
debugflag := flag.Bool("debug", false, "debug mode")
// 是否开启 http服务
httpMode := flag.Bool("http", false, "http mode")
// 是否开启 rikkabot
botMode := flag.Bool("bot", false, "bot mode(using to start rikkabot and also http can run)")
// 是否打印 qrcode
isPrintQr := flag.Bool("qrcode", false, "qrcode mode")
flag.Parse()
if *debugflag {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
Expand All @@ -41,53 +39,16 @@ func main() {
logging.Fatal("Recovered from panic", 1, map[string]interface{}{"panic": r})
}
}()
ctx := context.Background()
cli := wcf.NewClient(30)
cli.Run(true, false, false) // 运行wcf客户端

bot := openwechat.DefaultBot(openwechat.Desktop)
rbot := rikkabot.GetDefaultBot()

//// 注册登陆二维码回调
//bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
// 注册登陆二维码回调

var isNotUUidCallback bool
bot.UUIDCallback = func(uuid string) {
url := openwechat.GetQrcodeUrl(uuid)
rbot.SetloginUrl(url)
logging.Warn("登录地址: " + url)
if *isPrintQr {
consoleQrCodeand(uuid)
}
isNotUUidCallback = true
// 正向http http上报器
if *httpMode {
httpapi.RunHttp(rbot)
rbot.StartHandleEvent() // 处理事件

rbot.PushLoginNoticeEvent() // 推送登录回调通知
}
}

// 登陆
reloadStorage := openwechat.NewFileHotReloadStorage("storage.json")
defer func() {
err := reloadStorage.Close()
if err != nil {
logging.Fatal("get reload storage err", 1, map[string]interface{}{"err": err.Error()})
}
}()
logging.Warn("请在手机中确认登录 or 扫码登录")
if err := bot.PushLogin(reloadStorage, openwechat.NewRetryLoginOption()); err != nil {
logging.Error("bot.PushLogin() error", map[string]interface{}{"openwechat bot error": err.Error()})
return
}

a := adapter.NewAdapter(bot, rbot)

rbot := rikkabot.NewRikkaBot(ctx, cli)
a := adapter.NewAdapter(ctx, cli, rbot)
a.HandleCovert() // 消息转换
defer a.Close()

// 正向http http上报器
if *httpMode && !isNotUUidCallback {
if *httpMode {
httpapi.RunHttp(rbot)
rbot.StartHandleEvent() // 处理事件
}
Expand All @@ -98,10 +59,10 @@ func main() {

go func() {
for {
if !bot.Alive() {
rbot.PushLogOutNoticeEvent(1101, "open-wechat客户端掉线")
if !cli.IsLogin() {
rbot.PushLogOutNoticeEvent(1101, "微信未登录或掉线")
time.Sleep(1 * time.Second) // 1s 延迟退出
rbot.ExitWithErr(1101, "open-wechat客户端掉线")
rbot.ExitWithErr(1101, "微信未登录或掉线")
return
}
time.Sleep(5 * time.Second)
Expand All @@ -114,9 +75,3 @@ func main() {
logging.WarnWithErr(err, "rikka bot.Block() error")
}
}

func consoleQrCodeand(uuid string) {
url := openwechat.GetQrcodeUrl(uuid)
q, _ := qrcode.New(url, qrcode.Low)
fmt.Println(q.ToString(true))
}
Loading

0 comments on commit 39ff3ab

Please sign in to comment.