-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
65 lines (53 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"ChatGPT_WeChat_Bot/handler"
"ChatGPT_WeChat_Bot/logicerr"
"ChatGPT_WeChat_Bot/model"
"context"
"fmt"
"github.com/eatmoreapple/openwechat"
"log"
)
func main() {
config := &model.Config{}
if err := config.Load(); err != nil {
if err == logicerr.ConfigFileNotFoundError {
err = model.InitConfigFile()
if err != nil {
log.Fatal("创建配置文件 config.yml 失败: " + err.Error())
}
log.Println("已创建配置文件 config.yml,请参考文档,填写必要的字段。")
return
}
panic(err)
} else if err := config.Validate(); err != nil {
log.Fatal(err.Error())
}
ctx := context.Background()
ctx = context.WithValue(ctx, model.ConfigKey, config)
var bot *openwechat.Bot
if config.WeChat.DesktopMode != nil && *config.WeChat.DesktopMode {
bot = openwechat.DefaultBot(openwechat.Desktop)
} else {
bot = openwechat.DefaultBot()
}
reloadStorage := openwechat.NewFileHotReloadStorage("wechat_cache.json")
defer reloadStorage.Close()
// 注册登陆二维码回调
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
// 登陆
if err := bot.HotLogin(reloadStorage, openwechat.NewRetryLoginOption()); err != nil {
fmt.Println(err)
return
}
// 获取登陆的用户
self, err := bot.GetCurrentUser()
if err != nil {
fmt.Println(err)
return
}
ctx = context.WithValue(ctx, model.SelfKey, self)
bot.MessageHandler = handler.Default(ctx)
// 阻塞主 goroutine, 直到发生异常或者用户主动退出
bot.Block()
}