-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalerter.go
More file actions
59 lines (46 loc) · 1.17 KB
/
alerter.go
File metadata and controls
59 lines (46 loc) · 1.17 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
package alertg
import (
"fmt"
"gopkg.in/telebot.v4"
)
type Sender interface {
Send(to telebot.Recipient, what any, opts ...any) (*telebot.Message, error)
}
type Alerter struct {
sender Sender
chatIDs []telebot.ChatID
}
func Use(sender Sender) *Alerter {
return &Alerter{
sender: sender,
}
}
func (alt *Alerter) Notify(chatIDs ...telebot.ChatID) *Alerter {
alt.chatIDs = chatIDs
return alt
}
func (alt *Alerter) Debug(format string, args ...any) error {
msg := fmt.Sprintf("ℹ️ "+format, args...)
return alt.broadcast(msg)
}
func (alt *Alerter) Info(format string, args ...any) error {
msg := fmt.Sprintf("📰 "+format, args...)
return alt.broadcast(msg)
}
func (alt *Alerter) Warn(format string, args ...any) error {
msg := fmt.Sprintf("⚠️ "+format, args...)
return alt.broadcast(msg)
}
func (alt *Alerter) Error(format string, args ...any) error {
msg := fmt.Sprintf("🚨 "+format, args...)
return alt.broadcast(msg)
}
func (alt *Alerter) broadcast(msg string) error {
var failures ErrBroadcastFailed
for _, chatID := range alt.chatIDs {
if _, err := alt.sender.Send(chatID, msg); err != nil {
failures.Add(chatID)
}
}
return failures.AsError()
}