Skip to content

Commit 3fab704

Browse files
committed
feat: 增加续命保活
1 parent 8468317 commit 3fab704

File tree

4 files changed

+79
-27
lines changed

4 files changed

+79
-27
lines changed

engine/go.mod

-20
This file was deleted.

engine/robot.go

+74
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package engine
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57
"sync/atomic"
8+
"time"
69

710
"github.com/eatmoreapple/openwechat"
811
"github.com/yqchilde/pkgs/log"
12+
"github.com/yqchilde/pkgs/timer"
913

1014
"github.com/yqchilde/wxbot/engine/robot"
1115
)
@@ -103,9 +107,79 @@ func InitRobot() {
103107
robot.Groups = groups
104108
}
105109

110+
robot.Bot = bot
111+
go keepalive()
106112
bot.Block()
107113
}
108114

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+
109183
func getGroupNicknameByGroupUsername(username string) string {
110184
groups := robot.Groups.SearchByUserName(1, username)
111185
return groups[0].NickName

engine/robot/robot.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package robot
33
import "github.com/eatmoreapple/openwechat"
44

55
var (
6+
Bot *openwechat.Bot
67
Self *openwechat.Self
78
Friends openwechat.Friends
89
Groups openwechat.Groups

go.mod

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ module github.com/yqchilde/wxbot
22

33
go 1.19
44

5-
replace github.com/yqchilde/wxbot/engine v0.0.1 => ./engine
6-
75
require (
6+
github.com/eatmoreapple/openwechat v1.1.14
7+
github.com/gabriel-vasile/mimetype v1.4.1
8+
github.com/pkg/errors v0.9.1
89
github.com/tidwall/gjson v1.14.2
910
github.com/yqchilde/pkgs v0.0.2
10-
github.com/yqchilde/wxbot/engine v0.0.1
11+
gopkg.in/yaml.v3 v3.0.1
1112
)
1213

1314
require (
1415
github.com/arthurkiller/rollingwriter v1.1.3 // indirect
15-
github.com/eatmoreapple/openwechat v1.1.14 // indirect
16-
github.com/gabriel-vasile/mimetype v1.4.1 // indirect
17-
github.com/pkg/errors v0.9.1 // indirect
1816
github.com/robfig/cron v1.1.0 // indirect
1917
github.com/robfig/cron/v3 v3.0.1 // indirect
2018
github.com/tidwall/match v1.1.1 // indirect
@@ -23,5 +21,4 @@ require (
2321
go.uber.org/multierr v1.6.0 // indirect
2422
go.uber.org/zap v1.22.0 // indirect
2523
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
26-
gopkg.in/yaml.v3 v3.0.1 // indirect
2724
)

0 commit comments

Comments
 (0)