From 0ba20b3ed53e12ddfad65c133cebec3ace48b0ea Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Thu, 29 Nov 2018 22:15:21 +0400 Subject: [PATCH] Add battle announces to channel Channel ID is configured in fw_zookeeper.yaml. --- domains/battles/v1/exported.go | 27 +++++++++++++++++++++++++++ domains/battles/v1/forward.go | 20 ++++++++++++++++++++ internal/config/announces.go | 9 +++++++++ internal/config/struct.go | 3 ++- internal/telegram/respond.go | 23 +++++++++++++++++++++++ internal/telegram/telegram.go | 1 - main.go | 2 ++ 7 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 domains/battles/v1/exported.go create mode 100644 domains/battles/v1/forward.go create mode 100644 internal/config/announces.go diff --git a/domains/battles/v1/exported.go b/domains/battles/v1/exported.go new file mode 100644 index 0000000..06bbef5 --- /dev/null +++ b/domains/battles/v1/exported.go @@ -0,0 +1,27 @@ +// Fantasy World Zookeeper Bot +// Copyright (c) 2018 Vladimir "fat0troll" Hodakov + +package battlesv1 + +import ( + "github.com/rs/zerolog" + "lab.wtfteam.pro/fat0troll/fw_zookeeper/context" + "lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/router" + "regexp" +) + +var ( + c *context.Context + log zerolog.Logger +) + +// New initializes package +func New(cc *context.Context) { + c = cc + log = c.Logger.With().Str("domain", "battles").Int("version", 1).Logger() + + fightRegex := regexp.MustCompile(`@FWorldBot\s(.*)fight_(.{12})$`) + router.RegisterPrivateRegexp(fightRegex, ForwardCommand) + + log.Info().Msg("Domain «battles» initialized") +} diff --git a/domains/battles/v1/forward.go b/domains/battles/v1/forward.go new file mode 100644 index 0000000..ab5f656 --- /dev/null +++ b/domains/battles/v1/forward.go @@ -0,0 +1,20 @@ +// Fantasy World Zookeeper Bot +// Copyright (c) 2018 Vladimir "fat0troll" Hodakov + +package battlesv1 + +import ( + "gitlab.com/toby3d/telegram" + itelegram "lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/telegram" + "strings" +) + +// ForwardCommand responds to fight request +func ForwardCommand(update *telegram.Update) { + log.Info().Msg("Battle request received!") + message := update.Message.Text + message = strings.Replace(message, " fight", " join_fight", 1) + message = strings.Replace(message, "@FWorldBot ", "", 1) + + itelegram.RespondWithoutMarkdown(c.Config.Announces.ChannelID, message) +} diff --git a/internal/config/announces.go b/internal/config/announces.go new file mode 100644 index 0000000..27bc330 --- /dev/null +++ b/internal/config/announces.go @@ -0,0 +1,9 @@ +// Fantasy World Zookeeper Bot +// Copyright (c) 2018 Vladimir "fat0troll" Hodakov + +package config + +// Announces is a struct which handles announces configuration +type Announces struct { + ChannelID int64 `yaml:"channel_id"` +} diff --git a/internal/config/struct.go b/internal/config/struct.go index 4884ed3..90aad33 100644 --- a/internal/config/struct.go +++ b/internal/config/struct.go @@ -6,5 +6,6 @@ package config // Struct is a main configuration structure that holds all other // structs within. type Struct struct { - Telegram Telegram `yaml:"telegram"` + Telegram Telegram `yaml:"telegram"` + Announces Announces `yaml:"announces"` } diff --git a/internal/telegram/respond.go b/internal/telegram/respond.go index 688a7eb..e7c6aa7 100644 --- a/internal/telegram/respond.go +++ b/internal/telegram/respond.go @@ -24,3 +24,26 @@ func RespondWithMarkdown(chatID int64, message string) { log.Error().Err(err) } } + +// RespondWithoutMarkdown will send message to given chat without parse mode +func RespondWithoutMarkdown(chatID int64, message string) { + messageParams := getMessageParams(chatID, message, false) + messageParams.ParseMode = "HTML" + + _, err := bot.SendMessage(&messageParams) + if err != nil { + log.Error().Err(err) + } +} + +// RespondWithInlineKeyboard will send message to given chat with Markdown parse +// mode and keyboard attached +func RespondWithInlineKeyboard(chatID int64, message string, keyboard *telegram.InlineKeyboardMarkup) { + messageParams := getMessageParams(chatID, message, false) + messageParams.ReplyMarkup = keyboard + + _, err := bot.SendMessage(&messageParams) + if err != nil { + log.Error().Err(err) + } +} diff --git a/internal/telegram/telegram.go b/internal/telegram/telegram.go index a952017..ee4106d 100644 --- a/internal/telegram/telegram.go +++ b/internal/telegram/telegram.go @@ -90,7 +90,6 @@ func StartBot() { log.Info().Msg("Connection with Telegram established") for update := range updates { - log.Debug().Msgf("%+v", update) go router.Respond(update) } } diff --git a/main.go b/main.go index e42cd22..f2f6641 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "lab.wtfteam.pro/fat0troll/fw_zookeeper/context" + "lab.wtfteam.pro/fat0troll/fw_zookeeper/domains/battles/v1" "lab.wtfteam.pro/fat0troll/fw_zookeeper/domains/commands/v1" "lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/router" "lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/telegram" @@ -27,6 +28,7 @@ func main() { router.New(c) commandsv1.New(c) + battlesv1.New(c) telegram.New(c)