From 401f962bd218c134634cb43fb63069ec85e69289 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Tue, 8 Jan 2019 05:00:16 +0400 Subject: [PATCH] Move support of channel to helper with TDLib Now we're discarding messages sent directly to us, except for messages from @fw_zookeeper. --- domains/battles/v1/announce.go | 32 ++++++++++++++++++++++++++++ domains/battles/v1/forward.go | 38 +++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 domains/battles/v1/announce.go diff --git a/domains/battles/v1/announce.go b/domains/battles/v1/announce.go new file mode 100644 index 0000000..96650a7 --- /dev/null +++ b/domains/battles/v1/announce.go @@ -0,0 +1,32 @@ +// Fantasy World Zookeeper Bot +// Copyright (c) 2018 Vladimir "fat0troll" Hodakov + +package battlesv1 + +import ( + "gitlab.com/toby3d/telegram" + itelegram "lab.wtfteam.pro/fat0troll/fw_zookeeper/local/telegram" + "strings" +) + +// AnnounceBattle posts battle announce to channel +func AnnounceBattle(update *telegram.Update) { + announcerID := 788961789 + if update.Message.From.ID != announcerID { + log.Warn().Msgf("Someone trying to mimic announcer: user with ID %d and username %s", update.Message.From.ID, update.Message.From.Username) + return + } + + battleInformationArray := strings.Split(update.Message.Text, " ") + if len(battleInformationArray) != 2 { + log.Error().Msgf("Unexpected battle information format: got %d elements instead of 2", len(battleInformationArray)) + return + } + battleType := battleInformationArray[0] + battleTag := battleInformationArray[1] + + log.Debug().Msgf("Battle type «%s», battle tag %s", battleType, battleTag) + + itelegram.RespondWithoutMarkdown(c.Config.Announces.ChannelID, battleType) + itelegram.RespondWithoutMarkdown(c.Config.Announces.ChannelID, battleTag) +} diff --git a/domains/battles/v1/forward.go b/domains/battles/v1/forward.go index 2d8ea35..ab4e185 100644 --- a/domains/battles/v1/forward.go +++ b/domains/battles/v1/forward.go @@ -11,15 +11,33 @@ import ( // ForwardCommand responds to fight request func ForwardCommand(update *telegram.Update) { - log.Info().Msg("Battle request received!") - if update.Message.Chat.ID == c.Config.Announces.ChannelID { - log.Info().Msg("This battle is already posted") - } - message := update.Message.Text - if !strings.Contains(message, "join") { - message = strings.Replace(message, "fight", "join_fight", 1) - } - message = strings.Replace(message, "@FWorldBot ", "", 1) + message := "" + if update.Message.From.ID != 788961789 { + message := "*Смотритель более не принимает прямые запросы*\n" + message += "Люди глупы и безрассудны. Им дали возможность позвать на помощь — но они пользуются ею, чтобы самоутвердиться. Довольно!\n" + message += "Зовите на помощь @fw\\_zookeeper, как живого человека — через кнопку «Позвать на помощь» в игре.\n" + message += "Доброй охоты!" + itelegram.RespondWithMarkdown(update.Message.Chat.ID, message) + } else { + switch { + case strings.HasPrefix(update.Message.Text, "Кабаны!"): + AnnounceBattle(update) + case strings.HasPrefix(update.Message.Text, "Огры!"): + AnnounceBattle(update) + case strings.HasPrefix(update.Message.Text, "Буйволы!"): + AnnounceBattle(update) + default: + log.Info().Msg("Battle request received!") + if update.Message.Chat.ID == c.Config.Announces.ChannelID { + log.Info().Msg("This battle is already posted") + } + message = update.Message.Text + if !strings.Contains(message, "join") { + message = strings.Replace(message, "fight", "join_fight", 1) + } + message = strings.Replace(message, "@FWorldBot ", "", 1) - itelegram.RespondWithoutMarkdown(c.Config.Announces.ChannelID, message) + itelegram.RespondWithoutMarkdown(c.Config.Announces.ChannelID, message) + } + } }