2018-11-29 20:32:51 +04:00
|
|
|
// Fantasy World Zookeeper Bot
|
|
|
|
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
|
|
|
|
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
"gitlab.com/toby3d/telegram"
|
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
"lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/router"
|
|
|
|
)
|
|
|
|
|
2018-11-29 20:51:36 +04:00
|
|
|
func proxyDialer(addr string) (net.Conn, error) {
|
2018-11-29 20:32:51 +04:00
|
|
|
log.Debug().Msgf("Proxy used: %s", c.Config.Telegram.Proxy.Address)
|
|
|
|
proxyAuth := proxy.Auth{}
|
|
|
|
if c.Config.Telegram.Proxy.Username != "" {
|
|
|
|
proxyAuth.User = c.Config.Telegram.Proxy.Username
|
|
|
|
proxyAuth.Password = c.Config.Telegram.Proxy.Password
|
|
|
|
}
|
|
|
|
var dialProxy proxy.Dialer
|
|
|
|
var err error
|
|
|
|
dialProxy, err = proxy.SOCKS5("tcp", c.Config.Telegram.Proxy.Address, &proxyAuth, proxy.Direct)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Failed to dial proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialProxy.Dial("tcp", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bot returns Telegram instance
|
2018-11-29 20:51:36 +04:00
|
|
|
func Bot() *telegram.Bot {
|
|
|
|
return bot
|
2018-11-29 20:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartBot starts connection with Telegram
|
2018-11-29 20:51:36 +04:00
|
|
|
func StartBot() {
|
2018-11-29 20:32:51 +04:00
|
|
|
// Any errors here considered fatal, because main purpose of this app is Telegram interactions
|
|
|
|
var err error
|
|
|
|
var updates telegram.UpdatesChannel
|
|
|
|
if c.Config.Telegram.Proxy.Enabled {
|
2018-11-29 20:51:36 +04:00
|
|
|
bot = new(telegram.Bot)
|
2018-11-29 20:32:51 +04:00
|
|
|
client := new(http.Client)
|
2018-11-29 20:51:36 +04:00
|
|
|
client.Dial = proxyDialer
|
|
|
|
bot.SetClient(client)
|
|
|
|
bot.AccessToken = c.Config.Telegram.Token
|
|
|
|
bot.User, err = bot.GetMe()
|
2018-11-29 20:32:51 +04:00
|
|
|
} else {
|
2018-11-29 20:51:36 +04:00
|
|
|
bot, err = telegram.New(c.Config.Telegram.Token)
|
2018-11-29 20:32:51 +04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Config.Telegram.Webhook.Enabled {
|
|
|
|
var url *http.URI
|
|
|
|
url.Parse(nil, []byte(c.Config.Telegram.Webhook.Domain))
|
|
|
|
if len(url.Host()) == 0 {
|
|
|
|
log.Fatal().Msg("Can't parse webhook URL: got empty host")
|
|
|
|
}
|
2018-11-29 20:51:36 +04:00
|
|
|
log.Info().Msg("Trying to set webhook: " + url.String() + bot.AccessToken)
|
2018-11-29 20:32:51 +04:00
|
|
|
|
2018-11-29 20:51:36 +04:00
|
|
|
webhook := telegram.NewWebhook(url.String()+bot.AccessToken, nil)
|
2018-11-29 20:32:51 +04:00
|
|
|
webhook.MaxConnections = 40
|
|
|
|
|
2018-11-29 20:51:36 +04:00
|
|
|
updates = bot.NewWebhookChannel(url, webhook, "", "", c.Config.Telegram.Webhook.Listen)
|
2018-11-29 20:32:51 +04:00
|
|
|
} else {
|
|
|
|
log.Warn().Msg("Using long-polling for updates (not recommended)")
|
|
|
|
var info *telegram.WebhookInfo
|
2018-11-29 20:51:36 +04:00
|
|
|
info, err = bot.GetWebhookInfo()
|
2018-11-29 20:32:51 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
}
|
|
|
|
if info != nil && info.URL != "" {
|
|
|
|
log.Info().Msg("Deleting old webhook...")
|
2018-11-29 20:51:36 +04:00
|
|
|
_, err := bot.DeleteWebhook()
|
2018-11-29 20:32:51 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatesParams := telegram.GetUpdatesParameters{
|
|
|
|
Offset: 0,
|
|
|
|
Limit: 100,
|
|
|
|
Timeout: 60,
|
|
|
|
}
|
2018-11-29 20:51:36 +04:00
|
|
|
updates = bot.NewLongPollingChannel(&updatesParams)
|
2018-11-29 20:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msg("Connection with Telegram established")
|
|
|
|
|
|
|
|
for update := range updates {
|
2018-11-29 20:51:36 +04:00
|
|
|
go router.Respond(update)
|
2018-11-29 20:32:51 +04:00
|
|
|
}
|
|
|
|
}
|