-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotUtils.go
116 lines (92 loc) · 2.68 KB
/
botUtils.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
package main
import (
"errors"
"log"
"regexp"
"strconv"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
tb "gopkg.in/tucnak/telebot.v2"
)
// botInstance is the global instance of tb.Bot that all functions use
var botInstance *tb.Bot
func getBotPreferences() tb.Settings {
listenPort := ":" + envListenPort
var poller tb.Poller = &tb.Webhook{
Listen: listenPort,
Endpoint: &tb.WebhookEndpoint{PublicURL: publicURL},
}
if botMode != "production" {
deleteWebhook()
poller = &tb.LongPoller{Timeout: 1 * time.Second}
} else {
if !webhookIsSet() {
setWebhook()
}
}
pref := tb.Settings{
Token: envBotToken,
Poller: poller,
}
return pref
}
func reminderHelper(m *tb.Message) ([]StoredReminder, bool) {
done := true
reminders, err := getUserReminders(m.Sender)
if err != nil {
botInstance.Send(m.Chat, "Error Finding Reminders!")
} else if len(reminders) == 0 {
botInstance.Send(m.Chat, "No Pending Reminders!")
} else {
done = false
}
return reminders, done
}
func privateMessageHelper(m *tb.Message, silent bool) bool {
if !m.Private() {
if !silent {
botInstance.Send(m.Chat, "Must be done in private chat!")
}
return true
}
return false
}
func confirmReminderSet(wait Reminder, recipient tb.Recipient) {
stringQuantity := strconv.Itoa(wait.quantity)
string := "Reminder set for " + stringQuantity + " " + wait.units + "s!"
botInstance.Send(recipient, string)
}
func forwardMessage(recipient *tb.User, message *tb.Message) {
botInstance.Forward(recipient, message)
}
func forwardStoredMessageAfterDelay(id primitive.ObjectID, duration time.Duration) {
time.Sleep(duration)
rem, err := getStoredReminderFromID(id)
if err != nil {
log.Println("Message unable to be retrieved or no longer in database")
return
}
message := messageFromStoredReminder(rem)
go forwardMessage(rem.User, &message)
go removeMessageFromDB(id)
}
func forwardMessageAfterDelay(wait Reminder, recipient *tb.User, message *tb.Message) {
id := storeMessageIntoDB(message, recipient, wait.timestamp)
forwardStoredMessageAfterDelay(id, wait.duration)
}
func getWaitTime(payload string) (Reminder, error) {
temp := strings.Join(timeUnits, "|")
waitExpr := regexp.MustCompile(`(\d+) (` + temp + `)s?`)
matches := waitExpr.FindStringSubmatch(payload)
if matches == nil {
return Reminder{}, errors.New("No matches found")
}
quant, _ := strconv.Atoi(matches[1])
units := matches[2]
// TODO: Fix how duration is being generated
seconds := int64(quant * unitMap[units])
duration := time.Duration(seconds) * time.Second
timestamp := time.Now().Add(duration)
return Reminder{units: units, quantity: quant, duration: duration, timestamp: timestamp.Unix()}, nil
}