-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_hook.go
44 lines (37 loc) · 897 Bytes
/
logger_hook.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
package rice
import (
"context"
"time"
"github.com/nikoksr/notify"
"github.com/rs/zerolog"
)
type TelegramHook struct {
Notifier *notify.Notify
}
func NewTelegramHook() *TelegramHook {
return &TelegramHook{Notifier: notify.New()}
}
func (t *TelegramHook) AddTelegramBot(token, proxy string, chatID ...int64) *TelegramHook {
telegramService, err := NewTelegramService(token, proxy)
if err != nil {
return t
}
telegramService.AddReceivers(chatID...)
t.Notifier.UseServices(telegramService)
return t
}
func (t *TelegramHook) Run(e *zerolog.Event, level zerolog.Level, message string) {
if level > zerolog.DebugLevel {
go func() {
_ = t.send(level.String(), message)
}()
}
}
func (t *TelegramHook) send(title, msg string) error {
ctx, cancel := context.WithTimeout(
context.Background(),
30*time.Second,
)
defer cancel()
return t.Notifier.Send(ctx, title, msg)
}