2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-07-10 12:05:20 +00:00
package notifiers
import (
"bytes"
"fmt"
2018-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/core/notifier"
2018-07-14 02:37:39 +00:00
"github.com/hunterlong/statup/types"
2018-07-10 12:05:20 +00:00
"github.com/hunterlong/statup/utils"
"net/http"
2018-07-13 09:43:15 +00:00
"text/template"
2018-07-14 02:37:39 +00:00
"time"
2018-07-10 12:05:20 +00:00
)
const (
2018-07-14 02:37:39 +00:00
SLACK_ID = 2
SLACK_METHOD = "slack"
2018-07-16 07:02:33 +00:00
FAILING_TEMPLATE = ` { "attachments": [ { "fallback": "Service {{ .Service .Name }} - is currently failing", "text": "< {{ .Service .Domain }} | {{ .Service .Name }} > - Your Statup service ' {{ .Service .Name }} ' has just received a Failure notification with a HTTP Status code of {{ .Service .LastStatusCode }} .", "fields": [ { "title": "Expected", "value": " {{ .Service .Expected }} ", "short": true }, { "title": "Status Code", "value": " {{ .Service .LastStatusCode }} ", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] } `
SUCCESS_TEMPLATE = ` { "attachments": [ { "fallback": "Service {{ .Service .Name }} - is now back online", "text": "< {{ .Service .Domain }} | {{ .Service .Name }} > - Your Statup service ' {{ .Service .Name }} ' has just received a Failure notification.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Status Code", "value": " {{ .Service .LastStatusCode }} ", "short": true } ], "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] } `
2018-07-14 02:37:39 +00:00
TEST_TEMPLATE = ` { "text":" {{ . }} "} `
2018-07-10 12:05:20 +00:00
)
2018-07-11 03:06:21 +00:00
type Slack struct {
2018-09-12 04:14:22 +00:00
* notifier . Notification
2018-07-11 03:06:21 +00:00
}
2018-09-12 04:14:22 +00:00
var slacker = & Slack { & notifier . Notification {
2018-09-15 01:18:21 +00:00
Method : SLACK_METHOD ,
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-09-15 05:07:17 +00:00
CanTest : true ,
2018-09-12 04:14:22 +00:00
Form : [ ] notifier . NotificationForm { {
Type : "text" ,
Title : "Incoming Webhook Url" ,
Placeholder : "Insert your Slack webhook URL here." ,
2018-09-15 01:18:21 +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-15 01:18:21 +00:00
func sendSlack ( temp string , data interface { } ) error {
buf := new ( bytes . Buffer )
slackTemp , _ := template . New ( "slack" ) . Parse ( temp )
err := slackTemp . Execute ( buf , data )
if err != nil {
return err
}
slacker . AddQueue ( buf . String ( ) )
return nil
}
type slackMessage struct {
Service * types . Service
Template string
Time int64
}
2018-07-10 12:05:20 +00:00
// DEFINE YOUR NOTIFICATION HERE.
func init ( ) {
2018-09-12 04:14:22 +00:00
err := notifier . AddNotifier ( slacker )
2018-09-10 09:01:04 +00:00
if err != nil {
2018-09-12 04:14:22 +00:00
panic ( err )
2018-07-12 04:49:18 +00:00
}
2018-07-10 12:05:20 +00:00
}
2018-09-15 01:18:21 +00:00
func ( u * Slack ) Send ( msg interface { } ) error {
message := msg . ( string )
client := new ( http . Client )
_ , err := client . Post ( u . Host , "application/json" , bytes . NewBuffer ( [ ] byte ( message ) ) )
if err != nil {
return err
}
2018-07-12 06:53:18 +00:00
return nil
}
2018-09-15 01:18:21 +00:00
func ( u * Slack ) Select ( ) * notifier . Notification {
return u . Notification
2018-07-10 12:05:20 +00:00
}
2018-09-15 05:07:17 +00:00
func ( u * Slack ) OnTest ( n notifier . Notification ) ( bool , error ) {
2018-09-15 01:18:21 +00:00
utils . Log ( 1 , "Slack notifier loaded" )
msg := fmt . Sprintf ( "You're Statup Slack Notifier is working correctly!" )
2018-09-15 05:07:17 +00:00
err := sendSlack ( TEST_TEMPLATE , msg )
return true , err
2018-07-10 12:05:20 +00:00
}
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
2018-09-10 09:01:04 +00:00
func ( u * Slack ) OnFailure ( s * types . Service , f * types . Failure ) {
2018-09-15 01:18:21 +00:00
message := slackMessage {
Service : s ,
Template : FAILURE ,
Time : time . Now ( ) . Unix ( ) ,
2018-07-12 04:49:18 +00:00
}
2018-09-15 01:18:21 +00:00
sendSlack ( FAILING_TEMPLATE , message )
2018-07-10 12:05:20 +00:00
}
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
2018-09-10 09:01:04 +00:00
func ( u * Slack ) OnSuccess ( s * types . Service ) {
2018-07-10 12:05:20 +00:00
}
// ON SAVE OR UPDATE OF THE NOTIFIER FORM
2018-07-11 03:06:21 +00:00
func ( u * Slack ) OnSave ( ) error {
2018-09-15 01:18:21 +00:00
message := fmt . Sprintf ( "Notification %v is receiving updated information." , u . Method )
u . AddQueue ( message )
2018-07-10 12:05:20 +00:00
return nil
}