-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler.go
More file actions
116 lines (93 loc) · 2.52 KB
/
Copy pathhandler.go
File metadata and controls
116 lines (93 loc) · 2.52 KB
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
package slogtelegram
import (
"context"
"fmt"
"log/slog"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
slogcommon "github.com/samber/slog-common"
)
// curl -X POST \
// -H 'Content-Type: application/json' \
// -d '{"chat_id": "<your-chat-id>", "text": "This is a test from curl", "disable_notification": true}' \
// https://api.telegram.org/bot<your-bot-token>/sendMessage
type Option struct {
// log level (default: debug)
Level slog.Leveler
// Telegram bot token
Token string
// Username of the channel in the form of `@username`
Username string
// optional: customize Telegram message builder
Converter Converter
MessageConfigurator MessageConfigurator
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
func (o Option) NewTelegramHandler() slog.Handler {
if o.Level == nil {
o.Level = slog.LevelDebug
}
if o.Token == "" {
panic("missing Telegram token")
}
if o.Username == "" {
panic("missing Telegram username")
}
if o.Converter == nil {
o.Converter = DefaultConverter
}
client, err := tgbotapi.NewBotAPI(o.Token)
if err != nil {
fmt.Println("slog-telegram:", err)
return nil
}
return &TelegramHandler{
option: o,
client: client,
attrs: []slog.Attr{},
groups: []string{},
}
}
var _ slog.Handler = (*TelegramHandler)(nil)
type TelegramHandler struct {
option Option
client *tgbotapi.BotAPI
attrs []slog.Attr
groups []string
}
func (h *TelegramHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.option.Level.Level()
}
func (h *TelegramHandler) Handle(ctx context.Context, record slog.Record) error {
message := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, h.attrs, h.groups, &record)
msg := tgbotapi.NewMessageToChannel(h.option.Username, message)
if h.option.MessageConfigurator != nil {
msg = h.option.MessageConfigurator(msg, h.attrs)
}
// non-blocking
go func() {
_, _ = h.client.Send(msg)
}()
return nil
}
func (h *TelegramHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &TelegramHandler{
option: h.option,
client: h.client,
attrs: slogcommon.AppendAttrsToGroup(h.groups, h.attrs, attrs...),
groups: h.groups,
}
}
func (h *TelegramHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return &TelegramHandler{
option: h.option,
client: h.client,
attrs: h.attrs,
groups: append(h.groups, name),
}
}