-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler.go
502 lines (415 loc) · 12.8 KB
/
handler.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// DailyGakki - handler
// 2020-10-17 14:03
// Benny <[email protected]>
package main
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
"path/filepath"
"strconv"
"strings"
)
import "github.com/tgbot-collection/tgbot_ping"
func startHandler(m *tb.Message) {
caption := "欢迎来到每日最可爱的 Gakki😄\n我会每天定时为你发送最可爱的 Gakki!"
filename := "start.gif"
log.Infof("Start command: %d", m.Chat.ID)
_ = b.Notify(m.Chat, tb.UploadingPhoto)
data, _ := Asset(filepath.Join("images", filename))
log.Debugf("Find %s from memory...", filename)
p := &tb.Animation{File: tb.FromReader(bytes.NewReader(data)), FileName: filename, Caption: caption}
_, err := b.Send(m.Chat, p)
if err != nil {
log.Warnf("%s send failed %v", filename, err)
}
}
func aboutHandler(m *tb.Message) {
caption := "你老婆没啦!\n" +
"开发者:@BennyThink\n" +
"GitHub: https://github.com/tgbot-collection/DailyGakki \n" +
"Google Photos 地址:" + album
filename := "about.gif"
log.Infof("About command: %d", m.Chat.ID)
_ = b.Notify(m.Chat, tb.UploadingPhoto)
data, _ := Asset(filepath.Join("images", filename))
log.Debugf("Find %s from memory...", filename)
p := &tb.Animation{File: tb.FromReader(bytes.NewReader(data)), FileName: filename, Caption: caption}
_, err := b.Send(m.Chat, p)
if err != nil {
log.Warnf("%s send failed %v", filename, err)
}
}
func newHandler(m *tb.Message) {
log.Infof("New command: %d", m.Chat.ID)
// 默认发送3张
_ = b.Notify(m.Chat, tb.Typing)
sendAlbum := generatePhotos()
_ = b.Notify(m.Chat, tb.UploadingPhoto)
_, _ = b.SendAlbum(m.Chat, sendAlbum)
log.Debugf("Album has been sent to %d", m.Chat.ID)
}
func settingsHandler(m *tb.Message) {
if !permissionCheck(m) {
return
}
log.Infof("Settings command: %d", m.Chat.ID)
_ = b.Notify(m.Chat, tb.Typing)
// send out push time
var btns []tb.Btn
var selector = &tb.ReplyMarkup{}
add := selector.Data("增加推送时间", "AddPushStep1")
modify := selector.Data("修改推送时间", "ModifyPush")
btns = append(btns, add, modify)
selector.Inline(
selector.Row(btns...),
)
_ = b.Notify(m.Chat, tb.Typing)
pushTimeStr := strings.Join(getPushTime(m.Chat.ID), " ")
log.Infof("Push time is %s ...", pushTimeStr)
if pushTimeStr == "" {
message := fmt.Sprintf("哼假粉😕,都没有 /subscribe 还想看!")
_, _ = b.Send(m.Chat, message)
} else {
message := fmt.Sprintf("你目前的推送时间有:%s,你想要增加还是删除?", pushTimeStr)
_, _ = b.Send(m.Chat, message, selector)
}
}
func channelHandler(m *tb.Message) {
log.Infof("Channel message Handler: %d from %s", m.Chat.ID, m.Chat.Type)
me := b.Me.Username
switch m.Text {
case "/start" + "@" + me:
startHandler(m)
case "/about" + "@" + me:
aboutHandler(m)
case "/new" + "@" + me:
newHandler(m)
case "/settings" + "@" + me:
settingsHandler(m)
case "/subscribe" + "@" + me:
subHandler(m)
case "/unsubscribe" + "@" + me:
unsubHandler(m)
case "/status" + "@" + me:
statusHandler(m)
case "/submit" + "@" + me:
submitHandler(m)
case "/ping" + "@" + me:
pingHandler(m)
default:
log.Warnf("Oops. %s is not a command. Ignore it.", m.Text)
}
}
func subHandler(m *tb.Message) {
// check permission first
if !permissionCheck(m) {
return
}
caption := "已经订阅成功啦!将在每晚18:11准时为你推送最可爱的 Gakki!如有需要可在 /settings 中更改时间和频率"
filename := "sub.gif"
log.Infof("Sub command: %d", m.Chat.ID)
_ = b.Notify(m.Chat, tb.UploadingPhoto)
data, _ := Asset(filepath.Join("images", filename))
log.Debugf("Find %s from memory...", filename)
p := &tb.Animation{File: tb.FromReader(bytes.NewReader(data)), FileName: filename, Caption: caption}
_, err := b.Send(m.Chat, p)
if err != nil {
log.Warnf("%s send failed %v", filename, err)
}
addInitSub(m.Chat.ID)
}
func permissionCheck(m *tb.Message) bool {
// private and channel: allow
// group: check admin
// in channel there's no m.Sender . In channel bot is always admin
var canSubscribe = false
if m.Private() || m.Chat.Type == "channel" {
canSubscribe = true
} else {
admins, _ := b.AdminsOf(m.Chat)
for _, admin := range admins {
if admin.User.ID == m.Sender.ID {
canSubscribe = true
}
}
}
log.Infof("User %d on %s permission is %v", m.Chat.ID, m.Chat.Type, canSubscribe)
//
if !canSubscribe {
log.Warnf("Denied subscribe request for: %d", m.Sender.ID)
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, "ええ😉只有管理员才能进行设置哦")
return false
}
return true
}
func unsubHandler(m *tb.Message) {
if !permissionCheck(m) {
return
}
caption := "Gakki 已经不是你老婆了😒"
filename := "unsub.gif"
log.Infof("Unsub command: %d", m.Chat.ID)
_ = b.Notify(m.Chat, tb.UploadingPhoto)
data, _ := Asset(filepath.Join("images", filename))
log.Debugf("Find %s from memory...", filename)
p := &tb.Animation{File: tb.FromReader(bytes.NewReader(data)), FileName: filename, Caption: caption}
_, err := b.Send(m.Chat, p)
if err != nil {
log.Warnf("%s send failed %v", filename, err)
}
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, "😭")
// 读取文件,增加对象,然后写入
remove(m.Chat.ID)
}
func messageHandler(m *tb.Message) {
log.Debugf("Message Handler: %d from %s", m.Chat.ID, m.Chat.Type)
caption := "私は 今でも空と恋をしています。"
var filename string
switch m.Text {
case "😘":
filename = "kiss.gif"
case "😚":
filename = "kiss.gif"
case "😗":
filename = "kiss.gif"
case "❤️":
filename = "heart1.gif"
case "❤️❤️":
filename = "heart2.gif"
case "❤️❤️❤️":
filename = "heart3.gif"
case "🌹":
filename = "rose.gif"
case "🦎":
filename = "lizard.gif"
default:
filename = "default.gif"
return
}
log.Infof("Message Handler: %d from %s,Choose %s for text %s", m.Chat.ID, m.Chat.Type, filename, m.Text)
data, _ := Asset(filepath.Join("images", filename))
_ = b.Notify(m.Chat, tb.UploadingPhoto)
p := &tb.Animation{File: tb.FromReader(bytes.NewReader(data)), FileName: filename, Caption: caption}
_, err := b.Send(m.Chat, p)
if err != nil {
log.Warnf("%s send failed %v", filename, err)
}
}
func pingHandler(m *tb.Message) {
_ = b.Notify(m.Chat, tb.Typing)
info := tgbot_ping.GetRuntime("botsrunner_gakki_1", "Gakki Bot", "html")
_, _ = b.Send(m.Chat, info, &tb.SendOptions{ParseMode: tb.ModeHTML})
}
func statusHandler(m *tb.Message) {
_ = b.Notify(m.Chat, tb.Typing)
currentJSON := readJSON()
var isSub = false
for _, user := range currentJSON {
if user.ChatId == m.Chat.ID {
isSub = true
}
}
if isSub {
_, _ = b.Send(m.Chat, "Gakki 与你同在😄")
} else {
_, _ = b.Send(m.Chat, "还木有每日 Gakki💔")
}
}
func photoHandler(m *tb.Message) {
if !m.Private() {
return
}
if m.Chat.ID == 217778424 {
log.Warnln("Blacklist.")
_, _ = b.Reply(m, "👎")
_ = b.Delete(m)
return
}
userID, _ := strconv.Atoi(reviewer)
mm := tb.Message{
Sender: &tb.User{
ID: userID,
},
}
_ = b.Notify(m.Chat, tb.Typing)
botSent, _ := b.Reply(m, "你的审核已经发出去了……请耐心等待😄")
var btns []tb.Btn
var selector = &tb.ReplyMarkup{}
p1, p2 := botSent.MessageSig()
data := fmt.Sprintf("%v|%v", p1, p2)
approve := selector.Data("Yes", "Yes", data)
deny := selector.Data("No", "No", data)
btns = append(btns, approve, deny)
selector.Inline(
selector.Row(btns...),
)
fwd, err := b.Forward(mm.Sender, m, selector)
if err != nil {
log.Errorln(err)
_, _ = b.Edit(botSent, "呃……由于某种神秘的原因,审核请求发送失败了,你再发一下试试\n"+err.Error())
} else {
_, _ = b.Reply(fwd, "请Review", selector)
}
}
func callbackEntrance(c *tb.Callback) {
log.Infof("Initiating callback data %s from %d", c.Data, c.Sender.ID)
// this callback interacts with requester
switch {
case strings.HasPrefix(c.Data, "\fYes"):
approveCallback(c)
case strings.HasPrefix(c.Data, "\fNo"):
denyCallback(c)
case c.Data == "\fAddPushStep1":
addPushStep1(c)
case strings.HasPrefix(c.Data, "\faddPushStep2SelectTime"):
addPushStep2SelectTime(c)
case strings.HasPrefix(c.Data, "\fModifyPush"):
modifyPushStep1(c)
case strings.HasPrefix(c.Data, "\fmodifyPushStep2SelectTime||"):
modifyPushStep2(c)
}
}
func modifyPushStep2(c *tb.Callback) {
uid := c.Message.Chat.ID
time := strings.Replace(c.Data, "\fmodifyPushStep2SelectTime||", "", -1)
deleteOnePush(uid, time)
_ = b.Respond(c, &tb.CallbackResponse{Text: "删除好了哦!"})
// edit
pushSeries := getPushTime(uid)
var btns []tb.Btn
var selector = &tb.ReplyMarkup{}
for _, v := range pushSeries {
btns = append(btns, selector.Data(v, "modifyPushStep2SelectTime||"+v))
}
selector.Inline(
selector.Row(btns...),
)
_, _ = b.EditReplyMarkup(c.Message, selector)
}
func modifyPushStep1(c *tb.Callback) {
// this could be channel id
pushSeries := getPushTime(c.Message.Chat.ID)
var btns []tb.Btn
var selector = &tb.ReplyMarkup{}
for _, v := range pushSeries {
btns = append(btns, selector.Data(v, "modifyPushStep2SelectTime||"+v))
}
selector.Inline(
selector.Row(btns...),
)
_ = b.Respond(c, &tb.CallbackResponse{Text: "点击按钮即可删除这个时间的推送"})
_, _ = b.Send(c.Message.Chat, "选择要删除的时间", selector)
}
func addPushStep2SelectTime(c *tb.Callback) {
newTime := strings.Replace(c.Data, "\faddPushStep2SelectTime|", "", -1)
// this id could be channel id, not initiator's id
respond, message := addMorePush(c.Message.Chat.ID, newTime)
_ = b.Respond(c, &tb.CallbackResponse{Text: respond})
_, _ = b.Send(c.Message.Chat, message)
}
func addPushStep1(c *tb.Callback) {
// duplicate time, ignore here
var inlineKeys [][]tb.InlineButton
var unique []tb.InlineButton
unique = append(unique, tb.InlineButton{
Unique: fmt.Sprintf("addPushStep2SelectTime|%s", "18:11"),
Text: "18:11",
})
inlineKeys = append(inlineKeys, unique)
var btns []tb.InlineButton
var count = 1
for _, t := range timeSeries() {
if count <= 5 {
var temp = tb.InlineButton{
Unique: fmt.Sprintf("addPushStep2SelectTime|%s", t),
Text: t,
}
btns = append(btns, temp)
count++
} else {
count = 1
inlineKeys = append(inlineKeys, btns)
btns = []tb.InlineButton{}
}
}
_, _ = b.Send(c.Message.Chat, "好的,那你选个时间吧!", &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
}
func getStoredMessage(data string) tb.StoredMessage {
// data Yes|5159|123456789
splits := strings.Split(data, "|")
cid, _ := strconv.ParseInt(splits[2], 10, 64)
botM := tb.StoredMessage{MessageID: splits[1], ChatID: cid}
return botM
}
func approveCallback(c *tb.Callback) {
log.Infof("approve new photos from %s", c.Data)
botM := getStoredMessage(c.Data)
approveAction(c.Message.ReplyTo)
_ = b.Respond(c, &tb.CallbackResponse{Text: "Approved"})
_, _ = b.Edit(botM, "你的图片被接受了😊")
_ = b.Delete(c.Message) // this message
_ = b.Delete(c.Message.ReplyTo) // original message with photo
}
func denyCallback(c *tb.Callback) {
log.Infof("deny new photos from %s", c.Data)
botM := getStoredMessage(c.Data)
_ = b.Respond(c, &tb.CallbackResponse{Text: "Denied"})
_, _ = b.Edit(botM, "你的图片被拒绝了😫")
_ = b.Delete(c.Message) // this message
_ = b.Delete(c.Message.ReplyTo) // original message with photo
}
func approveAction(reviewMessage *tb.Message) {
// this handler interacts with reviewer
photo := reviewMessage.Photo
document := reviewMessage.Document
var filename = ""
var fileobject tb.File
if photo != nil {
filename = photo.UniqueID + ".jpg"
fileobject = photo.File
} else if document != nil {
filename = document.UniqueID + ".jpg"
fileobject = document.File
} else {
return
}
picPath := filepath.Join(photosPath, filename)
log.Infof("Downloading photos to %s", picPath)
err = b.Download(&fileobject, picPath)
if err != nil {
log.Errorln("Download failed", err)
}
}
func submitHandler(m *tb.Message) {
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, "想要向我提交新的图片吗?直接把图片发送给我就可以!单张,多张为一组,转发都可以的!\n"+
"文件和图片的形式发送给bot都可以哦。如有问题可以联系 @BennyThink")
}
func inline(q *tb.Query) {
var urls []string
var web = "https://bot.gakki.photos/"
for _, p := range ChoosePhotos(3) {
urls = append(urls, web+filepath.Base(p))
}
results := make(tb.Results, len(urls)) // []tb.Result
for i, url := range urls {
results[i] = &tb.PhotoResult{
URL: url,
ThumbURL: url,
}
// needed to set a unique string ID for each result
results[i].SetResultID(strconv.Itoa(i))
}
log.Infof("Inline pic %v", urls)
err := b.Answer(q, &tb.QueryResponse{
Results: results,
CacheTime: 60, // a minute
})
if err != nil {
log.Println(err)
}
}