statping/notifiers/slack.go

95 lines
4.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"
"github.com/statping/statping/types/null"
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 (
slackMethod = "slack"
)
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: null.NewNullString("https://webhooksurl.slack.com/***"),
2018-10-21 16:22:26 +00:00
Icon: "fab fa-slack",
SuccessData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "The service {{.Service.Name}} is back online." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Service", "emoji": true }, "style": "primary", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`),
FailureData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: The service {{.Service.Name}} is currently offline! :warning:" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Service:*\n{{.Service.Name}}" }, { "type": "mrkdwn", "text": "*URL:*\n{{.Service.Domain}}" }, { "type": "mrkdwn", "text": "*Status Code:*\n{{.Service.LastStatusCode}}" }, { "type": "mrkdwn", "text": "*When:*\n{{.Failure.CreatedAt}}" }, { "type": "mrkdwn", "text": "*Downtime:*\n{{.Service.DowntimeAgo}}" }, { "type": "plain_text", "text": "*Error:*\n{{.Failure.Issue}}" } ] }, { "type": "divider" }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Offline Service", "emoji": true }, "style": "danger", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`),
DataType: "json",
RequestInfo: "Slack allows you to customize your own messages with many complex components. Checkout the <a target=\"_blank\" href=\"https://api.slack.com/reference/surfaces/formatting\">Slack Message API</a> to learn how you can create your own.",
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) (string, error) {
resp, _, err := utils.HttpRequest(s.Host.String, "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true, nil)
if err != nil {
return "", err
}
return string(resp), nil
2018-07-12 06:53:18 +00:00
}
func (s *slack) OnTest() (string, error) {
example := services.Example(true)
testMsg := ReplaceVars(s.SuccessData.String, example, failures.Failure{})
contents, resp, err := utils.HttpRequest(s.Host.String, "POST", "application/json", nil, bytes.NewBuffer([]byte(testMsg)), time.Duration(10*time.Second), true, nil)
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) (string, error) {
msg := ReplaceVars(s.FailureData.String, srv, f)
out, err := s.sendSlack(msg)
return out, err
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) (string, error) {
msg := ReplaceVars(s.SuccessData.String, srv, failures.Failure{})
out, err := s.sendSlack(msg)
return out, err
2018-07-10 12:05:20 +00:00
}
2020-06-21 06:52:07 +00:00
// OnSave will trigger when this notifier is saved
func (s *slack) OnSave() (string, error) {
return "", nil
}