Update dependencies
This commit is contained in:
29
internal/telegram/exported.go
Normal file
29
internal/telegram/exported.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Fantasy World Zookeeper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"gitlab.com/toby3d/telegram"
|
||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper/context"
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
log zerolog.Logger
|
||||
|
||||
bot *telegram.Bot
|
||||
)
|
||||
|
||||
// New initializes package
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
log = c.Logger.With().Str("domain", "telegram").Int("version", 1).Logger()
|
||||
|
||||
log.Info().Msg("Starting Telegram instance")
|
||||
|
||||
go func() {
|
||||
StartBot()
|
||||
}()
|
||||
}
|
49
internal/telegram/respond.go
Normal file
49
internal/telegram/respond.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Fantasy World Zookeeper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
func getMessageParams(chatID int64, message string, disableWebPagePreview bool) telegram.SendMessageParameters {
|
||||
return telegram.SendMessageParameters{
|
||||
ChatID: chatID,
|
||||
Text: message,
|
||||
ParseMode: "Markdown",
|
||||
DisableWebPagePreview: disableWebPagePreview}
|
||||
}
|
||||
|
||||
// RespondWithMarkdown will send message to given chat with Markdown parse mode
|
||||
func RespondWithMarkdown(chatID int64, message string) {
|
||||
messageParams := getMessageParams(chatID, message, false)
|
||||
|
||||
_, err := bot.SendMessage(&messageParams)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
// RespondWithoutMarkdown will send message to given chat without parse mode
|
||||
func RespondWithoutMarkdown(chatID int64, message string) {
|
||||
messageParams := getMessageParams(chatID, message, false)
|
||||
messageParams.ParseMode = "HTML"
|
||||
|
||||
_, err := bot.SendMessage(&messageParams)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
// RespondWithInlineKeyboard will send message to given chat with Markdown parse
|
||||
// mode and keyboard attached
|
||||
func RespondWithInlineKeyboard(chatID int64, message string, keyboard *telegram.InlineKeyboardMarkup) {
|
||||
messageParams := getMessageParams(chatID, message, false)
|
||||
messageParams.ReplyMarkup = keyboard
|
||||
|
||||
_, err := bot.SendMessage(&messageParams)
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
}
|
97
internal/telegram/telegram.go
Normal file
97
internal/telegram/telegram.go
Normal file
@@ -0,0 +1,97 @@
|
||||
// Fantasy World Zookeeper Bot
|
||||
// Copyright (c) 2018 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
http "github.com/valyala/fasthttp"
|
||||
"gitlab.com/toby3d/telegram"
|
||||
"golang.org/x/net/proxy"
|
||||
"lab.wtfteam.pro/fat0troll/fw_zookeeper/internal/router"
|
||||
"net"
|
||||
)
|
||||
|
||||
func proxyDialer(addr string) (net.Conn, error) {
|
||||
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
|
||||
func Bot() *telegram.Bot {
|
||||
return bot
|
||||
}
|
||||
|
||||
// StartBot starts connection with Telegram
|
||||
func StartBot() {
|
||||
// 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 {
|
||||
bot = new(telegram.Bot)
|
||||
client := new(http.Client)
|
||||
client.Dial = proxyDialer
|
||||
bot.SetClient(client)
|
||||
bot.AccessToken = c.Config.Telegram.Token
|
||||
bot.User, err = bot.GetMe()
|
||||
} else {
|
||||
bot, err = telegram.New(c.Config.Telegram.Token)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
if c.Config.Telegram.Webhook.Enabled {
|
||||
var url http.URI
|
||||
url.Parse(nil, []byte("https://"+c.Config.Telegram.Webhook.Domain))
|
||||
if len(url.Host()) == 0 {
|
||||
log.Fatal().Msg("Can't parse webhook URL: got empty host")
|
||||
}
|
||||
log.Info().Msg("Trying to set webhook: " + url.String() + bot.AccessToken)
|
||||
|
||||
webhook := telegram.NewWebhook(url.String()+bot.AccessToken, nil)
|
||||
webhook.MaxConnections = 40
|
||||
|
||||
updates = bot.NewWebhookChannel(&url, webhook, "", "", c.Config.Telegram.Webhook.Listen)
|
||||
} else {
|
||||
log.Warn().Msg("Using long-polling for updates (not recommended)")
|
||||
var info *telegram.WebhookInfo
|
||||
info, err = bot.GetWebhookInfo()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
if info != nil && info.URL != "" {
|
||||
log.Info().Msg("Deleting old webhook...")
|
||||
_, err := bot.DeleteWebhook()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
}
|
||||
updatesParams := telegram.GetUpdatesParameters{
|
||||
Offset: 0,
|
||||
Limit: 100,
|
||||
Timeout: 60,
|
||||
}
|
||||
updates = bot.NewLongPollingChannel(&updatesParams)
|
||||
}
|
||||
|
||||
log.Info().Msg("Connection with Telegram established")
|
||||
|
||||
for update := range updates {
|
||||
updateText, _ := json.Marshal(update)
|
||||
log.Debug().Msgf("%s", string(updateText))
|
||||
go router.Respond(update)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user