forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.go
55 lines (45 loc) · 1.21 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
package checkup
import (
"fmt"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
)
// Telegram consist of all the sub components required to use Telegram API
type Telegram struct {
Token string `json:"token"`
GroupID int64 `json:"groupId"`
}
// Notify implements notifier interface
func (t Telegram) Notify(results []Result) error {
bot, err := telegram.NewBotAPI(t.Token)
if err != nil {
return err
}
for _, result := range results {
if !result.Healthy {
t.Send(result, bot)
}
}
return nil
}
// Notify implements notifier interface
func (t Telegram) NotifyAll(results []Result) error {
bot, err := telegram.NewBotAPI(t.Token)
if err != nil {
return err
}
for _, result := range results {
t.Send(result, bot)
}
return nil
}
// Send request via Telegram API
func (t Telegram) Send(result Result, bot *telegram.BotAPI) error {
var msg telegram.MessageConfig
if result.Down {
msg = telegram.NewMessage(t.GroupID, fmt.Sprintf("%s: %s is %s, error=%s", result.Title, result.Endpoint, string(result.Status()), result.Message))
} else {
msg = telegram.NewMessage(t.GroupID, fmt.Sprintf("%s: %s is %s", result.Title, result.Endpoint, string(result.Status())))
}
bot.Send(msg)
return nil
}