hdkv
/
i2_bot
Archived
1
Fork 0

Pin to all squads at :55 of every even hour

Closes #9
master
Vladimir Hodakov 2017-11-24 13:24:36 +04:00
parent a32ed89be8
commit b038d5b10d
8 changed files with 64 additions and 5 deletions

8
Gopkg.lock generated
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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())
}
}
}
}

View File

@ -10,5 +10,7 @@ import (
// PinnerInterface implements Pinner for importing via appcontex
type PinnerInterface interface {
Init()
PinBattleAlert()
PinMessageToAllChats(update *tgbotapi.Update) string
}

View File

@ -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{}

View File

@ -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)