Support ntfy tags

This commit is contained in:
Thorben Günther 2022-10-12 17:04:44 +02:00
parent 9043ccfb5e
commit 2818ba9885
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
3 changed files with 34 additions and 0 deletions

View file

@ -29,6 +29,7 @@ type labels struct {
type labelConfig struct {
Priority string
Tags []string
}
func readConfig(path string) (*config, error) {
@ -101,6 +102,16 @@ func readConfig(path string) (*config, error) {
}
}
d = labelDir.Children.Get("tags")
if d != nil {
var tags string
if err := d.ParseParams(&tags); err != nil {
return nil, err
}
labelConfig.Tags = strings.Split(tags, ",")
}
labels[fmt.Sprintf("%s:%s", labelName, name)] = *labelConfig
}
}

11
functions.go Normal file
View file

@ -0,0 +1,11 @@
package main
func sliceContains(s []string, e string) bool {
for _, v := range s {
if e == v {
return true
}
}
return false
}

12
main.go
View file

@ -100,6 +100,7 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
req.Header.Set("X-Title", title)
var priority string
var tags []string
for _, labelName := range rcv.cfg.labels.Order {
val, ok := event.CommonLabels[labelName]
if !ok {
@ -114,12 +115,23 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
if priority == "" {
priority = labelConfig.Priority
}
for _, val := range labelConfig.Tags {
if !sliceContains(tags, val) {
tags = append(tags, val)
}
}
}
if priority != "" {
req.Header.Set("X-Priority", priority)
}
tagString := strings.Join(tags, ",")
if tagString != "" {
req.Header.Set("X-Tags", tagString)
}
resp, err := client.Do(req)
if err != nil {
rcv.logger.Error(err)