2017-10-04 17:56:18 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
2018-03-31 16:45:09 +04:00
|
|
|
|
// Copyright (c) 2017-2018 Vladimir "fat0troll" Hodakov
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
|
|
|
|
package appcontext
|
|
|
|
|
|
|
|
|
|
import (
|
2018-03-31 16:45:09 +04:00
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"bitbucket.org/pztrn/flagger"
|
|
|
|
|
"bitbucket.org/pztrn/mogrus"
|
2018-03-31 09:16:32 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
"github.com/robfig/cron"
|
2018-05-19 12:14:25 +04:00
|
|
|
|
"github.com/fat0troll/i2_bot/lib/broadcaster/broadcasterinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/chatter/chatterinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/config"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/connections"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/datacache/datacacheinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/forwarder/forwarderinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/migrations/migrationsinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/orders/ordersinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/pinner/pinnerinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/pokedexer/pokedexerinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/reminder/reminderinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/router/routerinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/sender/senderinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/squader/squaderinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/statistics/statisticsinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/talkers/talkersinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/users/usersinterface"
|
|
|
|
|
"github.com/fat0troll/i2_bot/lib/welcomer/welcomerinterface"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// Context is an application context struct
|
2017-10-04 17:56:18 +04:00
|
|
|
|
type Context struct {
|
2017-11-22 16:32:10 +04:00
|
|
|
|
StartupFlags *flagger.Flagger
|
|
|
|
|
Cfg *config.Config
|
2017-11-24 13:24:36 +04:00
|
|
|
|
Cron *cron.Cron
|
2017-11-22 16:32:10 +04:00
|
|
|
|
Log *mogrus.LoggerHandler
|
|
|
|
|
Bot *tgbotapi.BotAPI
|
2018-01-29 23:50:25 +04:00
|
|
|
|
DataCache datacacheinterface.DataCacheInterface
|
2017-11-22 16:32:10 +04:00
|
|
|
|
Forwarder forwarderinterface.ForwarderInterface
|
|
|
|
|
Migrations migrationsinterface.MigrationsInterface
|
|
|
|
|
Router routerinterface.RouterInterface
|
|
|
|
|
Pokedexer pokedexerinterface.PokedexerInterface
|
|
|
|
|
Db *sqlx.DB
|
|
|
|
|
Talkers talkersinterface.TalkersInterface
|
|
|
|
|
Broadcaster broadcasterinterface.BroadcasterInterface
|
|
|
|
|
Welcomer welcomerinterface.WelcomerInterface
|
|
|
|
|
Pinner pinnerinterface.PinnerInterface
|
2017-12-23 01:40:36 +04:00
|
|
|
|
Reminder reminderinterface.ReminderInterface
|
2017-11-22 16:32:10 +04:00
|
|
|
|
Chatter chatterinterface.ChatterInterface
|
2018-03-31 16:45:09 +04:00
|
|
|
|
Sender senderinterface.SenderInterface
|
2017-11-22 16:32:10 +04:00
|
|
|
|
Squader squaderinterface.SquaderInterface
|
|
|
|
|
Users usersinterface.UsersInterface
|
|
|
|
|
Statistics statisticsinterface.StatisticsInterface
|
2017-11-26 15:28:55 +04:00
|
|
|
|
Orders ordersinterface.OrdersInterface
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// Init is a initialization function for context
|
2017-10-04 17:56:18 +04:00
|
|
|
|
func (c *Context) Init() {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
l := mogrus.New()
|
|
|
|
|
l.Initialize()
|
|
|
|
|
|
|
|
|
|
log := l.CreateLogger("i2_bot")
|
2018-03-31 09:16:32 +04:00
|
|
|
|
log.CreateOutput("stdout", os.Stdout, true, "info")
|
2017-11-22 16:32:10 +04:00
|
|
|
|
c.Log = log
|
|
|
|
|
|
|
|
|
|
c.StartupFlags = flagger.New(c.Log)
|
|
|
|
|
c.StartupFlags.Initialize()
|
|
|
|
|
|
|
|
|
|
// Adding available startup flags here
|
|
|
|
|
configFlag := flagger.Flag{}
|
|
|
|
|
configFlag.Name = "config"
|
|
|
|
|
configFlag.Description = "Configuration file path"
|
|
|
|
|
configFlag.Type = "string"
|
|
|
|
|
configFlag.DefaultValue = "./config.yaml"
|
|
|
|
|
err := c.StartupFlags.AddFlag(&configFlag)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Errorln(err)
|
|
|
|
|
}
|
|
|
|
|
c.StartupFlags.Parse()
|
|
|
|
|
|
|
|
|
|
configPath, err := c.StartupFlags.GetStringValue("config")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Errorln(err)
|
|
|
|
|
c.Log.Fatal("Can't get config file parameter from command line. Exiting.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Cfg = config.New()
|
|
|
|
|
c.Cfg.Init(c.Log, configPath)
|
2017-11-14 03:44:21 +04:00
|
|
|
|
|
2018-03-31 16:45:09 +04:00
|
|
|
|
logFile, err := os.OpenFile(c.Cfg.Logs.LogPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
|
2017-11-14 03:44:21 +04:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
2017-11-22 16:32:10 +04:00
|
|
|
|
c.Log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug")
|
2017-11-14 03:44:21 +04:00
|
|
|
|
|
|
|
|
|
c.Bot = connections.BotInit(c.Cfg, c.Log)
|
|
|
|
|
c.Db = connections.DBInit(c.Cfg, c.Log)
|
2017-11-22 16:32:10 +04:00
|
|
|
|
|
2017-11-24 13:24:36 +04:00
|
|
|
|
crontab := cron.New()
|
|
|
|
|
c.Cron = crontab
|
2017-11-22 16:32:10 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterBroadcasterInterface registering broadcaster interface in application
|
|
|
|
|
func (c *Context) RegisterBroadcasterInterface(bi broadcasterinterface.BroadcasterInterface) {
|
|
|
|
|
c.Broadcaster = bi
|
|
|
|
|
c.Broadcaster.Init()
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterChatterInterface registers chatter interface in application
|
|
|
|
|
func (c *Context) RegisterChatterInterface(ci chatterinterface.ChatterInterface) {
|
|
|
|
|
c.Chatter = ci
|
|
|
|
|
c.Chatter.Init()
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterDataCacheInterface registers datacache interface in application
|
|
|
|
|
func (c *Context) RegisterDataCacheInterface(di datacacheinterface.DataCacheInterface) {
|
|
|
|
|
c.DataCache = di
|
|
|
|
|
c.DataCache.Init()
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterForwarderInterface registers forwarder interface in application
|
|
|
|
|
func (c *Context) RegisterForwarderInterface(fi forwarderinterface.ForwarderInterface) {
|
|
|
|
|
c.Forwarder = fi
|
|
|
|
|
c.Forwarder.Init()
|
2017-10-07 02:23:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterMigrationsInterface registering migrations interface in application
|
|
|
|
|
func (c *Context) RegisterMigrationsInterface(mi migrationsinterface.MigrationsInterface) {
|
|
|
|
|
c.Migrations = mi
|
|
|
|
|
c.Migrations.Init()
|
2017-10-13 00:31:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterOrdersInterface registers orders interface in application
|
|
|
|
|
func (c *Context) RegisterOrdersInterface(oi ordersinterface.OrdersInterface) {
|
|
|
|
|
c.Orders = oi
|
|
|
|
|
c.Orders.Init()
|
2017-11-14 03:44:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterPinnerInterface registering pinner interface in application
|
|
|
|
|
func (c *Context) RegisterPinnerInterface(pi pinnerinterface.PinnerInterface) {
|
|
|
|
|
c.Pinner = pi
|
|
|
|
|
c.Pinner.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterPokedexerInterface registering parsers interface in application
|
|
|
|
|
func (c *Context) RegisterPokedexerInterface(pi pokedexerinterface.PokedexerInterface) {
|
|
|
|
|
c.Pokedexer = pi
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 01:40:36 +04:00
|
|
|
|
// RegisterReminderInterface registering reminder interface in application
|
|
|
|
|
func (c *Context) RegisterReminderInterface(ri reminderinterface.ReminderInterface) {
|
|
|
|
|
c.Reminder = ri
|
|
|
|
|
c.Reminder.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterRouterInterface registering router interface in application
|
|
|
|
|
func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) {
|
|
|
|
|
c.Router = ri
|
|
|
|
|
c.Router.Init()
|
2017-11-19 22:16:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-31 16:45:09 +04:00
|
|
|
|
// RegisterSenderInterface registering sender interface in application
|
|
|
|
|
func (c *Context) RegisterSenderInterface(si senderinterface.SenderInterface) {
|
|
|
|
|
c.Sender = si
|
|
|
|
|
c.Sender.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterStatisticsInterface registers statistics interface in application
|
|
|
|
|
func (c *Context) RegisterStatisticsInterface(si statisticsinterface.StatisticsInterface) {
|
|
|
|
|
c.Statistics = si
|
|
|
|
|
c.Statistics.Init()
|
2018-01-29 23:50:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-19 22:16:11 +04:00
|
|
|
|
// RegisterSquaderInterface registers squader interface in application
|
|
|
|
|
func (c *Context) RegisterSquaderInterface(si squaderinterface.SquaderInterface) {
|
|
|
|
|
c.Squader = si
|
|
|
|
|
c.Squader.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 12:37:46 +04:00
|
|
|
|
// RegisterTalkersInterface registering talkers interface in application
|
|
|
|
|
func (c *Context) RegisterTalkersInterface(ti talkersinterface.TalkersInterface) {
|
|
|
|
|
c.Talkers = ti
|
|
|
|
|
c.Talkers.Init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterWelcomerInterface registering welcomer interface in application
|
|
|
|
|
func (c *Context) RegisterWelcomerInterface(wi welcomerinterface.WelcomerInterface) {
|
|
|
|
|
c.Welcomer = wi
|
|
|
|
|
c.Welcomer.Init()
|
2017-11-26 15:28:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 06:06:32 +04:00
|
|
|
|
// RegisterUsersInterface registers users interface in application
|
|
|
|
|
func (c *Context) RegisterUsersInterface(ui usersinterface.UsersInterface) {
|
|
|
|
|
c.Users = ui
|
|
|
|
|
c.Users.Init()
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// RunDatabaseMigrations applies migrations on bot's startup
|
2017-10-06 02:56:06 +04:00
|
|
|
|
func (c *Context) RunDatabaseMigrations() {
|
2018-03-31 16:45:09 +04:00
|
|
|
|
err := c.Migrations.SetDialect("mysql")
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
err = c.Migrations.Migrate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Fatal(err.Error())
|
|
|
|
|
}
|
2017-10-06 02:56:06 +04:00
|
|
|
|
}
|
2018-02-05 12:37:46 +04:00
|
|
|
|
|
|
|
|
|
// StartBot starts listening for Telegram updates
|
|
|
|
|
func (c *Context) StartBot() {
|
|
|
|
|
_, err := c.Bot.SetWebhook(tgbotapi.NewWebhook(c.Cfg.Telegram.WebHookDomain + c.Bot.Token))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updates := c.Bot.ListenForWebhook("/" + c.Bot.Token)
|
2018-03-31 16:45:09 +04:00
|
|
|
|
go func() {
|
|
|
|
|
err = http.ListenAndServe(c.Cfg.Telegram.ListenAddress, nil)
|
|
|
|
|
}()
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Log.Fatal(err.Error())
|
|
|
|
|
}
|
2018-02-05 12:37:46 +04:00
|
|
|
|
|
|
|
|
|
c.Log.Info("Listening on " + c.Cfg.Telegram.ListenAddress)
|
|
|
|
|
c.Log.Info("Webhook URL: " + c.Cfg.Telegram.WebHookDomain + c.Bot.Token)
|
|
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
|
if update.Message != nil {
|
|
|
|
|
if update.Message.From != nil {
|
|
|
|
|
if update.Message.Date > (int(time.Now().Unix()) - 5) {
|
2018-03-31 16:45:09 +04:00
|
|
|
|
go c.Router.RouteRequest(update)
|
2018-02-05 12:37:46 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if update.InlineQuery != nil {
|
2018-03-31 16:45:09 +04:00
|
|
|
|
c.Router.RouteInline(update)
|
2018-02-05 12:37:46 +04:00
|
|
|
|
} else if update.CallbackQuery != nil {
|
2018-03-31 16:45:09 +04:00
|
|
|
|
c.Router.RouteCallback(update)
|
2018-02-05 12:37:46 +04:00
|
|
|
|
} else if update.ChosenInlineResult != nil {
|
|
|
|
|
c.Log.Debug(update.ChosenInlineResult.ResultID)
|
|
|
|
|
} else {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|