alerts updates

This commit is contained in:
Henry Dollman
2024-09-10 19:38:32 -04:00
parent 9bc7773607
commit b4cf5bb1c0

View File

@@ -20,10 +20,11 @@ import (
) )
type AlertData struct { type AlertData struct {
User *models.Record User *models.Record
Title string Title string
Message string Message string
Link string Link string
LinkText string
} }
type AlertManager struct { type AlertManager struct {
@@ -31,19 +32,9 @@ type AlertManager struct {
} }
func NewAlertManager(app *pocketbase.PocketBase) *AlertManager { func NewAlertManager(app *pocketbase.PocketBase) *AlertManager {
am := &AlertManager{ return &AlertManager{
app: app, app: app,
} }
// err := am.sendShoutrrrAlert(&mailer.Message{
// Subject: "Testing shoutrrr",
// Text: "this is a test from beszel",
// })
// if err != nil {
// log.Println("Error sending shoutrrr alert", "err", err.Error())
// }
return am
} }
func (am *AlertManager) HandleSystemAlerts(newStatus string, newRecord *models.Record, oldRecord *models.Record) { func (am *AlertManager) HandleSystemAlerts(newStatus string, newRecord *models.Record, oldRecord *models.Record) {
@@ -116,16 +107,12 @@ func (am *AlertManager) handleSlidingValueAlert(newRecord *models.Record, alertR
return return
} }
if user := alertRecord.ExpandedOne("user"); user != nil { if user := alertRecord.ExpandedOne("user"); user != nil {
// am.sendAlert(&mailer.Message{
// To: []mail.Address{{Address: user.GetString("email")}},
// Subject: subject,
// Text: body,
// })
am.sendAlert(AlertData{ am.sendAlert(AlertData{
User: user, User: user,
Title: subject, Title: subject,
Message: body, Message: body,
Link: am.app.Settings().Meta.AppUrl + "/system/" + url.QueryEscape(systemName), Link: am.app.Settings().Meta.AppUrl + "/system/" + url.QueryEscape(systemName),
LinkText: "View " + systemName,
}) })
} }
} }
@@ -160,10 +147,11 @@ func (am *AlertManager) handleStatusAlerts(newStatus string, oldRecord *models.R
// send alert // send alert
systemName := oldRecord.GetString("name") systemName := oldRecord.GetString("name")
am.sendAlert(AlertData{ am.sendAlert(AlertData{
User: user, User: user,
Title: fmt.Sprintf("Connection to %s is %s %v", systemName, alertStatus, emoji), Title: fmt.Sprintf("Connection to %s is %s %v", systemName, alertStatus, emoji),
Message: fmt.Sprintf("Connection to %s is %s", systemName, alertStatus), Message: fmt.Sprintf("Connection to %s is %s", systemName, alertStatus),
Link: am.app.Settings().Meta.AppUrl + "/system/" + url.QueryEscape(systemName), Link: am.app.Settings().Meta.AppUrl + "/system/" + url.QueryEscape(systemName),
LinkText: "View " + systemName,
}) })
return nil return nil
} }
@@ -171,7 +159,7 @@ func (am *AlertManager) handleStatusAlerts(newStatus string, oldRecord *models.R
func (am *AlertManager) sendAlert(data AlertData) { func (am *AlertManager) sendAlert(data AlertData) {
shoutrrrUrl := os.Getenv("SHOUTRRR_URL") shoutrrrUrl := os.Getenv("SHOUTRRR_URL")
if shoutrrrUrl != "" { if shoutrrrUrl != "" {
err := am.SendShoutrrrAlert(shoutrrrUrl, data.Title, data.Message, data.Link) err := am.SendShoutrrrAlert(shoutrrrUrl, data.Title, data.Message, data.Link, data.LinkText)
if err == nil { if err == nil {
log.Println("Sent shoutrrr alert") log.Println("Sent shoutrrr alert")
return return
@@ -196,7 +184,7 @@ func (am *AlertManager) sendAlert(data AlertData) {
} }
} }
func (am *AlertManager) SendShoutrrrAlert(notificationUrl string, title string, message string, link string) error { func (am *AlertManager) SendShoutrrrAlert(notificationUrl, title, message, link, linkText string) error {
supportsTitle := []string{"bark", "discord", "gotify", "ifttt", "join", "matrix", "ntfy", "opsgenie", "pushbullet", "pushover", "slack", "teams", "telegram", "zulip"} supportsTitle := []string{"bark", "discord", "gotify", "ifttt", "join", "matrix", "ntfy", "opsgenie", "pushbullet", "pushover", "slack", "teams", "telegram", "zulip"}
supportsLink := []string{"ntfy"} supportsLink := []string{"ntfy"}
// Parse the URL // Parse the URL
@@ -224,7 +212,7 @@ func (am *AlertManager) SendShoutrrrAlert(notificationUrl string, title string,
} else { } else {
// ntfy link // ntfy link
if scheme == "ntfy" { if scheme == "ntfy" {
queryParams.Add("Actions", fmt.Sprintf("view, Open Beszel, %s", am.app.Settings().Meta.AppUrl)) queryParams.Add("Actions", fmt.Sprintf("view, %s, %s", linkText, link))
} }
} }
@@ -269,7 +257,7 @@ func (am *AlertManager) SendTestNotification(c echo.Context) error {
if url == "" { if url == "" {
return c.JSON(http.StatusOK, map[string]string{"err": "URL is required"}) return c.JSON(http.StatusOK, map[string]string{"err": "URL is required"})
} }
err := am.SendShoutrrrAlert(url, "Test Alert", "This is a notification from Beszel.", am.app.Settings().Meta.AppUrl) err := am.SendShoutrrrAlert(url, "Test Alert", "This is a notification from Beszel.", am.app.Settings().Meta.AppUrl, "View Beszel")
if err != nil { if err != nil {
return c.JSON(http.StatusOK, map[string]string{"err": err.Error()}) return c.JSON(http.StatusOK, map[string]string{"err": err.Error()})
} }