-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtgbot-callbackfunctions.go
221 lines (184 loc) · 7.48 KB
/
tgbot-callbackfunctions.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
package tgbot
import "regexp"
// CommandFn Add a command function, with capture groups and/or named capture groups.
func (bot *TgBot) CommandFn(path string, f func(TgBot, Message, []string, map[string]string) *string) *TgBot {
path = convertToCommand(path)
path = bot.addUsernameCommand(path)
r := regexp.MustCompile(path)
bot.addToConditionalFuncs(TextConditionalCall{RegexCommand{r, f}})
return bot
}
// SimpleCommandFn Add a simple command function.
func (bot *TgBot) SimpleCommandFn(path string, f func(TgBot, Message, string) *string) *TgBot {
path = convertToCommand(path)
path = bot.addUsernameCommand(path)
r := regexp.MustCompile(path)
newf := SimpleCommandFuncStruct{f}
bot.addToConditionalFuncs(TextConditionalCall{RegexCommand{r, newf.CallSimpleCommandFunc}})
return bot
}
// MultiCommandFn add multiples commands with capture groups. Only one of this will be executed.
func (bot *TgBot) MultiCommandFn(paths []string, f func(TgBot, Message, []string, map[string]string) *string) *TgBot {
rc := []*regexp.Regexp{}
for _, p := range paths {
p = convertToCommand(p)
p = bot.addUsernameCommand(p)
r := regexp.MustCompile(p)
rc = append(rc, r)
}
bot.addToConditionalFuncs(TextConditionalCall{MultiRegexCommand{rc, f}})
return bot
}
// RegexFn add a regular expression function with capture groups and/or named capture groups.
func (bot *TgBot) RegexFn(path string, f func(TgBot, Message, []string, map[string]string) *string) *TgBot {
r := regexp.MustCompile(path)
bot.addToConditionalFuncs(TextConditionalCall{RegexCommand{r, f}})
return bot
}
// SimpleRegexFn add a simple regular expression function.
func (bot *TgBot) SimpleRegexFn(path string, f func(TgBot, Message, string) *string) *TgBot {
r := regexp.MustCompile(path)
newf := SimpleCommandFuncStruct{f}
bot.addToConditionalFuncs(TextConditionalCall{RegexCommand{r, newf.CallSimpleCommandFunc}})
return bot
}
// MultiRegexFn add multiples regular expressions with capture groups. Only one will be executed.
func (bot *TgBot) MultiRegexFn(paths []string, f func(TgBot, Message, []string, map[string]string) *string) *TgBot {
rc := []*regexp.Regexp{}
for _, p := range paths {
r := regexp.MustCompile(p)
rc = append(rc, r)
}
bot.addToConditionalFuncs(TextConditionalCall{MultiRegexCommand{rc, f}})
return bot
}
// ImageFn add a function to be called when an image arrives.
func (bot *TgBot) ImageFn(f func(TgBot, Message, []PhotoSize, string)) *TgBot {
bot.addToConditionalFuncs(ImageConditionalCall{f})
return bot
}
// AudioFn add a function to be called when an audio arrives.
func (bot *TgBot) AudioFn(f func(TgBot, Message, Audio, string)) *TgBot {
bot.addToConditionalFuncs(AudioConditionalCall{f})
return bot
}
// VoiceFn add a function to be called when an audio arrives.
func (bot *TgBot) VoiceFn(f func(TgBot, Message, Voice, string)) *TgBot {
bot.addToConditionalFuncs(VoiceConditionalCall{f})
return bot
}
// DocumentFn add a function to be called when a document arrives.
func (bot *TgBot) DocumentFn(f func(TgBot, Message, Document, string)) *TgBot {
bot.addToConditionalFuncs(DocumentConditionalCall{f})
return bot
}
// StickerFn add a function to be called when a sticker arrives.
func (bot *TgBot) StickerFn(f func(TgBot, Message, Sticker, string)) *TgBot {
bot.addToConditionalFuncs(StickerConditionalCall{f})
return bot
}
// VideoFn add a function to be called when a video arrives.
func (bot *TgBot) VideoFn(f func(TgBot, Message, Video, string)) *TgBot {
bot.addToConditionalFuncs(VideoConditionalCall{f})
return bot
}
// LocationFn add a function to be called when a location arrives.
func (bot *TgBot) LocationFn(f func(TgBot, Message, float64, float64)) *TgBot {
bot.addToConditionalFuncs(LocationConditionalCall{f})
return bot
}
// ReplyFn add a function to be called when a message replied other is arrives.
func (bot *TgBot) ReplyFn(f func(TgBot, Message, Message)) *TgBot {
bot.addToConditionalFuncs(RepliedConditionalCall{f})
return bot
}
// ForwardFn add a function to be called when a message forwarding other arrives.
func (bot *TgBot) ForwardFn(f func(TgBot, Message, User, int)) *TgBot {
bot.addToConditionalFuncs(ForwardConditionalCall{f})
return bot
}
// GroupFn add a function to be called in every group message.
func (bot *TgBot) GroupFn(f func(TgBot, Message, int, string)) *TgBot {
bot.addToConditionalFuncs(GroupConditionalCall{f})
return bot
}
// NewParticipantFn add a function to be called when new participant is received.
func (bot *TgBot) NewParticipantFn(f func(TgBot, Message, int, User)) *TgBot {
bot.addToConditionalFuncs(NewParticipantConditionalCall{f})
return bot
}
// LeftParticipantFn add a function to be called when a participant left.
func (bot *TgBot) LeftParticipantFn(f func(TgBot, Message, int, User)) *TgBot {
bot.addToConditionalFuncs(LeftParticipantConditionalCall{f})
return bot
}
// NewTitleChatFn add a function to be called when the title of a group is changed.
func (bot *TgBot) NewTitleChatFn(f func(TgBot, Message, int, string)) *TgBot {
bot.addToConditionalFuncs(NewTitleConditionalCall{f})
return bot
}
// NewPhotoChatFn add a function to be called when the photo of a chat is changed.
func (bot *TgBot) NewPhotoChatFn(f func(TgBot, Message, int, string)) *TgBot {
bot.addToConditionalFuncs(NewPhotoConditionalCall{f})
return bot
}
// DeleteChatPhotoFn add a function to be called when the photo of a chat is deleted.
func (bot *TgBot) DeleteChatPhotoFn(f func(TgBot, Message, int)) *TgBot {
bot.addToConditionalFuncs(DeleteChatPhotoConditionalCall{f})
return bot
}
// GroupChatCreatedFn add a function to be called when a group chat is created.
func (bot *TgBot) GroupChatCreatedFn(f func(TgBot, Message, int)) *TgBot {
bot.addToConditionalFuncs(GroupChatCreatedConditionalCall{f})
return bot
}
// AnyMsgFn add a function to be called in every message :)
func (bot *TgBot) AnyMsgFn(f func(TgBot, Message)) *TgBot {
bot.addToConditionalFuncs(CustomCall{AlwaysReturnTrue, f})
return bot
}
func (bot *TgBot) NotCalledFn(f func(TgBot, Message)) *TgBot {
bot.NoMessageFuncs = append(bot.NoMessageFuncs, NoMessageCall{f})
return bot
}
// CustomFn add a function to be called with a custom conditional function.
func (bot *TgBot) CustomFn(cond func(TgBot, Message) bool, f func(TgBot, Message)) *TgBot {
bot.addToConditionalFuncs(CustomCall{cond, f})
return bot
}
// StartChain will start a chain process, all the functions you add after this will be part of the same chain.
func (bot *TgBot) StartChain() *TgBot {
bot.ChainConditionals = append(bot.ChainConditionals, NewChainStructure())
bot.BuildingChain = true
return bot
}
// CancelChainCommand add a special command that cancel the current chain
func (bot *TgBot) CancelChainCommand(path string, f func(TgBot, Message, string) *string) *TgBot {
if !bot.BuildingChain {
return bot
}
if len(bot.ChainConditionals) > 0 {
path = convertToCommand(path)
path = bot.addUsernameCommand(path)
r := regexp.MustCompile(path)
newf := SimpleCommandFuncStruct{f}
bot.ChainConditionals[len(bot.ChainConditionals)-1].
SetCancelCond(TextConditionalCall{RegexCommand{r, newf.CallSimpleCommandFunc}})
}
return bot
}
// LoopChain will make the chain start again when the last action is done.
func (bot *TgBot) LoopChain() *TgBot {
if !bot.BuildingChain {
return bot
}
if len(bot.ChainConditionals) > 0 {
bot.ChainConditionals[len(bot.ChainConditionals)-1].SetLoop(true)
}
return bot
}
// EndChain ends the chain, after this, the functions will be added as always.
func (bot *TgBot) EndChain() *TgBot {
bot.BuildingChain = false
return bot
}