-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.go
35 lines (30 loc) · 971 Bytes
/
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
package vdlog
//https://stackoverflow.com/a/36623663/1885921
//https://stackoverflow.com/questions/33126743/how-do-i-join-my-bot-to-the-channel/36623663#36623663
import (
"fmt"
"net/http"
"net/url"
)
const telegramURLTemplate = "https://api.telegram.org/bot%s/sendMessage"
func createTelegramSink(botToken, chatID string, level EventLevel) func(e Event) error {
return func(e Event) error {
if e.Level > level {
return nil
}
telegramURL := fmt.Sprintf(telegramURLTemplate, botToken)
body := url.Values{}
body.Set("text", string(e.ToIndentedJSON())) //TODO better formating
body.Set("chat_id", chatID)
resp, err := http.PostForm(telegramURL, body)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
}
//LogToTelegram allows to send events to telegram channel/chat using telegram bot api
func LogToTelegram(botToken, chatID string, level EventLevel) {
AddSink("telegram", createTelegramSink(botToken, chatID, level))
}