|
| 1 | +package logging |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "explo/src/config" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/nikoksr/notify" |
| 13 | + "github.com/nikoksr/notify/service/discord" |
| 14 | + nhttp "github.com/nikoksr/notify/service/http" |
| 15 | + "github.com/nikoksr/notify/service/matrix" |
| 16 | + "maunium.net/go/mautrix/id" |
| 17 | +) |
| 18 | + |
| 19 | +// TODO: reuse notifier instead of creating a new one every time. right now it's fine cause Explo sends 1 message per run |
| 20 | + |
| 21 | + |
| 22 | +type NotificationClient struct { |
| 23 | + Cfg config.NotifyConfig |
| 24 | +} |
| 25 | + |
| 26 | +func InitNotify(cfg config.NotifyConfig) NotificationClient { |
| 27 | + return NotificationClient{ |
| 28 | + Cfg: cfg, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func sendMatrix(cfg config.MatrixNotif, msg string) error { |
| 33 | + // UserID and RoomID need to be cast as specific types |
| 34 | + srvc, err := matrix.New(id.UserID(cfg.UserID), id.RoomID(cfg.RoomID), cfg.HomeServer, cfg.AccessToken) |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("failed to create new Matrix notification service: %s", err.Error()) |
| 37 | + } |
| 38 | + |
| 39 | + notifier := notify.New() |
| 40 | + notifier.UseServices(srvc) |
| 41 | + |
| 42 | + err = notifier.Send(context.Background(), "Explo", msg) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +/* discordgo module (which notify uses) doesn't handle errors correctly |
| 51 | + no errors are given even when authentication fails*/ |
| 52 | +func sendDiscord(cfg config.DiscordNotif, msg string) error { |
| 53 | + srvc := discord.New() |
| 54 | + if err := srvc.AuthenticateWithBotToken(cfg.BotToken); err != nil { |
| 55 | + return fmt.Errorf("failed to authenticate against Discord: %s", err.Error()) |
| 56 | + } |
| 57 | + |
| 58 | + srvc.AddReceivers(cfg.ChannelIDs...) |
| 59 | + |
| 60 | + notifier := notify.New() |
| 61 | + notifier.UseServices(srvc) |
| 62 | + |
| 63 | + if err := notifier.Send(context.Background(), "**Explo**", msg); err != nil { |
| 64 | + return fmt.Errorf("failed to send Discord notification: %s", err.Error()) |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func sendHttp(cfg config.HttpNotif, msg string) error { |
| 71 | + httpNotify := nhttp.New() |
| 72 | + webhooks := getNotifWebhooks(cfg.ReceiverURLs) |
| 73 | + |
| 74 | + httpNotify.AddReceivers(webhooks...) |
| 75 | + notifier := notify.NewWithServices(httpNotify) |
| 76 | + |
| 77 | + if err := notifier.Send(context.Background(), "", msg); err != nil { |
| 78 | + return fmt.Errorf("failed to send HTTP notification: %s", err.Error()) |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func getNotifWebhooks(urls []string) []*nhttp.Webhook{ |
| 84 | + var webhooks []*nhttp.Webhook |
| 85 | + |
| 86 | + for _, url := range urls { |
| 87 | + |
| 88 | + webhooks = append(webhooks, &nhttp.Webhook{ |
| 89 | + ContentType: "application/json", |
| 90 | + URL: url, |
| 91 | + Method: http.MethodPost, |
| 92 | + BuildPayload: func(subject, message string) (payload any) { |
| 93 | + payl := json.RawMessage(message) |
| 94 | + return payl |
| 95 | + }, |
| 96 | + |
| 97 | + }) |
| 98 | + } |
| 99 | + return webhooks |
| 100 | +} |
| 101 | + |
| 102 | +// format notification for message client |
| 103 | +func formatRecordMsgClient(n Notification) string { |
| 104 | + attrs := make([]string, 0, len(n.Attrs)) |
| 105 | + |
| 106 | + for k, v := range n.Attrs { |
| 107 | + attrs = append(attrs, fmt.Sprintf("%s=%v", k, v)) |
| 108 | + } |
| 109 | + return fmt.Sprintf( |
| 110 | + "[%s] %s\n%s", |
| 111 | + n.Level, |
| 112 | + n.Message, |
| 113 | + strings.Join(attrs, ", "), |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +func formatRecordJSON(n Notification) string { |
| 118 | + nJSON, err := json.Marshal(n) |
| 119 | + if err != nil { |
| 120 | + slog.Error("failed to marshal notification", "err", err) |
| 121 | + } |
| 122 | + return string(nJSON) |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +func (c NotificationClient) SendNotification(n Notification) { |
| 127 | + var err error |
| 128 | + switch c.Cfg.Service { |
| 129 | + case "matrix": |
| 130 | + msg := formatRecordMsgClient(n) |
| 131 | + err = sendMatrix(c.Cfg.Matrix, msg) |
| 132 | + |
| 133 | + case "discord": |
| 134 | + msg := formatRecordMsgClient(n) |
| 135 | + err = sendDiscord(c.Cfg.Discord, msg) |
| 136 | + |
| 137 | + case "http": |
| 138 | + msg := formatRecordJSON(n) |
| 139 | + err = sendHttp(c.Cfg.Http, msg) |
| 140 | + |
| 141 | + case "": // no system defined |
| 142 | + return |
| 143 | + default: |
| 144 | + err = fmt.Errorf("wrong system defined for notifications: %s", c.Cfg.Service) |
| 145 | + } |
| 146 | + if err != nil { |
| 147 | + slog.Error(err.Error()) |
| 148 | + } else { |
| 149 | + slog.Info("notification sent", "service", c.Cfg.Service) |
| 150 | + } |
| 151 | +} |
0 commit comments