/
tgbot.go
311 lines (273 loc) · 8.37 KB
/
tgbot.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
304
305
306
307
308
309
310
311
package tgbot
import (
"fmt"
"sync/atomic"
"net/url"
"path"
"strings"
"github.com/botanio/sdk/go"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/gorelic"
)
const (
baseURL = "https://api.telegram.org/bot%s/%s"
fileURL = "https://api.telegram.org/file/bot%s/%s"
timeout = 60
)
// New creates an instance of a new bot with the token supplied, if it's invalid this method fail with a panic.
func New(token string) *TgBot {
return NewTgBot(token)
}
// NewTgBot creates an instance of a new bot with the token supplied, if it's invalid this method fail with a panic.
func NewTgBot(token string) *TgBot {
bot, err := NewWithError(token)
if err != nil {
panic(err)
}
return bot
}
// NewWithError creates an instance and return possible error
func NewWithError(token string) (*TgBot, error) {
url := fmt.Sprintf(baseURL, token, "%s")
furl := fmt.Sprintf(fileURL, token, "%s")
tgbot := &TgBot{
Token: token,
BaseRequestURL: url,
BaseFileRequestURL: furl,
MainListener: nil,
RelicCfg: nil,
BotanIO: nil,
TestConditionalFuncs: make([]ConditionCallStructure, 0),
NoMessageFuncs: make([]NoMessageCall, 0),
ChainConditionals: make([]*ChainStructure, 0),
BuildingChain: false,
DefaultOptions: DefaultOptionsBot{
CleanInitialUsername: true,
AllowWithoutSlashInMention: true,
},
}
user, err := tgbot.GetMe()
if err != nil {
return nil, err
// panic(err)
} else {
tgbot.FirstName = user.FirstName
tgbot.ID = user.ID
tgbot.Username = *user.Username
}
return tgbot, nil
}
// TgBot basic bot struct that handle all the interaction functions.
type TgBot struct {
Token string
FirstName string
ID int
Username string
BaseRequestURL string
BaseFileRequestURL string
RelicCfg *RelicConfig
BotanIO *botan.Botan
MainListener chan MessageWithUpdateID
LastUpdateID int64
TestConditionalFuncs []ConditionCallStructure
NoMessageFuncs []NoMessageCall
ChainConditionals []*ChainStructure
BuildingChain bool
DefaultOptions DefaultOptionsBot
}
type RelicConfig struct {
Token string
Name string
}
// ProcessAllMsg default message handler that take care of clean the messages, the chains and the action functions.
func (bot TgBot) ProcessAllMsg(msg Message) {
msg = bot.cleanMessage(msg)
// Let's try with and without goroutines here
for _, c := range bot.ChainConditionals {
if c.canCall(bot, msg) {
// c.call(bot, msg)
go c.call(bot, msg)
return
}
if c.UserInChain(msg) {
return
}
}
// execlater := make([]ConditionCallStructure, 0)
executed := false
for _, v := range bot.TestConditionalFuncs {
// if nm, ok := v.(NoMessageCall); ok {
// execlater = append(execlater, nm)
// }
if v.canCall(bot, msg) {
executed = true
v.call(bot, msg)
// go v.call(bot, msg)
}
}
if !executed {
for _, f := range bot.NoMessageFuncs {
f.call(bot, msg)
}
}
}
// MessagesHandler is the default listener, just listen for a channel and call the default message processor
func (bot TgBot) MessagesHandler(Incoming <-chan MessageWithUpdateID) {
for {
input := <-Incoming
go bot.ProcessAllMsg(input.Msg) // go this or not?
}
}
// ProcessMessages will take care about the highest message ID to get updates in the right way. This will call the MainListener channel with a MessageWithUpdateID
func (bot *TgBot) ProcessMessages(messages []MessageWithUpdateID) {
for _, msg := range messages {
// if int64(msg.UpdateID) > bot.LastUpdateID {
// // Add lock
// bot.LastUpdateID = int64(msg.UpdateID)
// }
atomic.CompareAndSwapInt64(&bot.LastUpdateID, bot.LastUpdateID, int64(msg.UpdateID))
if bot.MainListener != nil {
bot.MainListener <- msg
}
}
}
// AddMainListener add the channel as the main listener, this will be called with the messages received.
func (bot *TgBot) AddMainListener(list chan MessageWithUpdateID) {
bot.MainListener = list
}
// GetMessageChannel create a channel and start the default messages handler, you can use this to build your own server listener (just send the MessageWithUpdateID to that channel)
func (bot *TgBot) GetMessageChannel() chan MessageWithUpdateID {
ch := make(chan MessageWithUpdateID)
go bot.MessagesHandler(ch)
return ch
}
// StartMainListener will run a start a new channel and start the default message handler, assigning it to the main listener.
func (bot *TgBot) StartMainListener() {
ch := bot.GetMessageChannel()
bot.AddMainListener(ch)
}
// SimpleStart will start to get updates with the default listener and callbacks (with long-polling getUpdates way)
func (bot *TgBot) SimpleStart() {
bot.StartMainListener()
bot.Start()
}
// StartWithMessagesChannel will start to get updates with your own channel that handle the messages (with long-polling getUpdates way)
func (bot *TgBot) StartWithMessagesChannel(ch chan MessageWithUpdateID) {
go bot.MessagesHandler(ch)
bot.Start()
}
// Start will start the main process (that use the MainListener channel), it uses getUpdates with longs-polling way and handle the ID
func (bot *TgBot) Start() {
if bot.ID == 0 {
fmt.Println("No ID, maybe the token is bad.")
return
}
if bot.MainListener == nil {
fmt.Println("No listener!")
return
}
removedhook := false
for {
updatesList, err := bot.GetUpdates()
if err != nil {
fmt.Println(err)
if !removedhook {
fmt.Println("Removing webhook...")
bot.SetWebhook("")
removedhook = true
}
continue
}
bot.ProcessMessages(updatesList)
}
}
// ServerStart starts a server that listen for updates, if uri parameter is not empty string, it will try to set the proper webhook
// The default server uses Martini classic, and listen in POST /<pathl>/token (The token without the :)
// To listen it runs the Martini.Run() method, that get $HOST and $PORT from the environment, or uses locashost/3000 if not setted.
func (bot *TgBot) ServerStart(uri string, pathl string) {
bot.ServerStartHostPort(uri, pathl, "", "")
}
func (bot *TgBot) ServerStartHostPort(uri string, pathl string, host string, port string) {
if bot.DefaultOptions.RecoverPanic {
defer func() {
if r := recover(); r != nil {
fmt.Printf("There was some panic: %s\n", r)
}
}()
}
tokendiv := strings.Split(bot.Token, ":")
if len(tokendiv) != 2 {
return
}
pathl = path.Join(pathl, fmt.Sprintf("%s%s", tokendiv[0], tokendiv[1]))
if uri != "" {
puri, err := url.Parse(uri)
if err != nil {
fmt.Printf("Bad URL %s", uri)
return
}
nuri, _ := puri.Parse(pathl)
res, error := bot.SetWebhook(nuri.String())
if error != nil {
ec := res.ErrorCode
fmt.Printf("Error setting the webhook: \nError code: %d\nDescription: %s\n", &ec, res.Description)
return
}
}
if bot.MainListener == nil {
bot.StartMainListener()
}
m := martini.Classic()
m.Post(pathl, binding.Json(MessageWithUpdateID{}), func(params martini.Params, msg MessageWithUpdateID) {
if msg.UpdateID > 0 && msg.Msg.ID > 0 {
bot.HandleBotan(msg.Msg)
bot.MainListener <- msg
}
})
if bot.RelicCfg != nil {
gorelic.InitNewrelicAgent(bot.RelicCfg.Token, bot.RelicCfg.Name, false)
m.Use(gorelic.Handler)
}
if host == "" || port == "" {
m.Run()
} else {
m.RunOnAddr(host + ":" + port)
}
}
func (bot TgBot) HandleBotan(msg Message) {
if bot.BotanIO != nil {
id := msg.Chat.ID
name := "other"
if msg.Text != nil {
name = fmt.Sprintf("text:%s", *msg.Text)
}
bot.BotanIO.TrackAsync(id, msg, name, func(a botan.Answer, e []error) {})
}
}
func (bot *TgBot) SetRelicConfig(tok string, name string) *TgBot {
bot.RelicCfg = &RelicConfig{tok, name}
return bot
}
func (bot *TgBot) SetBotanToken(tok string) *TgBot {
t := botan.New(tok)
bot.BotanIO = &t
return bot
}
func (bot TgBot) buildPath(action string) string {
return fmt.Sprintf(bot.BaseRequestURL, action)
}
func (bot TgBot) buildFilePath(path string) string {
return fmt.Sprintf(bot.BaseFileRequestURL, path)
}
// Send start a Send petition to the user/chat cid. See Send* structs (SendPhoto, SendVideo, ...)
func (bot *TgBot) Send(cid int) *Send {
return &Send{cid, bot}
}
// Answer start a Send petition answering the message. See Send* structs (SendPhoto, SendText, ...)
func (bot *TgBot) Answer(msg Message) *Send {
return &Send{msg.Chat.ID, bot}
}
func (bot *TgBot) File(id string) *SendGetFile {
return &SendGetFile{bot, id}
}