2017-10-04 17:56:18 +04:00
|
|
|
|
// i2_bot – Instinct PokememBro Bot
|
|
|
|
|
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
|
|
import (
|
2018-01-29 23:50:25 +04:00
|
|
|
|
"bitbucket.org/pztrn/mogrus"
|
2018-05-06 05:57:52 +04:00
|
|
|
|
_ "github.com/go-sql-driver/mysql" // MySQL driver for sqlx
|
2017-10-26 18:17:58 +04:00
|
|
|
|
"github.com/go-telegram-bot-api/telegram-bot-api"
|
2017-11-14 03:44:21 +04:00
|
|
|
|
"github.com/jmoiron/sqlx"
|
2018-05-11 18:55:37 +04:00
|
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
"net/http"
|
2018-05-02 07:25:39 +04:00
|
|
|
|
"source.wtfteam.pro/i2_bot/i2_bot/lib/config"
|
2017-10-04 17:56:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
2018-05-11 18:55:37 +04:00
|
|
|
|
// botInitDirect used when no proxy in config file
|
|
|
|
|
func botInitDirect(cfg *config.Config, lg *mogrus.LoggerHandler) *tgbotapi.BotAPI {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
bot, err := tgbotapi.NewBotAPI(cfg.Telegram.APIToken)
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
lg.Fatal(err.Error())
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
bot.Debug = true
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-11-14 03:44:21 +04:00
|
|
|
|
lg.Info("Bot version: " + config.VERSION)
|
|
|
|
|
lg.Info("Authorized on account @", bot.Self.UserName)
|
2017-10-04 17:56:18 +04:00
|
|
|
|
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return bot
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 18:55:37 +04:00
|
|
|
|
// botInitWithProxy used when there is proxy in config file
|
|
|
|
|
func botInitWithProxy(cfg *config.Config, lg *mogrus.LoggerHandler) *tgbotapi.BotAPI {
|
|
|
|
|
proxyAuth := proxy.Auth{}
|
|
|
|
|
if cfg.Proxy.Username != "" {
|
|
|
|
|
proxyAuth.User = cfg.Proxy.Username
|
|
|
|
|
proxyAuth.Password = cfg.Proxy.Password
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dialProxy proxy.Dialer
|
|
|
|
|
var err error
|
|
|
|
|
if cfg.Proxy.Username != "" {
|
|
|
|
|
dialProxy, err = proxy.SOCKS5("tcp", cfg.Proxy.Address, &proxyAuth, proxy.Direct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
lg.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
dialProxy, err = proxy.SOCKS5("tcp", cfg.Proxy.Address, &proxyAuth, proxy.Direct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
lg.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proxyTransport := &http.Transport{Dial: dialProxy.Dial}
|
|
|
|
|
proxyClient := http.Client{Transport: proxyTransport}
|
|
|
|
|
|
|
|
|
|
bot, err := tgbotapi.NewBotAPIWithClient(cfg.Telegram.APIToken, &proxyClient)
|
|
|
|
|
if err != nil {
|
|
|
|
|
lg.Fatal(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bot.Debug = true
|
|
|
|
|
|
|
|
|
|
lg.Info("Bot version: " + config.VERSION)
|
|
|
|
|
lg.Info("Authorized on account @", bot.Self.UserName)
|
|
|
|
|
|
|
|
|
|
return bot
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// External functions
|
|
|
|
|
|
|
|
|
|
// BotInit initializes connection to Telegram
|
|
|
|
|
func BotInit(cfg *config.Config, lg *mogrus.LoggerHandler) *tgbotapi.BotAPI {
|
|
|
|
|
if cfg.Proxy.Enabled {
|
|
|
|
|
lg.Info("Using proxy for bot: " + cfg.Proxy.Address)
|
|
|
|
|
return botInitWithProxy(cfg, lg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lg.Info("Using direct connection to Telegram")
|
|
|
|
|
return botInitDirect(cfg, lg)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 09:39:50 +04:00
|
|
|
|
// DBInit initializes database connection
|
2017-11-14 03:44:21 +04:00
|
|
|
|
func DBInit(cfg *config.Config, lg *mogrus.LoggerHandler) *sqlx.DB {
|
2017-10-18 07:03:34 +04:00
|
|
|
|
database, err := sqlx.Connect("mysql", cfg.Database.User+":"+cfg.Database.Password+"@tcp("+cfg.Database.Host+":"+cfg.Database.Port+")/"+cfg.Database.Database+"?parseTime=true&charset=utf8mb4,utf8")
|
|
|
|
|
if err != nil {
|
2017-11-14 03:44:21 +04:00
|
|
|
|
lg.Fatal(err)
|
2017-10-18 07:03:34 +04:00
|
|
|
|
}
|
2017-11-14 03:44:21 +04:00
|
|
|
|
lg.Info("Database connection established!")
|
2017-10-18 07:03:34 +04:00
|
|
|
|
return database
|
2017-10-04 17:56:18 +04:00
|
|
|
|
}
|