-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_notifier.go
52 lines (45 loc) · 1.03 KB
/
telegram_notifier.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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type TelegramNotifier struct {
token string
chatID string
http *http.Client
}
func NewTelegramNotifier(token, chatID string) *TelegramNotifier {
return &TelegramNotifier{
token: token,
chatID: chatID,
http: &http.Client{
Timeout: 5 * time.Second,
},
}
}
func (t *TelegramNotifier) Notify(msg string) error {
notifyURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", t.token)
form := url.Values{}
form.Set("text", msg)
form.Set("chat_id", t.chatID)
form.Set("parse_mode", "MarkdownV2")
req, err := http.NewRequest(http.MethodPost, notifyURL, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err := t.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, _ := io.ReadAll(res.Body)
return fmt.Errorf("bad response %d from telegram: %s", res.StatusCode, string(body))
}
return nil
}