-
Notifications
You must be signed in to change notification settings - Fork 6
/
rest.go
376 lines (321 loc) · 10.1 KB
/
rest.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
package main
import (
"encoding/json"
"fmt"
"github.com/freshautomations/telegram-moderator-bot/context"
"github.com/freshautomations/telegram-moderator-bot/db"
"github.com/freshautomations/telegram-moderator-bot/defaults"
"github.com/freshautomations/telegram-moderator-bot/telegram"
"github.com/gorilla/mux"
"log"
"net/http"
"strings"
)
// Available administrator commands help text. X will be replaced with backtick.
const textHelpAdminCommands = `
X/promoteX _@username_ - Promote a user to moderator.
X/demoteX _@username_ - Demote a moderator to user.`
// Available moderator commands help text. X will be replaced with backtick.
const textHelpModeratorCommands = `
X/banX _@username_ - Kick and ban a user.
X/unbanX _@username_ - Unban a user.
X/warnX _@username_ - Warn a user.
X/listX - List moderators.`
// Composed help text.
const textHelpMessage = `Hi %s!
You are a%s.
Available commands:%s%s`
// Template for list-like messages.
const textListMessage = `%s:
%s.`
// String-joiner text.
const textNewlineComma = `,
`
// MembersType types
const (
regular = iota
moderators
admininstrators
creator
kicked
left
)
// Structure to hold parsed incoming text.
type CommandData struct {
Command string
Users []*telegram.User
UserStrings []string
}
// Filters incoming messages and updates internal database with user IDs. Filters out bots.
func PreprocessMessage(ctx *context.Context, incoming *telegram.Update) (message *telegram.Message) {
var from *telegram.User = nil
switch {
case incoming.Message != nil:
message = incoming.Message
from = message.From
case incoming.EditedMessage != nil:
message = incoming.EditedMessage
from = message.From
case incoming.ChannelPost != nil:
from = incoming.ChannelPost.From
case incoming.EditedChannelPost != nil:
from = incoming.EditedChannelPost.From
case incoming.InlineQuery != nil:
from = incoming.InlineQuery.From
case incoming.ChosenInlineResult.From != nil:
from = incoming.ChosenInlineResult.From
case incoming.CallbackQuery != nil:
from = incoming.CallbackQuery.From
case incoming.PreCheckoutQuery != nil:
from = incoming.PreCheckoutQuery.From
case incoming.ShippingQuery != nil:
from = incoming.ShippingQuery.From
default:
return
}
if from.IsBot {
return
}
name := from.FirstName
if from.LastName != "" {
name = name + " " + from.LastName
}
err := db.UpdateUserData(ctx, &db.UserData{from.Username, from.Id, name})
if err != nil {
//Todo: handle DynamoDB capacity limitations
log.Printf("[tempdebug] error updating user in DB: %+v", err.Error())
}
return
}
// Parse the incoming message for bot command and a list of users.
func ParseInput(m *telegram.Message) *CommandData {
output := &CommandData{}
if len(m.Entities) < 1 {
return nil
}
if m.Text[0] != '/' {
return nil
}
if m.Entities[0].Type != "bot_command" {
return nil
} else {
output.Command = m.Text[m.Entities[0].Offset : m.Entities[0].Offset+m.Entities[0].Length]
}
for _, entity := range m.Entities {
if entity.Type == "text_mention" {
output.Users = append(output.Users, entity.User)
} else {
if entity.Type == "mention" {
//Cut off the "@" from the front of the username.
output.UserStrings = append(output.UserStrings, m.Text[entity.Offset+1:entity.Offset+entity.Length])
}
}
}
return output
}
// Checks the list of members and compiles a User array out of valid users.
func CheckMembers(ctx *context.Context, ChatId int64, command *CommandData, MembersType int) []*telegram.User {
var ids []int
var result []*telegram.User
for _, user := range command.Users {
ids = append(ids, user.Id)
}
for _, user := range command.UserStrings {
dbUserData, err := db.GetUserData(ctx, user)
if err != nil {
if defaults.Debug {
log.Printf("[debug] (CheckMembers) Could not get user data from database for user %s, %+v", user, err.Error())
}
continue
}
if dbUserData == nil {
if defaults.Debug {
log.Printf("[debug] (CheckMembers) User not found in database: %s", user)
}
continue
}
ids = append(ids, dbUserData.UserID)
}
for _, userId := range ids {
userData, err := telegram.GetChatMember(ctx, ChatId, userId)
if err != nil {
if defaults.Debug {
log.Printf("[debug] (CheckMembers) Could not get user verification data from Telegram for user ID %d, %+v", userId, err.Error())
}
continue
}
if userData.User.IsBot {
continue
}
if defaults.Debug {
log.Printf("[debug] CheckMembers ChatMember %+v.", userData)
}
if MembersType == regular {
if userData.Status != "member" {
continue
}
}
if MembersType == creator {
if userData.Status != "creator" {
continue
}
}
if MembersType == kicked {
if userData.Status != "kicked" {
continue
}
}
if MembersType == left {
if userData.Status != "left" {
continue
}
}
if MembersType == moderators {
if userData.Status != "administrator" || userData.CanPromoteMembers {
continue
}
}
if MembersType == admininstrators {
if (userData.Status != "administrator" || !userData.CanPromoteMembers) && userData.Status != "creator" {
continue
}
}
if defaults.Debug {
log.Printf("[debug] CheckMembers Keeping ChatMember %+v.", userData)
}
result = append(result, userData.User)
}
return result
}
// MainHandler handles the requests coming to `/`.
func MainHandler(ctx *context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
status = http.StatusOK
w.WriteHeader(status)
incoming := &telegram.Update{}
err = json.NewDecoder(r.Body).Decode(incoming)
if err != nil {
log.Printf("[error] MainHandler decoder: %v", err)
status = http.StatusBadRequest
return
}
if incoming == nil {
log.Print("[error] MainHandler incoming data is empty.")
status = http.StatusBadRequest
return
}
message := PreprocessMessage(ctx, incoming)
if message == nil {
return
}
if message.Chat.Type != "supergroup" {
return
}
command := ParseInput(message)
if command == nil {
return
}
chatId := message.Chat.Id
messageId := message.MessageId
if defaults.Debug {
log.Printf("[debug] Command received %s from %s. Mentions: %s, Text_Mentions: %+v.", command.Command, message.From, strings.Join(command.UserStrings, ";"), command.Users)
log.Printf("[debug] Chat ID: %d, Message ID: %d, User ID: %d", chatId, messageId, message.From.Id)
}
isAdmin, isMod, getPrivilegesError := telegram.GetPrivileges(ctx, chatId, message.From.Id)
if getPrivilegesError != nil {
telegram.ReplyMessage(ctx, chatId, messageId, "Could not check user privileges.")
return status, getPrivilegesError
}
if !isMod {
return
}
// Commands for moderators
switch command.Command {
case "/help":
adminCommands := ""
privilegeSnippet := " *moderator*"
if isAdmin {
privilegeSnippet = "n *administrator*"
adminCommands = textHelpAdminCommands
}
text := fmt.Sprintf(textHelpMessage,
message.From.FirstName,
privilegeSnippet,
strings.Replace(textHelpModeratorCommands, "X", "`", -1),
strings.Replace(adminCommands, "X", "`", -1))
telegram.ReplyMessage(ctx, chatId, messageId, text)
return
case "/warn":
warned, banned := telegram.WarnMember(ctx, chatId, CheckMembers(ctx, chatId, command, regular))
if len(warned) >= 1 {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Warned user(s)", strings.Join(warned, textNewlineComma)))
}
if len(banned) >= 1 {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Banned user(s)", strings.Join(banned, textNewlineComma)))
}
if len(warned) < 1 && len(banned) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No users were warned.")
}
return
case "/ban":
list := telegram.BanMember(ctx, chatId, CheckMembers(ctx, chatId, command, regular))
if len(list) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No users were banned.")
} else {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Banned user(s)", strings.Join(list, textNewlineComma)))
}
return
case "/unban":
list := telegram.UnbanMember(ctx, chatId, CheckMembers(ctx, chatId, command, kicked))
if len(list) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No users were unbanned.")
} else {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Unbanned user(s)", strings.Join(list, textNewlineComma)))
}
return
case "/list":
list := telegram.ListModerators(ctx, chatId)
if len(list) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No moderators found.")
} else {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Moderators", strings.Join(list, textNewlineComma)))
}
return
}
if !isAdmin {
log.Printf("[warning] Non-administrator trying administrator command: %s, %s", command.Command, message.From)
return
}
// Commands for administrators
switch command.Command {
case "/promote":
list, errors := telegram.AddModerator(ctx, chatId, CheckMembers(ctx, chatId, command, regular))
if len(list) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No moderators were added.")
} else {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Added moderator(s)", strings.Join(list, textNewlineComma)))
}
if len(errors) > 0 {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf("Errors: %s.", strings.Join(errors, "; ")))
}
case "/demote":
list, errors := telegram.RemoveModerator(ctx, chatId, CheckMembers(ctx, chatId, command, moderators))
if len(list) < 1 {
telegram.ReplyMessage(ctx, chatId, messageId, "No moderators were removed.")
} else {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf(textListMessage, "Removed moderator(s)", strings.Join(list, textNewlineComma)))
}
if len(errors) > 0 {
telegram.ReplyMessage(ctx, chatId, messageId, fmt.Sprintf("Errors: %s.", strings.Join(errors, "; ")))
}
}
return
}
// AddRoutes adds the routes of the different calls to GorillaMux.
func AddRoutes(ctx *context.Context) (r *mux.Router) {
// Root and routes
r = mux.NewRouter()
r.Handle("/", context.Handler{ctx, MainHandler})
// Finally
http.Handle("/", r)
return
}