This commit is contained in:
@@ -23,14 +23,14 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
c *context.Context
|
||||
ctx *context.Context
|
||||
connections map[string]*TelegramConnection
|
||||
)
|
||||
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
ctx = cc
|
||||
connections = make(map[string]*TelegramConnection)
|
||||
|
||||
tp := TelegramPusher{}
|
||||
c.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp))
|
||||
ctx.RegisterPusherInterface("telegram", pusherinterface.PusherInterface(tp))
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ func (tc *TelegramConnection) Initialize(connName string, cfg configstruct.Confi
|
||||
|
||||
func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage) {
|
||||
// Prepare message body.
|
||||
messageData := c.SendToParser(message.Username, message)
|
||||
messageData := ctx.SendToParser(message.Username, message)
|
||||
|
||||
messageToSend, _ := messageData["message"].(string)
|
||||
// We'll use HTML, so reformat links accordingly (if any).
|
||||
@@ -49,7 +49,7 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage)
|
||||
}
|
||||
}
|
||||
|
||||
c.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
ctx.Log.Debug().Msgf("Crafted message: %s", messageToSend)
|
||||
|
||||
// Send message.
|
||||
tc.SendMessage(messageToSend)
|
||||
@@ -62,7 +62,7 @@ func (tc *TelegramConnection) SendMessage(message string) {
|
||||
msgdata.Set("parse_mode", "HTML")
|
||||
|
||||
// Are we should use proxy?
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
httpTransport := &http.Transport{}
|
||||
|
||||
// nolint:nestif
|
||||
@@ -82,30 +82,30 @@ func (tc *TelegramConnection) SendMessage(message string) {
|
||||
|
||||
proxyURLParsed, err := url.Parse(proxyURL)
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL")
|
||||
ctx.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL")
|
||||
} else {
|
||||
httpTransport.Proxy = http.ProxyURL(proxyURLParsed)
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
client := &http.Client{Transport: httpTransport}
|
||||
botURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID)
|
||||
|
||||
c.Log.Debug().Msgf("Bot URL: %s", botURL)
|
||||
ctx.Log.Debug().Msgf("Bot URL: %s", botURL)
|
||||
|
||||
// ToDo: fix it.
|
||||
// nolint
|
||||
response, err := client.PostForm(botURL, msgdata)
|
||||
if err != nil {
|
||||
c.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram")
|
||||
ctx.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram")
|
||||
} else {
|
||||
c.Log.Debug().Msgf("Status: %s", response.Status)
|
||||
ctx.Log.Debug().Msgf("Status: %s", response.Status)
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body := []byte{}
|
||||
_, _ = response.Body.Read(body)
|
||||
response.Body.Close()
|
||||
c.Log.Debug().Msg(string(body))
|
||||
ctx.Log.Debug().Msg(string(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,14 +24,14 @@ import (
|
||||
type TelegramPusher struct{}
|
||||
|
||||
func (tp TelegramPusher) Initialize() {
|
||||
c.Log.Info().Msg("Initializing Telegram protocol pusher...")
|
||||
ctx.Log.Info().Msg("Initializing Telegram protocol pusher...")
|
||||
|
||||
// Get configuration for pushers and initialize every connection.
|
||||
cfg := c.Config.GetConfig()
|
||||
cfg := ctx.Config.GetConfig()
|
||||
for name, config := range cfg.Telegram {
|
||||
c.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
ctx.Log.Info().Str("conn", name).Msg("Initializing connection...")
|
||||
|
||||
// nolint:exhaustivestruct
|
||||
// nolint:exhaustruct
|
||||
conn := TelegramConnection{}
|
||||
connections[name] = &conn
|
||||
|
||||
@@ -42,17 +42,17 @@ func (tp TelegramPusher) Initialize() {
|
||||
func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) {
|
||||
conn, found := connections[connection]
|
||||
if !found {
|
||||
c.Log.Error().Str("conn", connection).Msg("Connection not found")
|
||||
ctx.Log.Error().Str("conn", connection).Msg("Connection not found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.Log.Debug().Str("conn", connection).Msg("Pushing data")
|
||||
ctx.Log.Debug().Str("conn", connection).Msg("Pushing data")
|
||||
conn.ProcessMessage(data)
|
||||
}
|
||||
|
||||
func (tp TelegramPusher) Shutdown() {
|
||||
c.Log.Info().Msg("Shutting down Telegram pusher...")
|
||||
ctx.Log.Info().Msg("Shutting down Telegram pusher...")
|
||||
|
||||
for _, conn := range connections {
|
||||
conn.Shutdown()
|
||||
|
Reference in New Issue
Block a user