This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (149 loc) · 3.93 KB
/
main.go
File metadata and controls
174 lines (149 loc) · 3.93 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"time"
kcard "local/khlcard"
"github.com/jpillora/overseer"
"github.com/jpillora/overseer/fetcher"
"github.com/lonelyevil/kook"
"github.com/lonelyevil/kook/log_adapter/plog"
"github.com/phuslu/log"
"github.com/spf13/viper"
)
// TODO:
// qq 二维码发送至 kook 登录
// qq 接口由 kook stdio 频道控制
var appName string = "QQ Hime"
var buildVersion string = appName + " 0050"
func buildUpdateLog() string {
updateLog := ""
updateLog += "1. 更新协议库等\n"
updateLog += "\n\nHelloWorks-QQ Hime@[GitHub](https://github.com/HelloWorksGroup/Route2QQ-bot)"
return updateLog
}
type IMNode interface {
init()
start() error
stop()
registMsgHandler()
routeMsg2Group(gid string, uid string, msg string)
sendMsg2Group(gid string, msg string)
// sendMsg2Person(uid string, msg string)
sendImg2GroupByBytes(gid string, img []byte)
sendImg2GroupByUrl(gid string, url string)
name() string
}
type handlerRule struct {
matcher string
getter func(ctxCommon *kook.EventDataGeneral, matchs []string, reply func(string) string)
}
func kookLog(markdown string) {
gLog.Info().Msgf("kq-log:%s", markdown)
localTime := time.Now().Local()
strconv.Itoa(localTime.Hour())
strconv.Itoa(localTime.Minute())
strconv.Itoa(localTime.Second())
tstr := fmt.Sprintf("%02d:%02d:%02d", localTime.Hour(), localTime.Minute(), localTime.Second())
fmt.Println("["+tstr+" KOOK LOG]:", markdown)
if stdoutChannel != "0" {
sendMarkdown(stdoutChannel, "`"+tstr+"` "+markdown)
}
}
var gLog log.Logger
func prog(state overseer.State) {
fmt.Printf("App#[%s] start ...\n", state.ID)
GetConfig()
gLog = log.Logger{
Level: log.InfoLevel,
Writer: &log.MultiEntryWriter{
&log.ConsoleWriter{ColorOutput: true},
&log.FileWriter{
Filename: "kq.log",
MaxSize: 512 << 10,
MaxBackups: 16,
LocalTime: true},
},
}
s := kook.New(token, plog.NewLogger(&gLog))
me, _ := s.UserMe()
fmt.Println("ID=" + me.ID)
botID = me.ID
s.AddHandler(markdownMessageHandler)
s.AddHandler(imageMessageHandler)
s.Open()
localSession = s
fmt.Println("KOOK node online.")
qqbotInit()
qqbotStart()
fmt.Println("QQ node online.")
if viper.Get("oldversion").(string) != buildVersion {
go func() {
<-time.After(time.Second * time.Duration(3))
card := kcard.KHLCard{}
card.Init()
card.Card.Theme = "success"
card.AddModule_header(appName + " 热更新完成")
card.AddModule_divider()
card.AddModule_markdown("当前版本号:`" + buildVersion + "`")
card.AddModule_markdown("**更新内容:**\n" + buildUpdateLog())
sendKCard(stdoutChannel, card.String())
}()
}
viper.Set("oldversion", buildVersion)
viper.WriteConfig()
kookLog("系统已完全启动")
msgCache.gc()
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt, overseer.SIGUSR2)
sig := <-sc
if sig == overseer.SIGUSR2 {
kookLog("检测到二进制变更,系统即将进行快速重启")
} else {
kookLog("接收到外部指令,系统即将关闭")
}
beforeShutdown()
fmt.Println("Bot will shutdown after 1 second.")
<-time.After(time.Second * time.Duration(1))
qqbotStop()
// Cleanly close down the KHL session.
s.Close()
}
func main() {
overseer.Run(overseer.Config{
Required: true,
Program: prog,
Fetcher: &fetcher.File{Path: "Route2QQ"},
Debug: false,
})
}
func markdownMessageHandler(ctx *kook.KmarkdownMessageContext) {
if ctx.Extra.Author.Bot {
return
}
switch ctx.Common.TargetID {
case botID:
directMessageHandler(ctx.Common)
case stdoutChannel:
stdinHandler(ctx)
default:
for k, v := range kook2qqRouteMap {
if ctx.Common.TargetID == k {
go kookMsgToQQGroup(ctx, k, v)
}
}
for kookGid, v := range kook2vcRouteMap {
if ctx.Common.TargetID == kookGid {
go kookMsgToVC(ctx, kookGid, v.Url, v.Gid, v.Secret)
}
}
}
}
func imageMessageHandler(ctx *kook.ImageMessageContext) {
if ctx.Extra.Author.Bot {
return
}
imageHandler(ctx)
}