Skip to content

Commit

Permalink
Add secondary bot setup to only make findTrollHouses request
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ryukinix committed Jul 27, 2020
1 parent 8b116f5 commit cfb6140
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions troll_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,49 @@ func setupLogging() {
}
}

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

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

bot.Debug = true

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

return bot
return bot, nil
}

func setupBots() (*telegram.BotAPI, *telegram.BotAPI) {
var bot, botHidden *telegram.BotAPI
var err error

log.Println("Setup the main bot")
bot, err = setupBot("TELEGRAM_BOT_TOKEN")
if err != nil {
log.Fatalf("Bot setup failed: %v", err)
}

log.Println("Setup the hidden bot")
botHidden, err = setupBot("TELEGRAM_BOT_HIDDEN_TOKEN")
if err != nil {
log.Printf("Bot setup failed: %v. Fallback to main bot.", err)
botHidden = bot
}

return bot, botHidden
}
}

func main() {
setupLogging()
bot := setupBot()
bot, botHidden := setupBots()

for update := range getUpdates(bot) {
if messageEvent(&update) {
if update.Message.Text == "/lelerax" {
Expand All @@ -206,7 +228,7 @@ func main() {

if newChatMemberEvent(&update) {
for _, member := range *update.Message.NewChatMembers {
if trollHouse := findTrollHouses(bot, member.ID); trollHouse != "" {
if trollHouse := findTrollHouses(botHidden, member.ID); trollHouse != "" {
kickTroll(bot, &update, member, trollHouse)
} else if fromChatEvent(&update, "commonlispbr") && !member.IsBot {
welcomeMessage(bot, &update, member)
Expand Down

0 comments on commit cfb6140

Please sign in to comment.