-
Notifications
You must be signed in to change notification settings - Fork 4
/
context.go
160 lines (145 loc) · 3.84 KB
/
context.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
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
package rei
import (
"fmt"
"reflect"
"sync"
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Ctx struct {
Event
State
Caller *TelegramClient
Message *tgba.Message
ma *Matcher
IsToMe bool
}
// decoder 反射获取的数据
type decoder []dec
type dec struct {
index int
key string
}
// decoder 缓存
var decoderCache = sync.Map{}
// Parse 将 Ctx.State 映射到结构体
func (ctx *Ctx) Parse(model interface{}) (err error) {
var (
rv = reflect.ValueOf(model).Elem()
t = rv.Type()
modelDec decoder
)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("parse state error: %v", r)
}
}()
d, ok := decoderCache.Load(t)
if ok {
modelDec = d.(decoder)
} else {
modelDec = decoder{}
for i := 0; i < t.NumField(); i++ {
t1 := t.Field(i)
if key, ok := t1.Tag.Lookup("zero"); ok {
modelDec = append(modelDec, dec{
index: i,
key: key,
})
}
}
decoderCache.Store(t, modelDec)
}
for _, d := range modelDec { // decoder类型非小内存,无法被编译器优化为快速拷贝
rv.Field(d.index).Set(reflect.ValueOf(ctx.State[d.key]))
}
return nil
}
// CheckSession 判断会话连续性
func (ctx *Ctx) CheckSession() Rule {
msg := ctx.Value.(*tgba.Message)
return func(ctx2 *Ctx) bool {
msg2, ok := ctx.Value.(*tgba.Message)
if !ok || msg.From == nil || msg.Chat == nil || msg2.From == nil || msg2.Chat == nil { // 确保无空
return false
}
return msg.From.ID == msg2.From.ID && msg.Chat.ID == msg2.Chat.ID
}
}
// Send 发送消息到对方
//
// c.ChatID = ctx.Message.Chat.ID
// if replytosender {
// c.ReplyToMessageID = ctx.Message.MessageID
// }
func (ctx *Ctx) Send(replytosender bool, c tgba.Chattable) (tgba.Message, error) {
msg := reflect.ValueOf(c).Elem()
msg.FieldByName("ChatID").SetInt(ctx.Message.Chat.ID)
if replytosender {
msg.FieldByName("ReplyToMessageID").SetInt(int64(ctx.Message.MessageID))
}
return ctx.Caller.Send(c)
}
// SendPlainMessage 发送无 entities 文本消息到对方
func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (tgba.Message, error) {
msg := tgba.NewMessage(ctx.Message.Chat.ID, fmt.Sprint(printable...))
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(&msg)
}
// SendMessage 发送富文本消息到对方
func (ctx *Ctx) SendMessage(replytosender bool, text string, entities ...tgba.MessageEntity) (tgba.Message, error) {
msg := &tgba.MessageConfig{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
},
Text: text,
Entities: entities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(msg)
}
// SendPhoto 发送图片消息到对方
func (ctx *Ctx) SendPhoto(file tgba.RequestFileData, replytosender bool, caption string, captionentities ...tgba.MessageEntity) (tgba.Message, error) {
msg := &tgba.PhotoConfig{
BaseFile: tgba.BaseFile{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
},
File: file,
},
Caption: caption,
CaptionEntities: captionentities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(msg)
}
// SendAudio 发送音频消息到对方
func (ctx *Ctx) SendAudio(file tgba.RequestFileData, replytosender bool, caption string, captionentities ...tgba.MessageEntity) (tgba.Message, error) {
msg := tgba.AudioConfig{
BaseFile: tgba.BaseFile{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
},
File: file,
},
Caption: caption,
CaptionEntities: captionentities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(&msg)
}
// Block 匹配成功后阻止后续触发
func (ctx *Ctx) Block() {
ctx.ma.SetBlock(true)
}
// Block 在 pre, rules, mid 阶段阻止后续触发
func (ctx *Ctx) Break() {
ctx.ma.Break = true
}