-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
125 lines (98 loc) · 3.18 KB
/
handlers.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
package main
import (
"log"
"strconv"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
tb "gopkg.in/tucnak/telebot.v2"
)
func setHandlers() {
botInstance.Handle("/remindme", remindMeHandler)
botInstance.Handle("/cancel", cancelHandler)
botInstance.Handle("/list", listHandler)
botInstance.Handle(tb.OnText, onTextHandler)
botInstance.Handle(tb.OnCallback, callbackHandler)
}
func remindMeHandler(m *tb.Message) {
wait, err := getWaitTime(m.Payload)
if m.Payload == "" || err != nil {
botInstance.Send(m.Chat, "No valid time units found!")
return
}
if m.ReplyTo == nil {
botInstance.Send(m.Chat, "No message to forward!")
return
}
go confirmReminderSet(wait, m.Chat)
go forwardMessageAfterDelay(wait, m.Sender, m.ReplyTo)
}
func cancelHandler(m *tb.Message) {
if privateMessageHelper(m, false) {
return
}
reminders, done := reminderHelper(m)
if !done {
var buttonArray [][]tb.InlineButton
buttonArray = append(buttonArray, []tb.InlineButton{tb.InlineButton{Unique: CallbackCancelAll, Text: "Cancel All"}})
for i := range reminders {
messageText := time.Unix(reminders[i].Timestamp, 0).String()
buttonRow := []tb.InlineButton{tb.InlineButton{Unique: CallbackCancelReminder, Text: messageText, Data: reminders[i].ID.Hex()}}
buttonArray = append(buttonArray, buttonRow)
}
buttonArray = append(buttonArray, []tb.InlineButton{tb.InlineButton{Unique: CallbackAbortCancel, Text: "Abort"}})
botInstance.Send(m.Sender, "Which reminder do you want to cancel?", &tb.ReplyMarkup{InlineKeyboard: buttonArray, ReplyKeyboardRemove: true})
}
}
func listHandler(m *tb.Message) {
if privateMessageHelper(m, false) {
return
}
reminders, done := reminderHelper(m)
if !done {
textArray := []string{"You have " + strconv.Itoa(len(reminders)) + " active reminders:"}
for i := range reminders {
textArray = append(textArray, time.Unix(reminders[i].Timestamp, 0).String())
}
text := strings.Join(textArray, "\n")
botInstance.Send(m.Sender, text)
}
}
// Handles direct forwarded requests
func onTextHandler(m *tb.Message) {
if privateMessageHelper(m, true) {
return
}
log.Println("Private Message!")
if waitingMessage, ok := currentLimboUsers[m.Sender.ID]; ok {
// Already waiting
wait, err := getWaitTime(m.Text)
if err != nil {
botInstance.Send(m.Sender, "No valid match! Aborting...")
} else {
go confirmReminderSet(wait, m.Sender)
go forwardMessageAfterDelay(wait, m.Sender, waitingMessage)
}
delete(currentLimboUsers, m.Sender.ID)
} else {
currentLimboUsers[m.Sender.ID] = m
botInstance.Send(m.Sender, "When should I remind you?")
}
}
func callbackHandler(c *tb.Callback) {
data := strings.TrimSpace(c.Data)
identifiers := strings.Split(data, "|")
unique := identifiers[0]
switch unique {
case CallbackCancelAll:
removeAllUserMessages(c.Sender)
botInstance.Edit(c.Message, "All reminders cancelled!", &tb.ReplyMarkup{})
case CallbackCancelReminder:
idStr := identifiers[1]
id, _ := primitive.ObjectIDFromHex(idStr)
removeMessageFromDB(id)
botInstance.Edit(c.Message, "Reminder cancelled!", &tb.ReplyMarkup{})
case CallbackAbortCancel:
botInstance.Edit(c.Message, "No reminders cancelled", &tb.ReplyMarkup{})
}
}