diff --git a/frontend/main.go b/frontend/main.go index b602447..f200d47 100644 --- a/frontend/main.go +++ b/frontend/main.go @@ -21,19 +21,21 @@ type settingType struct { netSpecificMode string titleBrand string navBarBrand string + telegramBotName string } var setting settingType func main() { var settingDefault = settingType{ - servers: []string{""}, - proxyPort: 8000, - whoisServer: "whois.verisign-grs.com", - listen: ":5000", - dnsInterface: "asn.cymru.com", - titleBrand: "Bird-lg Go", - navBarBrand: "Bird-lg Go", + servers: []string{""}, + proxyPort: 8000, + whoisServer: "whois.verisign-grs.com", + listen: ":5000", + dnsInterface: "asn.cymru.com", + titleBrand: "Bird-lg Go", + navBarBrand: "Bird-lg Go", + telegramBotName: "", } if env := os.Getenv("BIRDLG_SERVERS"); env != "" { @@ -67,6 +69,9 @@ func main() { if env := os.Getenv("BIRDLG_NAVBAR_BRAND"); env != "" { settingDefault.navBarBrand = env } + if env := os.Getenv("BIRDLG_TELEGRAM_BOT_NAME"); env != "" { + settingDefault.telegramBotName = env + } serversPtr := flag.String("servers", strings.Join(settingDefault.servers, ","), "server name prefixes, separated by comma") domainPtr := flag.String("domain", settingDefault.domain, "server name domain suffixes") @@ -77,6 +82,7 @@ func main() { netSpecificModePtr := flag.String("net-specific-mode", settingDefault.netSpecificMode, "network specific operation mode, [(none)|dn42]") titleBrandPtr := flag.String("title-brand", settingDefault.titleBrand, "prefix of page titles in browser tabs") navBarBrandPtr := flag.String("navbar-brand", settingDefault.navBarBrand, "brand to show in the navigation bar") + telegramBotNamePtr := flag.String("telegram-bot-name", settingDefault.navBarBrand, "telegram bot name (used to filter @bot commands)") flag.Parse() if *serversPtr == "" { @@ -91,7 +97,7 @@ func main() { pos := strings.Index(server, "<") if pos != -1 { serversDisplay[i] = server[0:pos] - servers[i] = server[pos+1:len(server)-1] + servers[i] = server[pos+1 : len(server)-1] } } @@ -106,6 +112,7 @@ func main() { strings.ToLower(*netSpecificModePtr), *titleBrandPtr, *navBarBrandPtr, + *telegramBotNamePtr, } ImportTemplates() diff --git a/frontend/telegram_bot.go b/frontend/telegram_bot.go index b7d45d6..2a8d390 100644 --- a/frontend/telegram_bot.go +++ b/frontend/telegram_bot.go @@ -31,7 +31,8 @@ type tgWebhookResponse struct { func telegramIsCommand(message string, command string) bool { b := false - b = b || strings.HasPrefix(message, "/"+command+"@") + b = b || strings.HasPrefix(message, "/"+command+"@"+setting.telegramBotName+" ") + b = b || message == "/"+command+"@"+setting.telegramBotName b = b || strings.HasPrefix(message, "/"+command+" ") b = b || message == "/"+command return b diff --git a/frontend/telegram_bot_test.go b/frontend/telegram_bot_test.go new file mode 100644 index 0000000..2612435 --- /dev/null +++ b/frontend/telegram_bot_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "testing" +) + +func doTestTelegramIsCommand(t *testing.T, message string, command string, expected bool) { + if telegramIsCommand(message, command) != expected { + t.Errorf("telegramIsCommand(\"%s\", \"%s\") unexpected result", message, command) + } +} + +func TestTelegramIsCommand(t *testing.T) { + setting.telegramBotName = "test_bot" + + // Recognize command + doTestTelegramIsCommand(t, "/trace", "trace", true) + doTestTelegramIsCommand(t, "/trace", "trace1234", false) + doTestTelegramIsCommand(t, "/trace", "tra", false) + doTestTelegramIsCommand(t, "/trace", "abcdefg", false) + + // Recognize command with parameters + doTestTelegramIsCommand(t, "/trace google.com", "trace", true) + doTestTelegramIsCommand(t, "/trace google.com", "trace1234", false) + doTestTelegramIsCommand(t, "/trace google.com", "tra", false) + doTestTelegramIsCommand(t, "/trace google.com", "abcdefg", false) + + // Recognize command with bot name + doTestTelegramIsCommand(t, "/trace@test_bot", "trace", true) + doTestTelegramIsCommand(t, "/trace@test_bot", "trace1234", false) + doTestTelegramIsCommand(t, "/trace@test_bot", "tra", false) + doTestTelegramIsCommand(t, "/trace@test_bot", "abcdefg", false) + doTestTelegramIsCommand(t, "/trace@test_bot_123", "trace", false) + doTestTelegramIsCommand(t, "/trace@test_", "trace", false) + + // Recognize command with bot name and parameters + doTestTelegramIsCommand(t, "/trace@test_bot google.com", "trace", true) + doTestTelegramIsCommand(t, "/trace@test_bot google.com", "trace1234", false) + doTestTelegramIsCommand(t, "/trace@test_bot google.com", "tra", false) + doTestTelegramIsCommand(t, "/trace@test_bot google.com", "abcdefg", false) + doTestTelegramIsCommand(t, "/trace@test_bot_123 google.com", "trace", false) + doTestTelegramIsCommand(t, "/trace@test google.com", "trace", false) +}