awl-ntfy/ntfy.go

65 lines
1.3 KiB
Go
Raw Permalink Normal View History

2025-07-01 22:40:05 +02:00
package main
import (
"fmt"
"net/http"
"strings"
)
2025-07-07 18:01:22 +02:00
var NotifyChannel string
2025-08-12 11:16:40 +02:00
func SendAwlNotification(binColors []string, pink, yellow, blue, gray, brown bool) error {
2025-07-01 22:40:05 +02:00
var bins = make([]string, 0, 5)
for _, color := range binColors {
switch color {
case "pink":
2025-08-12 11:16:40 +02:00
if pink {
bins = append(bins, "🟣 Pink")
}
2025-07-01 22:40:05 +02:00
case "gelb":
2025-08-12 11:16:40 +02:00
if yellow {
bins = append(bins, "🟡 Gelb")
}
2025-07-01 22:40:05 +02:00
case "blau":
2025-08-12 11:16:40 +02:00
if blue {
bins = append(bins, "🔵 Blau")
}
2025-07-01 22:40:05 +02:00
case "grau":
2025-08-12 11:16:40 +02:00
if gray {
bins = append(bins, "⚫ Grau")
}
2025-07-01 22:40:05 +02:00
case "braun":
2025-08-12 11:16:40 +02:00
if brown {
bins = append(bins, "🟤 Braun")
}
2025-07-01 22:40:05 +02:00
}
}
2025-08-12 11:16:40 +02:00
if len(bins) == 0 {
return nil
}
2025-07-01 22:40:05 +02:00
var message string
if len(bins) == 1 {
message = "Morgen wird folgende Tonne abgeholt: "
} else {
message = "Morgen werden folgende Tonnen abgeholt: "
}
2025-07-29 09:50:26 +02:00
message += "\n"
message += strings.Join(bins, "\n")
2025-07-07 18:01:22 +02:00
return SendNotification(message)
2025-07-01 22:40:05 +02:00
}
2025-07-07 18:01:22 +02:00
func SendNotification(message string) error {
topic := fmt.Sprintf("https://ntfy.sh/awl-neuss-%s", NotifyChannel)
2025-07-01 22:40:05 +02:00
_, err := http.Post(topic, "text/plain", strings.NewReader(message))
return err
}
2025-07-07 18:01:22 +02:00
func SendErr(err error) error {
fmt.Println(err)
2025-08-23 22:49:22 +02:00
topic := "https://ntfy.sh/awl-neuss-err"
_, err = http.Post(topic, "text/plain", strings.NewReader(NotifyChannel + "\n" + err.Error()))
2025-07-01 22:40:05 +02:00
return err
}