Moved to zerolog, linting fixes, update Drone configuration.

This commit is contained in:
2021-11-21 16:16:10 +05:00
parent c6c3e2e76f
commit 46aee4d2ab
27 changed files with 258 additions and 169 deletions

View File

@@ -18,7 +18,6 @@
package telegrampusher
import (
// local
"go.dev.pztrn.name/opensaps/context"
pusherinterface "go.dev.pztrn.name/opensaps/pushers/interface"
)

View File

@@ -18,13 +18,11 @@
package telegrampusher
import (
// stdlib
"fmt"
"net/http"
"net/url"
"strings"
// local
configstruct "go.dev.pztrn.name/opensaps/config/struct"
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
)
@@ -41,17 +39,17 @@ func (tc *TelegramConnection) ProcessMessage(message slackmessage.SlackMessage)
// Prepare message body.
messageData := c.SendToParser(message.Username, message)
messageToSend := messageData["message"].(string)
messageToSend, _ := messageData["message"].(string)
// We'll use HTML, so reformat links accordingly (if any).
linksRaw, linksFound := messageData["links"]
if linksFound {
links := linksRaw.([][]string)
links, _ := linksRaw.([][]string)
for _, link := range links {
messageToSend = strings.Replace(messageToSend, link[0], `<a href="`+link[1]+`">`+link[2]+`</a>`, -1)
messageToSend = strings.ReplaceAll(messageToSend, link[0], `<a href="`+link[1]+`">`+link[2]+`</a>`)
}
}
c.Log.Debugln("Crafted message:", messageToSend)
c.Log.Debug().Msgf("Crafted message: %s", messageToSend)
// Send message.
tc.SendMessage(messageToSend)
@@ -64,8 +62,10 @@ func (tc *TelegramConnection) SendMessage(message string) {
msgdata.Set("parse_mode", "HTML")
// Are we should use proxy?
// nolint:exhaustivestruct
httpTransport := &http.Transport{}
// nolint:nestif
if tc.config.Proxy.Enabled {
// Compose proxy URL.
proxyURL := "http://"
@@ -82,27 +82,30 @@ func (tc *TelegramConnection) SendMessage(message string) {
proxyURLParsed, err := url.Parse(proxyURL)
if err != nil {
c.Log.Errorln("Error while constructing/parsing proxy URL:", err.Error())
c.Log.Error().Err(err).Msg("Error while constructing/parsing proxy URL")
} else {
httpTransport.Proxy = http.ProxyURL(proxyURLParsed)
}
}
// nolint:exhaustivestruct
client := &http.Client{Transport: httpTransport}
botURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tc.config.BotID)
c.Log.Debugln("Bot URL:", botURL)
c.Log.Debug().Msgf("Bot URL: %s", botURL)
// ToDo: fix it.
// nolint
response, err := client.PostForm(botURL, msgdata)
if err != nil {
c.Log.Errorln("Error occurred while sending data to Telegram:", err.Error())
c.Log.Error().Err(err).Msg("Error occurred while sending data to Telegram")
} else {
c.Log.Debugln("Status:", response.Status)
if response.StatusCode != 200 {
c.Log.Debug().Msgf("Status: %s", response.Status)
if response.StatusCode != http.StatusOK {
body := []byte{}
_, _ = response.Body.Read(body)
response.Body.Close()
c.Log.Debugln(body)
c.Log.Debug().Msg(string(body))
}
}
}

View File

@@ -18,20 +18,20 @@
package telegrampusher
import (
// local
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
)
type TelegramPusher struct{}
func (tp TelegramPusher) Initialize() {
c.Log.Infoln("Initializing Telegram protocol pusher...")
c.Log.Info().Msg("Initializing Telegram protocol pusher...")
// Get configuration for pushers and initialize every connection.
cfg := c.Config.GetConfig()
for name, config := range cfg.Telegram {
c.Log.Infof("Initializing connection: '%s'", name)
c.Log.Info().Str("conn", name).Msg("Initializing connection...")
// nolint:exhaustivestruct
conn := TelegramConnection{}
connections[name] = &conn
@@ -42,16 +42,17 @@ func (tp TelegramPusher) Initialize() {
func (tp TelegramPusher) Push(connection string, data slackmessage.SlackMessage) {
conn, found := connections[connection]
if !found {
c.Log.Errorf("Connection not found: '%s'!", connection)
c.Log.Error().Str("conn", connection).Msg("Connection not found")
return
}
c.Log.Debugf("Pushing data to '%s'", connection)
c.Log.Debug().Str("conn", connection).Msg("Pushing data")
conn.ProcessMessage(data)
}
func (tp TelegramPusher) Shutdown() {
c.Log.Infoln("Shutting down Telegram pusher...")
c.Log.Info().Msg("Shutting down Telegram pusher...")
for _, conn := range connections {
conn.Shutdown()