Skip to content

Commit cfb6140

Browse files
committed
Add secondary bot setup to only make findTrollHouses request
This is important because if the targer trollHouse banned this bot, it will fail to find troll with this error: { "description": "Forbidden: bot was kicked from the supergroup chat", "error_code": 403, "ok": false } However, if we use a hidden bot (unknown from trolls) to only make this request, it works pretty fine. After all, they never will know which bot to ban to stop the troll-shield.
1 parent 8b116f5 commit cfb6140

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

troll_shield.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,27 +176,49 @@ func setupLogging() {
176176
}
177177
}
178178

179-
func setupBot() *telegram.BotAPI {
180-
token, exists := os.LookupEnv("TELEGRAM_BOT_TOKEN")
179+
func setupBot(envVar string) (*telegram.BotAPI, error) {
180+
token, exists := os.LookupEnv(envVar)
181181
if !exists {
182-
log.Fatal("TELEGRAM_BOT_TOKEN env should be defined.")
182+
return nil, fmt.Errorf("%s env should be defined", envVar)
183183
}
184184
bot, err := telegram.NewBotAPI(token)
185185

186186
if err != nil {
187-
log.Panic(err)
187+
return nil, fmt.Errorf("Setup %v failed with: %v", envVar, err)
188188
}
189189

190190
bot.Debug = true
191191

192192
log.Printf("Authorized on account @%s", bot.Self.UserName)
193193

194-
return bot
194+
return bot, nil
195+
}
196+
197+
func setupBots() (*telegram.BotAPI, *telegram.BotAPI) {
198+
var bot, botHidden *telegram.BotAPI
199+
var err error
200+
201+
log.Println("Setup the main bot")
202+
bot, err = setupBot("TELEGRAM_BOT_TOKEN")
203+
if err != nil {
204+
log.Fatalf("Bot setup failed: %v", err)
205+
}
206+
207+
log.Println("Setup the hidden bot")
208+
botHidden, err = setupBot("TELEGRAM_BOT_HIDDEN_TOKEN")
209+
if err != nil {
210+
log.Printf("Bot setup failed: %v. Fallback to main bot.", err)
211+
botHidden = bot
212+
}
213+
214+
return bot, botHidden
215+
}
195216
}
196217

197218
func main() {
198219
setupLogging()
199-
bot := setupBot()
220+
bot, botHidden := setupBots()
221+
200222
for update := range getUpdates(bot) {
201223
if messageEvent(&update) {
202224
if update.Message.Text == "/lelerax" {
@@ -206,7 +228,7 @@ func main() {
206228

207229
if newChatMemberEvent(&update) {
208230
for _, member := range *update.Message.NewChatMembers {
209-
if trollHouse := findTrollHouses(bot, member.ID); trollHouse != "" {
231+
if trollHouse := findTrollHouses(botHidden, member.ID); trollHouse != "" {
210232
kickTroll(bot, &update, member, trollHouse)
211233
} else if fromChatEvent(&update, "commonlispbr") && !member.IsBot {
212234
welcomeMessage(bot, &update, member)

0 commit comments

Comments
 (0)