-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathping.go
317 lines (269 loc) · 8.73 KB
/
ping.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
package jarvisbot
import (
"fmt"
"strconv"
"time"
"github.com/boltdb/bolt"
"github.com/tucnak/telebot"
)
const rateLimit = 3
const countKey = "count"
const timestampKey = "timestamp"
// CollectPing sends a message requesting all users in a group to respond.
// This allows Jarvis to collect their usernames
func (j *JarvisBot) CollectPing(msg *message) {
if !msg.Chat.IsGroupChat() {
j.SendMessage(msg.Chat, "This feature only works in group chats, sorry!", nil)
return
}
if msg.IsReply() {
so := &telebot.SendOptions{ReplyTo: *msg.Message}
if msg.Sender.Username == "" {
j.SendMessage(msg.Chat, "I'm afraid you don't have a username, "+msg.Sender.FirstName+". You should create one if you'd like @ notifications.", so)
} else {
if !j.usernameExistsForChat(&msg.Chat, &msg.Sender) {
err := j.saveUserToDB(&msg.Chat, &msg.Sender)
if err != nil {
j.log.Printf("[%s] error saving user to db: %s", time.Now().Format(time.RFC3339), err)
return
}
}
j.SendMessage(msg.Chat, "Thanks, "+msg.Sender.FirstName+". I've saved @"+msg.Sender.Username+" for use later.", so)
}
} else {
so := &telebot.SendOptions{ReplyMarkup: telebot.ReplyMarkup{ForceReply: true}}
j.SendMessage(msg.Chat, "/pingsetup: Sets up ping functionality\nPlease reply to this so I can store your username \U0001F60A", so)
}
}
// Ping returns a list of all usernames in a given chat, along with a message.
// This is used for alerting all members in a group.
func (j *JarvisBot) Ping(msg *message) {
if !msg.Chat.IsGroupChat() {
j.SendMessage(msg.Chat, "This feature only works in group chats, sorry!", nil)
return
}
// We don't accept pings from people without usernames.
if msg.Sender.Username == "" {
j.SendMessage(msg.Chat, "You can only ping if you have a username yourself, sorry!", nil)
return
}
if !j.groupBucketExists(&msg.Chat) {
j.SendMessage(msg.Chat, "I don't have any records for this group. Perhaps run /pingsetup first?", nil)
return
}
if j.canSendWithinTimeLimit(&msg.Chat) {
usernames, err := j.getAllUsernamesForChat(&msg.Chat, &msg.Sender)
if err != nil {
j.log.Printf("[%s] error getting all usernames for chat %s: %s", time.Now().Format(time.RFC3339), msg.Chat.Title, err)
return
}
if usernames == "" {
j.SendMessage(msg.Chat, "I'm afraid I don't currently have any other usernames for this group apart from yours, "+msg.Sender.FirstName+".", &telebot.SendOptions{ReplyTo: *msg.Message})
return
}
message := ""
for _, v := range msg.Args {
message = message + v + " "
}
// We update the last saved time and hour count.
j.updateLastSentTime(&msg.Chat)
j.SendMessage(msg.Chat, usernames+message, nil)
} else {
j.SendMessage(msg.Chat, "Rate limit reached! You can only send "+strconv.Itoa(rateLimit)+" pings to a group every hour.", nil)
}
}
// saveUserToDB saves the given user to the given chat bucket in Bolt.
func (j *JarvisBot) saveUserToDB(chat *telebot.Chat, sender *telebot.User) error {
groupID, userID, username := strconv.Itoa(int(chat.ID)), strconv.Itoa(sender.ID), sender.Username
if sender.Username == "" {
return fmt.Errorf("user has no username!")
}
err := j.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb, err := b.CreateBucketIfNotExists([]byte(groupID))
if err != nil {
return err
}
err = gb.Put([]byte(userID), []byte(username))
if err != nil {
return err
}
return nil
})
return err
}
// saveUsernameSafely checks if a user exists in a given group chat, and saves if he/she
// doesn't currently exist.
// We try as much as possible to avoid opening a Read-Write transaction, because
// Bolt can only have one Read-Write transaction at any time.
func (j *JarvisBot) saveUsernameSafely(chat *telebot.Chat, sender *telebot.User) {
if sender.Username == "" || !chat.IsGroupChat() {
return
}
if !j.usernameExistsForChat(chat, sender) {
err := j.saveUserToDB(chat, sender)
if err != nil {
j.log.Printf("[%s] error saving user %s to group %s: %s", time.Now().Format(time.RFC3339), sender.Username, chat.Title, err)
}
}
}
// usernameExistsForChat checks if a username exists for a given chat.
// This returns false if the userID exists but the username has changed.
func (j *JarvisBot) usernameExistsForChat(chat *telebot.Chat, sender *telebot.User) bool {
res := false
// Guard against possibility that chat isn't a group chat.
// We're lazy here, returning true implies that we don't want to run a save.
if !chat.IsGroupChat() {
return true
}
groupID, senderID, username := strconv.Itoa(int(chat.ID)), strconv.Itoa(sender.ID), sender.Username
j.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb := b.Bucket([]byte(groupID))
if gb == nil {
return nil
}
v := gb.Get([]byte(senderID))
if v != nil && string(v) == username {
res = true
return nil
}
return nil
})
return res
}
// getAllUsernamesForChat returns all usernames for a chat, sans the sender's.
func (j *JarvisBot) getAllUsernamesForChat(chat *telebot.Chat, sender *telebot.User) (string, error) {
uArray, groupID, senderID := []string{}, strconv.Itoa(int(chat.ID)), strconv.Itoa(sender.ID)
if !chat.IsGroupChat() {
return "", fmt.Errorf("chat %s is not a group chat!", groupID)
}
err := j.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb := b.Bucket([]byte(groupID))
if gb == nil {
return fmt.Errorf("error retrieving bucket for group ID %s", groupID)
}
gb.ForEach(func(k, v []byte) error {
key, curr := string(k), string(v)
if key != senderID && key != timestampKey && key != countKey {
uArray = append(uArray, curr)
}
return nil
})
return nil
})
if err != nil {
return "", err
}
res := ""
for _, v := range uArray {
res = res + "@" + v + " "
}
return res, nil
}
// Check if a bucket for the group chat already exists.
func (j *JarvisBot) groupBucketExists(chat *telebot.Chat) bool {
res, groupID := false, strconv.Itoa(int(chat.ID))
j.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb := b.Bucket([]byte(groupID))
if gb != nil {
res = true
}
return nil
})
return res
}
// canSendWithinTimeLimit checks the time limit for the given group chat.
// We limit pings per group chat to rateLimit every hour.
func (j *JarvisBot) canSendWithinTimeLimit(chat *telebot.Chat) bool {
res := false
groupID := strconv.Itoa(int(chat.ID))
err := j.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb := b.Bucket([]byte(groupID))
if gb == nil {
return fmt.Errorf("invariant error - time limit checked for %s when no group bucket exists", groupID)
}
timeString := gb.Get([]byte(timestampKey))
if timeString == nil {
res = true
return nil
}
lastTime, err := time.Parse(time.RFC3339, string(timeString))
if err != nil {
return fmt.Errorf("problem parsing timestring for group ID %s: %s", groupID, err)
}
if time.Since(lastTime) > time.Hour {
res = true
} else {
if count := gb.Get([]byte(countKey)); count != nil {
numCount, err := strconv.Atoi(string(count))
if err != nil {
return err
}
res = (numCount < rateLimit)
}
}
return nil
})
if err != nil {
j.log.Printf("[%s] error grabbing last-send-time from db: %s", time.Now().Format(time.RFC3339), err)
}
return res
}
// updateLastSentTime updates the last sent time for a given chat.
func (j *JarvisBot) updateLastSentTime(chat *telebot.Chat) error {
if !chat.IsGroupChat() {
return fmt.Errorf("chat is not a group chat!")
}
groupID := strconv.Itoa(int(chat.ID))
err := j.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(group_usernames_bucket_name)
gb := b.Bucket([]byte(groupID))
if gb == nil {
return fmt.Errorf("invariant error - group bucket does not exist, yet attempt to update timestamp")
}
timeString := gb.Get([]byte(timestampKey))
if timeString == nil {
return resetTime(gb)
} else {
lastTime, err := time.Parse(time.RFC3339, string(timeString))
if err != nil {
return err
}
if time.Since(lastTime) > time.Hour {
return resetTime(gb)
} else {
countBytes := gb.Get([]byte(countKey))
if countBytes == nil {
return fmt.Errorf("invariant error - timestamp was saved but no count exists")
}
lastCount, err := strconv.Atoi(string(countBytes))
if err != nil {
return err
}
newCount := strconv.Itoa(lastCount + 1)
err = gb.Put([]byte(countKey), []byte(newCount))
if err != nil {
return err
}
}
}
return nil
})
return err
}
// Helpers
func resetTime(b *bolt.Bucket) error {
err := b.Put([]byte(timestampKey), []byte(time.Now().Format(time.RFC3339)))
if err != nil {
return err
}
err = b.Put([]byte(countKey), []byte(strconv.Itoa(1)))
if err != nil {
return err
}
return nil
}