statping/notifiers/slack.go

84 lines
3.6 KiB
Go
Raw Normal View History

2020-03-12 08:09:25 +00:00
package notifiers
2018-07-10 12:05:20 +00:00
import (
"bytes"
2018-09-26 15:26:16 +00:00
"errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2018-11-25 03:56:09 +00:00
"strings"
2018-07-14 02:37:39 +00:00
"time"
2018-07-10 12:05:20 +00:00
)
2020-03-14 03:13:20 +00:00
var _ notifier.Notifier = (*slack)(nil)
2020-03-09 15:15:15 +00:00
2018-07-10 12:05:20 +00:00
const (
2018-10-06 05:05:50 +00:00
slackMethod = "slack"
failingTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "Your Statping service <{{.Service.Domain}}|{{.Service.Name}}> has just received a Failure notification based on your expected results. {{.Service.Name}} responded with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected Status Code", "value": "{{.Service.ExpectedStatus}}", "short": true }, { "title": "Received Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ,{ "title": "Error Message", "value": "{{.Failure.Issue}}", "short": false } ], "color": "#FF0000", "thumb_url": "https://statping.com", "footer": "Statping", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
2018-12-04 05:57:11 +00:00
successTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "Your Statping service <{{.Service.Domain}}|{{.Service.Name}}> is now back online and meets your expected responses.", "color": "#00FF00", "thumb_url": "https://statping.com", "footer": "Statping", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
2018-07-10 12:05:20 +00:00
)
2018-10-06 05:00:40 +00:00
type slack struct {
2020-03-14 03:13:20 +00:00
*notifications.Notification
}
2020-03-14 03:13:20 +00:00
func (s *slack) Select() *notifications.Notification {
return s.Notification
}
var slacker = &slack{&notifications.Notification{
2018-10-06 05:05:50 +00:00
Method: slackMethod,
2018-10-06 05:00:40 +00:00
Title: "slack",
Description: "Send notifications to your slack channel when a service is offline. Insert your Incoming webhook URL for your channel to receive notifications. Based on the <a href=\"https://api.slack.com/incoming-webhooks\">Slack API</a>.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(10 * time.Second),
Host: "https://webhooksurl.slack.com/***",
2018-10-21 16:22:26 +00:00
Icon: "fab fa-slack",
2020-03-17 03:13:07 +00:00
Limits: 60,
2020-03-14 03:13:20 +00:00
Form: []notifications.NotificationForm{{
2018-09-12 04:14:22 +00:00
Type: "text",
Title: "Incoming Webhook Url",
Placeholder: "Insert your Slack Webhook URL here.",
SmallText: "Incoming Webhook URL from <a href=\"https://api.slack.com/apps\" target=\"_blank\">Slack Apps</a>",
2018-09-12 04:14:22 +00:00
DbField: "Host",
2018-09-27 01:49:21 +00:00
Required: true,
2018-09-12 04:14:22 +00:00
}}},
}
// Send will send a HTTP Post to the slack webhooker API. It accepts type: string
func (s *slack) sendSlack(msg string) error {
_, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true)
if err != nil {
return err
}
2020-03-14 03:13:20 +00:00
defer resp.Body.Close()
return nil
2018-07-12 06:53:18 +00:00
}
func (s *slack) OnTest() (string, error) {
contents, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(`{"text":"testing message"}`)), time.Duration(10*time.Second), true)
if err != nil {
return "", err
}
2020-03-14 03:13:20 +00:00
defer resp.Body.Close()
2018-09-26 15:26:16 +00:00
if string(contents) != "ok" {
return string(contents), errors.New("the slack response was incorrect, check the URL")
2018-09-26 15:26:16 +00:00
}
return string(contents), nil
2018-07-10 12:05:20 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
func (s *slack) OnFailure(srv *services.Service, f *failures.Failure) error {
msg := ReplaceVars(failingTemplate, srv, f)
return s.sendSlack(msg)
2018-07-10 12:05:20 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
func (s *slack) OnSuccess(srv *services.Service) error {
msg := ReplaceVars(successTemplate, srv, nil)
return s.sendSlack(msg)
2018-07-10 12:05:20 +00:00
}