Skip to content

Commit

Permalink
add slack backend
Browse files Browse the repository at this point in the history
  • Loading branch information
hrntknr committed Jul 25, 2022
1 parent 422e9ba commit 0ac9f85
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions backends/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package backends

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

func init() {
backends["slack"] = NewSlack()
}

type SlackConfig struct {
Webhook string `mapstructure:"webhook" validate:"required"`
Color *string `mapstructure:"color" validate:"omitempty,hexcolor"`
}

type Slack struct {
}

func NewSlack() BackendInterface {
return &Slack{}
}

func (*Slack) GetConfig() interface{} {
return SlackConfig{}
}

func (*Slack) Send(configIface interface{}, title string, message string, status *bool) error {
config, ok := configIface.(SlackConfig)
if !ok {
return fmt.Errorf("invalid config")
}
var color string
if config.Color != nil {
color = *config.Color
} else if status != nil {
if *status {
color = "good"
} else {
color = "danger"
}
} else {
color = "#ffffff"
}

body := map[string]interface{}{
"attachments": []map[string]interface{}{
{
"title": title,
"text": message,
"color": color,
},
},
}
jsonBody, err := json.Marshal(body)
if err != nil {
return err
}
res, err := http.Post(config.Webhook, "application/json", bytes.NewBuffer(jsonBody))
if err != nil {
return err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return fmt.Errorf("slack: %s", res.Status)
}

return nil
}

0 comments on commit 0ac9f85

Please sign in to comment.