-
Notifications
You must be signed in to change notification settings - Fork 99
/
message.go
303 lines (240 loc) · 6.01 KB
/
message.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package message
import (
"fmt"
"regexp"
"strings"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)
func All(_ *gotgbot.Message) bool {
return true
}
func FromUserID(id int64) filters.Message {
return func(m *gotgbot.Message) bool {
return m.From.Id == id
}
}
func FromUsername(name string) filters.Message {
return func(m *gotgbot.Message) bool {
return m.From.Username == name
}
}
func ChatUsername(name string) filters.Message {
return func(m *gotgbot.Message) bool {
return m.Chat.Username != "" && m.Chat.Username == name
}
}
func ChatID(id int64) filters.Message {
return func(m *gotgbot.Message) bool {
return m.Chat.Id == id
}
}
func ForwardFromUserID(id int64) filters.Message {
return func(m *gotgbot.Message) bool {
if m.ForwardOrigin == nil {
return false
}
u := m.ForwardOrigin.MergeMessageOrigin().SenderUser
return u != nil && u.Id == id
}
}
func ForwardFromChatID(id int64) filters.Message {
return func(m *gotgbot.Message) bool {
if m.ForwardOrigin == nil {
return false
}
c := m.ForwardOrigin.MergeMessageOrigin().Chat
return c != nil && c.Id == id
}
}
func Regex(p string) (filters.Message, error) {
r, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("failed to compile regex: %w", err)
}
return func(m *gotgbot.Message) bool {
return r.MatchString(m.GetText())
}, nil
}
func Reply(msg *gotgbot.Message) bool {
return msg.ReplyToMessage != nil
}
func ChatType(t string) filters.Message {
return func(m *gotgbot.Message) bool {
return m.Chat.Type == t
}
}
func Private(msg *gotgbot.Message) bool {
return msg.Chat.Type == "private"
}
func Group(msg *gotgbot.Message) bool {
return msg.Chat.Type == "group"
}
func Supergroup(msg *gotgbot.Message) bool {
return msg.Chat.Type == "supergroup"
}
func Channel(msg *gotgbot.Message) bool {
return msg.Chat.Type == "channel"
}
func Business(msg *gotgbot.Message) bool {
return msg.BusinessConnectionId != ""
}
func Forwarded(msg *gotgbot.Message) bool {
return msg.ForwardOrigin != nil
}
func Text(msg *gotgbot.Message) bool {
return msg.Text != ""
}
func HasPrefix(prefix string) filters.Message {
return func(msg *gotgbot.Message) bool {
return strings.HasPrefix(msg.GetText(), prefix)
}
}
func HasSuffix(suffix string) filters.Message {
return func(msg *gotgbot.Message) bool {
return strings.HasSuffix(msg.GetText(), suffix)
}
}
func Contains(contains string) filters.Message {
return func(msg *gotgbot.Message) bool {
return strings.Contains(msg.GetText(), contains)
}
}
func Equal(eq string) filters.Message {
return func(msg *gotgbot.Message) bool {
return msg.GetText() == eq
}
}
func Caption(msg *gotgbot.Message) bool {
return msg.Caption != ""
}
func Command(msg *gotgbot.Message) bool {
ents := msg.GetEntities()
return len(ents) > 0 && ents[0].Type == "bot_command" && ents[0].Offset == 0
}
func Animation(msg *gotgbot.Message) bool {
return msg.Animation != nil
}
func Audio(msg *gotgbot.Message) bool {
return msg.Audio != nil
}
func Document(msg *gotgbot.Message) bool {
return msg.Document != nil
}
func Photo(msg *gotgbot.Message) bool {
return len(msg.Photo) > 0
}
func Sticker(msg *gotgbot.Message) bool {
return msg.Sticker != nil
}
func Video(msg *gotgbot.Message) bool {
return msg.Video != nil
}
func VideoNote(msg *gotgbot.Message) bool {
return msg.VideoNote != nil
}
func Voice(msg *gotgbot.Message) bool {
return msg.Voice != nil
}
func Contact(msg *gotgbot.Message) bool {
return msg.Contact != nil
}
func Dice(msg *gotgbot.Message) bool {
return msg.Dice != nil
}
func DiceValue(msg *gotgbot.Message, val int64) bool {
return msg.Dice != nil && msg.Dice.Value == val
}
func Game(msg *gotgbot.Message) bool {
return msg.Game != nil
}
func Poll(msg *gotgbot.Message) bool {
return msg.Poll != nil
}
func Venue(msg *gotgbot.Message) bool {
return msg.Venue != nil
}
func Location(msg *gotgbot.Message) bool {
return msg.Location != nil
}
func NewChatMembers(msg *gotgbot.Message) bool {
return msg.NewChatMembers != nil
}
func LeftChatMember(msg *gotgbot.Message) bool {
return msg.LeftChatMember != nil
}
func PinnedMessage(msg *gotgbot.Message) bool {
return msg.PinnedMessage != nil
}
func ViaBot(msg *gotgbot.Message) bool {
return msg.ViaBot != nil
}
func Entities(m *gotgbot.Message) bool {
return len(m.Entities) > 0
}
func Entity(entType string) filters.Message {
return func(m *gotgbot.Message) bool {
for _, ent := range m.Entities {
if ent.Type == entType {
return true
}
}
return false
}
}
func CaptionEntities(m *gotgbot.Message) bool {
return len(m.CaptionEntities) > 0
}
func CaptionEntity(entType string) filters.Message {
return func(m *gotgbot.Message) bool {
for _, ent := range m.CaptionEntities {
if ent.Type == entType {
return true
}
}
return false
}
}
func Migrate(msg *gotgbot.Message) bool {
return msg.MigrateFromChatId != 0 || msg.MigrateToChatId != 0
}
func MigrateFrom(msg *gotgbot.Message) bool {
return msg.MigrateFromChatId != 0
}
func MigrateTo(msg *gotgbot.Message) bool {
return msg.MigrateToChatId != 0
}
func ReplyMarkup(msg *gotgbot.Message) bool {
return msg.ReplyMarkup != nil
}
func MediaGroup(msg *gotgbot.Message) bool {
return msg.MediaGroupId != ""
}
func IsAutomaticForward(msg *gotgbot.Message) bool {
return msg.IsAutomaticForward
}
func UsersShared(msg *gotgbot.Message) bool {
return msg.UsersShared != nil
}
func ChatShared(msg *gotgbot.Message) bool {
return msg.ChatShared != nil
}
func Story(msg *gotgbot.Message) bool {
return msg.Story != nil
}
func TopicEdited(msg *gotgbot.Message) bool {
return msg.ForumTopicEdited != nil
}
func TopicCreated(msg *gotgbot.Message) bool {
return msg.ForumTopicCreated != nil
}
func TopicClosed(msg *gotgbot.Message) bool {
return msg.ForumTopicClosed != nil
}
func TopicReopened(msg *gotgbot.Message) bool {
return msg.ForumTopicReopened != nil
}
func TopicAction(msg *gotgbot.Message) bool {
return TopicEdited(msg) || TopicCreated(msg) ||
TopicClosed(msg) || TopicReopened(msg)
}