From 7975ea54c1d6f3ce49c89b49cf213682e4644aea Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sun, 22 Oct 2017 14:04:14 +0400 Subject: [PATCH] /send_all feature for administrators --- lib/getters/chat.go | 13 ++++++ .../gettersinterface/gettersinterface.go | 2 + lib/getters/player.go | 18 ++++++++ lib/router/router.go | 12 +++++- lib/talkers/broadcast.go | 41 +++++++++++++++++++ lib/talkers/help.go | 9 +++- .../talkersinterface/talkersinterface.go | 4 +- 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 lib/talkers/broadcast.go diff --git a/lib/getters/chat.go b/lib/getters/chat.go index 4963ef6..c390eac 100644 --- a/lib/getters/chat.go +++ b/lib/getters/chat.go @@ -54,3 +54,16 @@ func (g *Getters) GetOrCreateChat(telegramUpdate *tgbotapi.Update) (dbmapping.Ch return chatRaw, true } + +// GetAllPrivateChats returns all private chats +func (g *Getters) GetAllPrivateChats() ([]dbmapping.Chat, bool) { + privateChats := []dbmapping.Chat{} + + err := c.Db.Select(&privateChats, "SELECT * FROM chats WHERE chat_type='private'") + if err != nil { + log.Println(err) + return privateChats, false + } + + return privateChats, true +} diff --git a/lib/getters/gettersinterface/gettersinterface.go b/lib/getters/gettersinterface/gettersinterface.go index 4c0ee3d..34a597b 100644 --- a/lib/getters/gettersinterface/gettersinterface.go +++ b/lib/getters/gettersinterface/gettersinterface.go @@ -15,8 +15,10 @@ type GettersInterface interface { Init() GetOrCreateChat(update *tgbotapi.Update) (dbmapping.Chat, bool) GetChatByID(chatID int) (dbmapping.Chat, bool) + GetAllPrivateChats() ([]dbmapping.Chat, bool) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) GetPlayerByID(playerID int) (dbmapping.Player, bool) + PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool GetProfile(playerID int) (dbmapping.Profile, bool) GetPokememes() ([]dbmapping.PokememeFull, bool) GetBestPokememes(playerID int) ([]dbmapping.PokememeFull, bool) diff --git a/lib/getters/player.go b/lib/getters/player.go index 6d4b628..fd74119 100644 --- a/lib/getters/player.go +++ b/lib/getters/player.go @@ -50,3 +50,21 @@ func (g *Getters) GetOrCreatePlayer(telegramID int) (dbmapping.Player, bool) { return playerRaw, true } + +// PlayerBetterThan return true, if profile is more or equal powerful than +// provided power level +func (g *Getters) PlayerBetterThan(playerRaw *dbmapping.Player, powerLevel string) bool { + var isBetter = false + switch playerRaw.Status { + case "owner": + isBetter = true + case "admin": + if powerLevel != "owner" { + isBetter = true + } + default: + isBetter = false + } + + return isBetter +} diff --git a/lib/router/router.go b/lib/router/router.go index b46f13e..450cb96 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -48,6 +48,9 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { var meMsg = regexp.MustCompile("/me\\z") var bestMsg = regexp.MustCompile("/best\\z") + // Owner commands + var sendAllMsg = regexp.MustCompile("/send_all(.+)") + // Forwards var pokememeMsg = regexp.MustCompile("(Уровень)(.+)(Опыт)(.+)\n(Элементы:)(.+)\n(.+)(💙MP)") var profileMsg = regexp.MustCompile(`(Онлайн: )(\d+)\n(Турнир через)(.+)\n\n(.*)\n(Элементы)(.+)\n(.*)\n\n(.+)(Уровень)(.+)\n`) @@ -97,7 +100,7 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { } // Help case helpMsg.MatchString(text): - c.Talkers.HelpMessage(update) + c.Talkers.HelpMessage(update, &playerRaw) // Pokememes info case pokedexMsg.MatchString(text): if strings.HasSuffix(text, "1") { @@ -125,6 +128,13 @@ func (r *Router) RouteRequest(update tgbotapi.Update) string { // Suggestions case bestMsg.MatchString(text): c.Talkers.BestPokememesList(update, playerRaw) + // Admin commands + case sendAllMsg.MatchString(text): + if c.Getters.PlayerBetterThan(&playerRaw, "admin") { + c.Talkers.AdminBroadcastMessage(update) + } else { + c.Talkers.AnyMessageUnauthorized(update) + } // Easter eggs case huMsg.MatchString(text): c.Talkers.MatMessage(update) diff --git a/lib/talkers/broadcast.go b/lib/talkers/broadcast.go new file mode 100644 index 0000000..7883016 --- /dev/null +++ b/lib/talkers/broadcast.go @@ -0,0 +1,41 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package talkers + +import ( // stdlib + // 3rd party + "strings" + + "github.com/go-telegram-bot-api/telegram-bot-api" +) + +// AdminBroadcastMessage sends message to all private chats with bot +func (t *Talkers) AdminBroadcastMessage(update tgbotapi.Update) string { + broadcastingMessageBody := strings.Replace(update.Message.Text, "/send_all", "", 1) + + privateChats, ok := c.Getters.GetAllPrivateChats() + if !ok { + return "fail" + } + + for i := range privateChats { + chat := privateChats[i] + broadcastingMessage := "*Привет, " + chat.Name + "!*\n\n" + broadcastingMessage += "*Важное сообщение от администратора " + update.Message.From.FirstName + " " + update.Message.From.LastName + "* (@" + update.Message.From.UserName + ")\n\n" + broadcastingMessage += broadcastingMessageBody + + msg := tgbotapi.NewMessage(int64(chat.TelegramID), broadcastingMessage) + msg.ParseMode = "Markdown" + c.Bot.Send(msg) + } + + message := "Сообщение всем отправлено. Надеюсь, пользователи бота за него тебя не убьют.\n" + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + msg.ParseMode = "Markdown" + + c.Bot.Send(msg) + + return "ok" +} diff --git a/lib/talkers/help.go b/lib/talkers/help.go index abfd28a..3efbf9b 100644 --- a/lib/talkers/help.go +++ b/lib/talkers/help.go @@ -8,17 +8,22 @@ import ( "github.com/go-telegram-bot-api/telegram-bot-api" // local "../config" + "../dbmapping" ) // HelpMessage gives user all available commands -func (t *Talkers) HelpMessage(update tgbotapi.Update) { +func (t *Talkers) HelpMessage(update tgbotapi.Update, playerRaw *dbmapping.Player) { message := "*Бот Инстинкта Enchanched.*\n\n" message += "Текущая версия: *" + config.VERSION + "*\n\n" - message += "Список команд:\n\n" + message += "Список команд\n\n" message += "+ /me – посмотреть свой сохраненный профиль в боте\n" message += "+ /best – посмотреть лучших покемонов для поимки\n" message += "+ /pokedeks – получить список известных боту покемемов\n" + if c.Getters.PlayerBetterThan(playerRaw, "admin") { + message += "+ /send\\_all _текст_ — отправить сообщение всем пользователям бота\n" + } message += "+ /help – выводит данное сообщение\n" + message += "\n\n" message += "Связаться с автором: @fat0troll\n" diff --git a/lib/talkers/talkersinterface/talkersinterface.go b/lib/talkers/talkersinterface/talkersinterface.go index 4bb1e5d..4c86fed 100644 --- a/lib/talkers/talkersinterface/talkersinterface.go +++ b/lib/talkers/talkersinterface/talkersinterface.go @@ -15,7 +15,7 @@ type TalkersInterface interface { Init() HelloMessageUnauthorized(update tgbotapi.Update) HelloMessageAuthorized(update tgbotapi.Update, playerRaw dbmapping.Player) - HelpMessage(update tgbotapi.Update) + HelpMessage(update tgbotapi.Update, playerRaw *dbmapping.Player) PokememesList(update tgbotapi.Update, page int) PokememeInfo(update tgbotapi.Update, playerRaw dbmapping.Player) string BestPokememesList(update tgbotapi.Update, playerRaw dbmapping.Player) string @@ -30,6 +30,8 @@ type TalkersInterface interface { AnyMessageUnauthorized(update tgbotapi.Update) GetterError(update tgbotapi.Update) + AdminBroadcastMessage(update tgbotapi.Update) string + DurakMessage(update tgbotapi.Update) MatMessage(update tgbotapi.Update) }