|
1 | 1 | package engine
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
| 6 | + "os" |
5 | 7 | "sync/atomic"
|
| 8 | + "time" |
6 | 9 |
|
7 | 10 | "github.com/eatmoreapple/openwechat"
|
8 | 11 | "github.com/yqchilde/pkgs/log"
|
| 12 | + "github.com/yqchilde/pkgs/timer" |
9 | 13 |
|
10 | 14 | "github.com/yqchilde/wxbot/engine/robot"
|
11 | 15 | )
|
@@ -103,9 +107,79 @@ func InitRobot() {
|
103 | 107 | robot.Groups = groups
|
104 | 108 | }
|
105 | 109 |
|
| 110 | + robot.Bot = bot |
| 111 | + go keepalive() |
106 | 112 | bot.Block()
|
107 | 113 | }
|
108 | 114 |
|
| 115 | +func keepalive() { |
| 116 | + task := timer.NewTimerTask() |
| 117 | + _, err := task.AddTaskByFunc("keepalive", "0 0/30 * * * *", func() { |
| 118 | + if robot.Bot.Alive() { |
| 119 | + if checkWhetherNeedToLogin() { |
| 120 | + reloadStorage := &robot.JsonLocalStorage{FileName: "storage.json"} |
| 121 | + if err := robot.Bot.HotLogin(reloadStorage, false); err != nil { |
| 122 | + log.Errorf("热登录续命失败, err: %v", err) |
| 123 | + return |
| 124 | + } |
| 125 | + log.Println("热登录续命成功") |
| 126 | + if err := robot.Bot.DumpHotReloadStorage(); err != nil { |
| 127 | + log.Errorf("热登录数据持久化失败, err: %v", err) |
| 128 | + return |
| 129 | + } |
| 130 | + log.Println("热登录数据持久化成功") |
| 131 | + } |
| 132 | + |
| 133 | + helper, err := robot.Self.FileHelper() |
| 134 | + if err != nil { |
| 135 | + log.Errorf("获取文件助手失败, err: %v", err) |
| 136 | + return |
| 137 | + } |
| 138 | + if _, err := helper.SendText(openwechat.ZombieText); err != nil { |
| 139 | + log.Errorf("Robot保活失败, err: %v", err) |
| 140 | + return |
| 141 | + } |
| 142 | + log.Println("Robot保活成功") |
| 143 | + } |
| 144 | + }) |
| 145 | + if err != nil { |
| 146 | + log.Errorf("NewScheduled add task error: %v", err) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func checkWhetherNeedToLogin() bool { |
| 151 | + storage, err := os.ReadFile("storage.json") |
| 152 | + if err != nil { |
| 153 | + log.Errorf("获取热登录配置失败, err: %v", err) |
| 154 | + return false |
| 155 | + } |
| 156 | + |
| 157 | + var hotLoginData openwechat.HotReloadStorageItem |
| 158 | + err = json.Unmarshal(storage, &hotLoginData) |
| 159 | + if err != nil { |
| 160 | + log.Errorf("unmarshal hot login storage err: %v", err) |
| 161 | + return false |
| 162 | + } |
| 163 | + |
| 164 | + for _, cookies := range hotLoginData.Cookies { |
| 165 | + if len(cookies) <= 0 { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + for _, cookie := range cookies { |
| 170 | + if cookie.Name == "wxsid" { |
| 171 | + gmtLocal, _ := time.LoadLocation("GMT") |
| 172 | + expiresGMTTime, _ := time.ParseInLocation("Mon, 02-Jan-2006 15:04:05 GMT", cookie.RawExpires, gmtLocal) |
| 173 | + expiresLocalTime := expiresGMTTime.In(time.Local) |
| 174 | + overHours := expiresLocalTime.Sub(time.Now().Local()).Hours() |
| 175 | + log.Printf("距离登录失效还剩%v小时", overHours) |
| 176 | + return overHours < 3 |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return false |
| 181 | +} |
| 182 | + |
109 | 183 | func getGroupNicknameByGroupUsername(username string) string {
|
110 | 184 | groups := robot.Groups.SearchByUserName(1, username)
|
111 | 185 | return groups[0].NickName
|
|
0 commit comments