From b038d5b10d93e65839915db1a92157ee631928c6 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Fri, 24 Nov 2017 13:24:36 +0400 Subject: [PATCH] Pin to all squads at :55 of every even hour Closes #9 --- Gopkg.lock | 8 ++++- cmd/i2_bot/i2_bot.go | 3 ++ lib/appcontext/appcontext.go | 8 ++--- lib/pinner/exported.go | 2 ++ lib/pinner/pinner.go | 32 +++++++++++++++++++ lib/pinner/pinnerinterface/pinnerinterface.go | 2 ++ lib/squader/getters.go | 13 ++++++++ .../squaderinterface/squaderinterface.go | 1 + 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 9dff5b0..486d133 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -25,6 +25,12 @@ revision = "056a4d47dcc4d67fa3947a4f13945a5c690e568b" version = "v2.1.0" +[[projects]] + name = "github.com/robfig/cron" + packages = ["."] + revision = "b024fc5ea0e34bc3f83d9941c8d60b0622bfaca4" + version = "v1" + [[projects]] name = "github.com/sirupsen/logrus" packages = ["."] @@ -70,6 +76,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "72b8805a04554297ce6fcb28ecaad016e3398624e90621de1b067cfe2f662e40" + inputs-digest = "e26a19cb9a3e96a96b1d51599ead99341edc2278667ddf8a7dac4cd197fbe09d" solver-name = "gps-cdcl" solver-version = 1 diff --git a/cmd/i2_bot/i2_bot.go b/cmd/i2_bot/i2_bot.go index e18114d..6e0346d 100644 --- a/cmd/i2_bot/i2_bot.go +++ b/cmd/i2_bot/i2_bot.go @@ -47,6 +47,9 @@ func main() { c.Log.Info("= i2_bot initialized. =") c.Log.Info("=======================") + c.Cron.Start() + c.Log.Info("> Cron started.") + u := tgbotapi.NewUpdate(0) u.Timeout = 60 diff --git a/lib/appcontext/appcontext.go b/lib/appcontext/appcontext.go index 28bfb13..9569709 100644 --- a/lib/appcontext/appcontext.go +++ b/lib/appcontext/appcontext.go @@ -6,6 +6,7 @@ package appcontext import ( "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jmoiron/sqlx" + "github.com/robfig/cron" "lab.pztrn.name/fat0troll/i2_bot/lib/broadcaster/broadcasterinterface" "lab.pztrn.name/fat0troll/i2_bot/lib/chatter/chatterinterface" "lab.pztrn.name/fat0troll/i2_bot/lib/config" @@ -29,6 +30,7 @@ import ( type Context struct { StartupFlags *flagger.Flagger Cfg *config.Config + Cron *cron.Cron Log *mogrus.LoggerHandler Bot *tgbotapi.BotAPI Forwarder forwarderinterface.ForwarderInterface @@ -87,11 +89,9 @@ func (c *Context) Init() { c.Bot = connections.BotInit(c.Cfg, c.Log) c.Db = connections.DBInit(c.Cfg, c.Log) -} - -// InitializeStartupFlags gives information about available startup flags -func (c *Context) InitializeStartupFlags() { + crontab := cron.New() + c.Cron = crontab } // RegisterRouterInterface registering router interface in application diff --git a/lib/pinner/exported.go b/lib/pinner/exported.go index f6170c5..85560db 100644 --- a/lib/pinner/exported.go +++ b/lib/pinner/exported.go @@ -25,4 +25,6 @@ func New(ac *appcontext.Context) { // Init is an initialization function for pinner func (p *Pinner) Init() { c.Log.Info("Initializing Pinner...") + + c.Cron.AddFunc("0 55 */2 * * *", p.PinBattleAlert) } diff --git a/lib/pinner/pinner.go b/lib/pinner/pinner.go index a16fc73..c1a5e33 100644 --- a/lib/pinner/pinner.go +++ b/lib/pinner/pinner.go @@ -69,3 +69,35 @@ func (p *Pinner) PinMessageToAllChats(update *tgbotapi.Update) string { return "ok" } + +// PinBattleAlert pins to all squads 'battle alert' at :55 of every even hour +// Even hours are in Moscow timezone +func (p *Pinner) PinBattleAlert() { + c.Log.Debug("> Cron invoked PinBattleAlert()") + + message := "*Турнир Лиги покемемов состоится через 5 минут!*\nБоевая готовность, отряд!" + groupChats, _ := c.Squader.GetAllSquadChats() + + for i := range groupChats { + if groupChats[i].ChatType == "supergroup" { + msg := tgbotapi.NewMessage(groupChats[i].TelegramID, message) + msg.ParseMode = "Markdown" + + pinnableMessage, err := c.Bot.Send(msg) + if err != nil { + c.Log.Error(err.Error()) + } + + pinChatMessageConfig := tgbotapi.PinChatMessageConfig{ + ChatID: pinnableMessage.Chat.ID, + MessageID: pinnableMessage.MessageID, + DisableNotification: true, + } + + _, err = c.Bot.PinChatMessage(pinChatMessageConfig) + if err != nil { + c.Log.Error(err.Error()) + } + } + } +} diff --git a/lib/pinner/pinnerinterface/pinnerinterface.go b/lib/pinner/pinnerinterface/pinnerinterface.go index e9d3ff3..f515676 100644 --- a/lib/pinner/pinnerinterface/pinnerinterface.go +++ b/lib/pinner/pinnerinterface/pinnerinterface.go @@ -10,5 +10,7 @@ import ( // PinnerInterface implements Pinner for importing via appcontex type PinnerInterface interface { Init() + + PinBattleAlert() PinMessageToAllChats(update *tgbotapi.Update) string } diff --git a/lib/squader/getters.go b/lib/squader/getters.go index b750aec..5624837 100644 --- a/lib/squader/getters.go +++ b/lib/squader/getters.go @@ -38,6 +38,19 @@ func (s *Squader) GetSquadByID(squadID int) (dbmapping.SquadChat, bool) { return squadFull, true } +// GetAllSquadChats returns all main squad chats +func (s *Squader) GetAllSquadChats() ([]dbmapping.Chat, bool) { + groupChats := []dbmapping.Chat{} + + err := c.Db.Select(&groupChats, "SELECT ch.* FROM chats ch, squads s WHERE s.chat_id=ch.id") + if err != nil { + c.Log.Error(err) + return groupChats, false + } + + return groupChats, true +} + // GetUserRolesInSquads lists all user roles func (s *Squader) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool) { userRoles := []dbmapping.SquadPlayerFull{} diff --git a/lib/squader/squaderinterface/squaderinterface.go b/lib/squader/squaderinterface/squaderinterface.go index 1809491..7389df8 100644 --- a/lib/squader/squaderinterface/squaderinterface.go +++ b/lib/squader/squaderinterface/squaderinterface.go @@ -12,6 +12,7 @@ import ( type SquaderInterface interface { Init() + GetAllSquadChats() ([]dbmapping.Chat, bool) GetSquadByID(squadID int) (dbmapping.SquadChat, bool) GetUserRolesInSquads(playerRaw *dbmapping.Player) ([]dbmapping.SquadPlayerFull, bool)