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-07-22 19:07:42 +00:00
"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 (
2020-06-15 04:46:28 +00:00
slackMethod = "slack"
)
2018-10-06 05:00:40 +00:00
type slack struct {
2020-03-14 03:13:20 +00:00
* notifications . Notification
2018-07-11 03:06:21 +00:00
}
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" ,
2020-04-04 00:28:09 +00:00
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>." ,
2018-09-15 01:18:21 +00:00
Author : "Hunter Long" ,
AuthorUrl : "https://github.com/hunterlong" ,
Delay : time . Duration ( 10 * time . Second ) ,
2018-10-21 16:22:26 +00:00
Icon : "fab fa-slack" ,
2020-07-22 19:07:42 +00:00
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 }} " } ] } ] } ` ) ,
2020-07-30 23:14:41 +00:00
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 .Downtime .Human }} " }, { "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 }} " } ] } ] } ` ) ,
2020-06-15 04:46:28 +00:00
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" ,
2020-04-04 00:28:09 +00:00
Title : "Incoming Webhook Url" ,
2020-07-30 23:14:41 +00:00
Placeholder : "https://hooks.slack.com/services/ETJ1B87WE/H76D6G8S30/H4d97R4EcZ40SpfyqPlAHr" ,
2020-04-04 00:28:09 +00:00
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
} } } ,
}
2020-03-23 02:50:30 +00:00
// Send will send a HTTP Post to the slack webhooker API. It accepts type: string
2020-06-15 07:46:27 +00:00
func ( s * slack ) sendSlack ( msg string ) ( string , error ) {
2020-07-22 19:07:42 +00:00
resp , _ , err := utils . HttpRequest ( s . Host . String , "POST" , "application/json" , nil , strings . NewReader ( msg ) , time . Duration ( 10 * time . Second ) , true , nil )
2018-09-15 01:18:21 +00:00
if err != nil {
2020-06-15 07:46:27 +00:00
return "" , err
2018-09-15 01:18:21 +00:00
}
2020-06-15 07:46:27 +00:00
return string ( resp ) , nil
2018-07-12 06:53:18 +00:00
}
2020-04-11 05:59:51 +00:00
func ( s * slack ) OnTest ( ) ( string , error ) {
2020-06-15 07:46:27 +00:00
example := services . Example ( true )
2020-07-22 19:07:42 +00:00
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 )
2020-03-23 02:50:30 +00:00
if err != nil {
2020-04-11 05:59:51 +00:00
return "" , err
2020-03-23 02:50:30 +00:00
}
2020-03-14 03:13:20 +00:00
defer resp . Body . Close ( )
2018-09-26 15:26:16 +00:00
if string ( contents ) != "ok" {
2020-04-11 05:59:51 +00:00
return string ( contents ) , errors . New ( "the slack response was incorrect, check the URL" )
2018-09-26 15:26:16 +00:00
}
2020-04-11 05:59:51 +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
2020-06-26 04:08:12 +00:00
func ( s * slack ) OnFailure ( srv services . Service , f failures . Failure ) ( string , error ) {
2020-07-22 19:07:42 +00:00
msg := ReplaceVars ( s . FailureData . String , srv , f )
2020-06-15 07:46:27 +00:00
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
2020-06-26 04:08:12 +00:00
func ( s * slack ) OnSuccess ( srv services . Service ) ( string , error ) {
2020-07-22 19:07:42 +00:00
msg := ReplaceVars ( s . SuccessData . String , srv , failures . Failure { } )
2020-06-15 07:46:27 +00:00
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
}
2020-08-03 05:48:35 +00:00
func ( s * slack ) Valid ( values notifications . Values ) error {
return nil
}