-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand_router.go
More file actions
134 lines (119 loc) · 3.32 KB
/
Copy pathcommand_router.go
File metadata and controls
134 lines (119 loc) · 3.32 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
package main
import (
"sort"
"strings"
)
type commandFunc func(*CommandHandler, *MessageContext) string
type CommandHandler struct {
sender ChatSender
commands map[string]commandFunc
commandKeys []string
userdata map[string]any
qtProfileToken string
qtProfileData map[string]any
qtLastOperation string
}
var (
commandRegistry = map[string]commandFunc{
"/绑定账号": (*CommandHandler).bindAccount,
"/绑定帐号": (*CommandHandler).bindAccount,
"/取消绑定": (*CommandHandler).unbindAccount,
"/解除绑定": (*CommandHandler).unbindAccount,
"/获取快照": (*CommandHandler).getSnapshot,
"/查询": (*CommandHandler).searchExams,
"/我的信息": (*CommandHandler).myInfo,
"/考试详情": (*CommandHandler).examInfo,
"/答题卡": (*CommandHandler).getAnswerSheet,
"/科目详情": (*CommandHandler).subjectInfo,
"/错题详情": (*CommandHandler).subjectFalseQuestions,
"/答题详情": (*CommandHandler).subjectAllQuestions,
"/帮助": (*CommandHandler).showHelp,
}
commandRegistryKeys = sortedCommandKeys(commandRegistry)
optionalNormalizer = strings.NewReplacer(
"/", "",
"|", "",
"|", "",
"[", "",
"]", "",
"[", "",
"]", "",
"【", "",
"】", "",
"{", "",
"}", "",
"(", "",
")", "",
"(", "",
")", "",
)
)
func sortedCommandKeys(commands map[string]commandFunc) []string {
commandKeys := make([]string, 0, len(commands))
for key := range commands {
commandKeys = append(commandKeys, key)
}
sort.Slice(commandKeys, func(i, j int) bool { return len(commandKeys[i]) > len(commandKeys[j]) })
return commandKeys
}
func newCommandHandler(sender ChatSender) *CommandHandler {
return &CommandHandler{
sender: sender,
commands: commandRegistry,
commandKeys: commandRegistryKeys,
userdata: map[string]any{},
}
}
func (h *CommandHandler) requireAuth(ctx *MessageContext) (bool, string) {
userdata := opView(ctx.UserID)
if ok, _ := userdata["Return"].(bool); !ok {
logCommandTrace(ctx, "auth.failed", map[string]any{
"reason": "user_not_bound",
})
return false, messageNotBindedAccount
}
h.userdata = userdata
return true, ""
}
func stringOrNA(v any) string {
if s := asString(v); s != "" {
return s
}
return "N/A"
}
func intString(v any) string {
s := asString(v)
return strings.TrimLeft(s, "0")
}
func normalizeOptional(value string) string {
return optionalNormalizer.Replace(value)
}
func (h *CommandHandler) handle(ctx *MessageContext) {
commandKey := ""
for _, key := range h.commandKeys {
if strings.HasPrefix(ctx.Content, key) {
commandKey = key
break
}
}
command := (*CommandHandler).messageNone
if commandKey != "" {
args := strings.TrimSpace(ctx.Content[len(commandKey):])
ctx.Content = strings.TrimSpace(commandKey + " " + args)
command = h.commands[commandKey]
}
responseText := command(h, ctx)
if strings.TrimSpace(responseText) != "" {
h.sender.SendText(messageContext(ctx), ctx, responseText)
}
}
func (h *CommandHandler) messageNone(ctx *MessageContext) string {
// userdata := opView(ctx.UserID)
// if ok, _ := userdata["Return"].(bool); ok {
// return "* 本条消息未触发任何指令。"
// }
return "* 本条消息未触发任何指令。"
}
func (h *CommandHandler) showHelp(ctx *MessageContext) string {
return "* 本机器人使用方法请参考README。"
}