Skip to content

Commit 24cedd5

Browse files
authored
Add support to qualified command like /pass@lelerax_bot (#11)
* Add support to qualified command like /pass@lelerax_bot I finally registered the bot commands via API, so we have autocomplete on Telegram by using the bot. In groups using /pass or /kills would be translated to /pass@lelerax_bot and /kills@lelerax_bot, but in v0.7.0 troll-shield version this is not supported. This PR takes care to support this properly, giving attention to edge cases like using /pass@anotherBotName (this will not work). /pass /kill simplified syntax will always works, but can trigger multiple bots if they implements the same command. CHANGELOG: - Add checkCommand which checks properly if is a valid command - Add commandEvent which can be use to filter commands (performance) - Add unit tests for both functions - Rename /lelerax to /ping, which makes more sense. - Add renan_r as admin. * Remove funlen linter, too much annoying
1 parent 5679cc0 commit 24cedd5

File tree

4 files changed

+94
-15
lines changed

4 files changed

+94
-15
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Run linters
1717
run: |
1818
export PATH=$PATH:$(go env GOPATH)/bin
19-
golangci-lint -E bodyclose,misspell,gocyclo,dupl,gofmt,golint,unconvert,goimports,depguard,gocritic,funlen,interfacer run
19+
golangci-lint -E bodyclose,misspell,gocyclo,dupl,gofmt,golint,unconvert,goimports,depguard,gocritic,interfacer run
2020
test:
2121
strategy:
2222
matrix:

main.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,35 @@ import (
1212
func main() {
1313
setupLogging()
1414
bot, botHidden, err := setupBots()
15+
botUser := bot.Self.UserName
1516
if err != nil {
1617
log.Fatal(err.Error())
1718
}
1819
kills := loadKills(killsFile)
1920
log.Printf("Currently kill state: %v", kills)
20-
2121
for update := range getUpdates(bot) {
2222
if messageEvent(&update) {
23-
if update.Message.Text == "/lelerax" {
23+
// Exit automatically from group after the bot receive a message from it
24+
for _, trollGroup := range trollGroups {
25+
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) {
26+
leaveChat(bot, &update, trollGroup)
27+
}
28+
}
29+
}
30+
31+
if commandEvent(&update) {
32+
msg := update.Message.Text
33+
if checkCommand(botUser, msg, "/ping") {
2434
reply(bot, &update, "Estou vivo.")
2535
}
2636

27-
if update.Message.Text == "/kills" {
37+
if checkCommand(botUser, msg, "/kills") {
2838
reportKills(bot, &update, kills)
2939
}
3040

31-
if strings.HasPrefix(update.Message.Text, "/pass ") && fromAdminEvent(&update) {
41+
if checkCommand(botUser, msg, "/pass") && fromAdminEvent(&update) {
3242
addPassList(bot, &update)
3343
}
34-
35-
// Exit automatically from group after the bot receive a message from it
36-
for _, trollGroup := range trollGroups {
37-
if fromChatEvent(&update, strings.TrimLeft(trollGroup, "@")) {
38-
leaveChat(bot, &update, trollGroup)
39-
}
40-
}
4144
}
4245

4346
if newChatMemberEvent(&update) {

troll_shield.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var admins = []string{
4646
"lerax",
4747
"luksamuk",
4848
"perkunos",
49+
"renan_r",
4950
}
5051

5152
const logfile = "troll-shield.log"
@@ -334,6 +335,22 @@ func fromAdminEvent(update *telegram.Update) bool {
334335
// addPass to passList and send a message
335336
func addPassList(bot TrollShieldBot, update *telegram.Update) {
336337
userName := extractPassUserName(update.Message.Text)
337-
passList = append(passList, userName)
338-
reply(bot, update, fmt.Sprintf("O passe para %q foi adicionado.", userName))
338+
if len(userName) > 0 {
339+
passList = append(passList, userName)
340+
reply(bot, update, fmt.Sprintf("O passe para %q foi adicionado.", userName))
341+
}
342+
}
343+
344+
func commandEvent(update *telegram.Update) bool {
345+
return messageEvent(update) && strings.HasPrefix(update.Message.Text, "/")
346+
}
347+
348+
// check if a /command is valid
349+
func checkCommand(botUserName string, msg string, command string) bool {
350+
isQualified := strings.Contains(msg, command+"@")
351+
if isQualified {
352+
qualifiedCommand := fmt.Sprintf("%v@%v", command, botUserName)
353+
return strings.HasPrefix(msg, qualifiedCommand)
354+
}
355+
return strings.HasPrefix(msg, command)
339356
}

troll_shield_test.go

+60-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func TestEvents(t *testing.T) {
111111
t.Errorf("newChatMemberEvent should return true when there is new members, got: %v", got)
112112
}
113113

114+
// fromChatEvent
114115
if got := fromChatEvent(&update, "commonlispbr"); got != false {
115116
t.Errorf("fromChatEvent should return false when there is no new members, got: %v", got)
116117
}
@@ -123,6 +124,16 @@ func TestEvents(t *testing.T) {
123124
t.Errorf("fromChatEvent should return true when there is new members, got: %v", got)
124125
}
125126

127+
// commandEvent
128+
update.Message.Text = "pancadaria tiro e bomba"
129+
if got := commandEvent(&update); got != false {
130+
t.Errorf("commandEvent should return false when there is no prefix / on Message.Text, got: %v", got)
131+
}
132+
update.Message.Text = "/command"
133+
if got := commandEvent(&update); got != true {
134+
t.Errorf("commandEvent should return true when receives /command like message, got: %v", got)
135+
}
136+
126137
}
127138

128139
func TestFindTrollHouses(t *testing.T) {
@@ -276,13 +287,20 @@ func TestExtractPassUserName(t *testing.T) {
276287
"/pass First Name",
277288
"First Name",
278289
},
290+
{
291+
"/pass@lelerax_bot @tretanews_bot",
292+
"@tretanews_bot",
293+
},
294+
{
295+
"/pass",
296+
"",
297+
},
279298
}
280299

281300
for _, test := range tableTest {
282301
if got := extractPassUserName(test.input); got != test.expected {
283302
t.Errorf("Expected %q, got %q", test.expected, got)
284303
}
285-
286304
}
287305
}
288306

@@ -327,3 +345,44 @@ func TestFromAdminEvent(t *testing.T) {
327345
t.Errorf("delduca should not even being a member, neither admin.")
328346
}
329347
}
348+
349+
func TestCheckCommand(t *testing.T) {
350+
tableTest := []struct {
351+
botUserName string
352+
msg string
353+
command string
354+
expected bool
355+
}{
356+
{
357+
"lelerax_bot",
358+
"/kills@lelerax_bot",
359+
"/kills",
360+
true,
361+
},
362+
{
363+
"botnilson",
364+
"/pass @infeliz",
365+
"/pass",
366+
true,
367+
},
368+
{
369+
"botsvaldo",
370+
"/pass@lelerax_bot @username",
371+
"/pass",
372+
false,
373+
},
374+
{
375+
"botsvaldo",
376+
"/pass@botsvaldo @username",
377+
"/pass",
378+
true,
379+
},
380+
}
381+
382+
for i, test := range tableTest {
383+
if got := checkCommand(test.botUserName, test.msg, test.command); got != test.expected {
384+
t.Errorf("%d. Expected %v, got %v for %+v.", i+1, test.expected, got, test)
385+
}
386+
}
387+
388+
}

0 commit comments

Comments
 (0)