-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
telegram.go
128 lines (105 loc) · 3.25 KB
/
telegram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package notification
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/SyncYomi/SyncYomi/internal/domain"
"github.com/SyncYomi/SyncYomi/pkg/errors"
"github.com/rs/zerolog"
"html"
"io"
"net/http"
"time"
)
type TelegramMessage struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
ParseMode string `json:"parse_mode"`
}
type telegramSender struct {
log zerolog.Logger
Settings domain.Notification
}
func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
return &telegramSender{
log: log.With().Str("sender", "telegram").Logger(),
Settings: settings,
}
}
func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
m := TelegramMessage{
ChatID: s.Settings.Channel,
Text: s.buildMessage(event, payload),
ParseMode: "HTML",
//ParseMode: "MarkdownV2",
}
jsonData, err := json.Marshal(m)
if err != nil {
s.log.Error().Err(err).Msgf("telegram client could not marshal data: %v", m)
return errors.Wrap(err, "could not marshal data: %+v", m)
}
url := fmt.Sprintf("https://api.telegram.org/bot%v/sendMessage", s.Settings.Token)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
return errors.Wrap(err, "could not create request")
}
req.Header.Set("Content-Type", "application/json")
t := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := http.Client{Transport: t, Timeout: 30 * time.Second}
res, err := client.Do(req)
if err != nil {
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
return errors.Wrap(err, "could not make request: %+v", req)
}
body, err := io.ReadAll(res.Body)
if err != nil {
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
return errors.Wrap(err, "could not read data")
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
s.log.Error().Msgf("telegram client could not close body: %v", err)
}
}(res.Body)
s.log.Trace().Msgf("telegram status: %v response: %v", res.StatusCode, string(body))
if res.StatusCode != http.StatusOK {
s.log.Error().Err(err).Msgf("telegram client request error: %v", string(body))
return errors.New("bad status: %v body: %v", res.StatusCode, string(body))
}
s.log.Debug().Msg("notification successfully sent to telegram")
return nil
}
func (s *telegramSender) CanSend(event domain.NotificationEvent) bool {
if s.isEnabled() && s.isEnabledEvent(event) {
return true
}
return false
}
func (s *telegramSender) isEnabled() bool {
if s.Settings.Enabled && s.Settings.Token != "" && s.Settings.Channel != "" {
return true
}
return false
}
func (s *telegramSender) isEnabledEvent(event domain.NotificationEvent) bool {
for _, e := range s.Settings.Events {
if e == string(event) {
return true
}
}
return false
}
func (s *telegramSender) buildMessage(event domain.NotificationEvent, payload domain.NotificationPayload) string {
msg := ""
if payload.Subject != "" && payload.Message != "" {
msg += fmt.Sprintf("%v\n<b>%v</b>", payload.Subject, html.EscapeString(payload.Message))
}
return msg
}