notifier temp

pull/29/head
Hunter Long 2018-07-13 02:43:15 -07:00
parent 78fdca6430
commit 5ee6fe62ab
4 changed files with 32 additions and 20 deletions

View File

@ -17,14 +17,14 @@ func OnSuccess(s *Service) {
for _, p := range CoreApp.AllPlugins { for _, p := range CoreApp.AllPlugins {
p.OnSuccess(structs.Map(s)) p.OnSuccess(structs.Map(s))
} }
notifiers.OnSuccess() notifiers.OnSuccess(structs.Map(s))
} }
func OnFailure(s *Service, f FailureData) { func OnFailure(s *Service, f FailureData) {
for _, p := range CoreApp.AllPlugins { for _, p := range CoreApp.AllPlugins {
p.OnFailure(structs.Map(s)) p.OnFailure(structs.Map(s))
} }
notifiers.OnFailure() notifiers.OnFailure(structs.Map(s))
} }
func OnSettingsSaved(c *Core) { func OnSettingsSaved(c *Core) {

View File

@ -144,7 +144,7 @@ func (u *Email) Run() error {
} }
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Email) OnFailure() error { func (u *Email) OnFailure(data map[string]interface{}) error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here! // Do failing stuff here!
@ -153,7 +153,7 @@ func (u *Email) OnFailure() error {
} }
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Email) OnSuccess() error { func (u *Email) OnSuccess(data map[string]interface{}) error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here! // Do failing stuff here!

View File

@ -54,8 +54,8 @@ type Notifier interface {
Init() error Init() error
Install() error Install() error
Run() error Run() error
OnFailure() error OnFailure(map[string]interface{}) error
OnSuccess() error OnSuccess(map[string]interface{}) error
Select() *Notification Select() *Notification
Test() error Test() error
} }
@ -148,17 +148,17 @@ func (n *Notification) GetValue(dbField string) string {
return "" return ""
} }
func OnFailure() { func OnFailure(data map[string]interface{}) {
for _, comm := range AllCommunications { for _, comm := range AllCommunications {
n := comm.(Notifier) n := comm.(Notifier)
n.OnFailure() n.OnFailure(data)
} }
} }
func OnSuccess() { func OnSuccess(data map[string]interface{}) {
for _, comm := range AllCommunications { for _, comm := range AllCommunications {
n := comm.(Notifier) n := comm.(Notifier)
n.OnSuccess() n.OnSuccess(data)
} }
} }

View File

@ -6,11 +6,14 @@ import (
"github.com/hunterlong/statup/utils" "github.com/hunterlong/statup/utils"
"net/http" "net/http"
"time" "time"
"text/template"
) )
const ( const (
SLACK_ID = 2 SLACK_ID = 2
SLACK_METHOD = "slack" SLACK_METHOD = "slack"
SERVICE_TEMPLATE = `{ "attachments": [ { "fallback": "ReferenceError - UI is not defined: https://honeybadger.io/path/to/event/", "text": "<https://honeybadger.io/path/to/event/|Google> - Your Statup service 'Google' has just received a Failure notification.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Response", "value": "production", "short": true } ], "color": "#FF0000", "thumb_url": "http://example.com/path/to/thumb.png", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png", "ts": 123456789 } ] }`
TEST_TEMPLATE = `{"text":"%{{.Message}}"}`
) )
var ( var (
@ -63,7 +66,7 @@ func (u *Slack) Init() error {
} }
func (u *Slack) Test() error { func (u *Slack) Test() error {
SendSlack("Slack notifications on your Statup server is working!") SendSlack(TEST_TEMPLATE, nil)
return nil return nil
} }
@ -86,17 +89,16 @@ func (u *Slack) Run() error {
} }
// CUSTOM FUNCTION FO SENDING SLACK MESSAGES // CUSTOM FUNCTION FO SENDING SLACK MESSAGES
func SendSlack(msg string) error { func SendSlack(temp string, data ...interface{}) error {
//if slackUrl == "" { buf := new(bytes.Buffer)
// return errors.New("Slack Webhook URL has not been set in settings") slackTemp, _ := template.New("slack").Parse(temp)
//} slackTemp.Execute(buf, data)
fullMessage := fmt.Sprintf("{\"text\":\"%v\"}", msg) slackMessages = append(slackMessages, buf.String())
slackMessages = append(slackMessages, fullMessage)
return nil return nil
} }
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Slack) OnFailure() error { func (u *Slack) OnFailure(data map[string]interface{}) error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
// Do failing stuff here! // Do failing stuff here!
@ -105,9 +107,19 @@ func (u *Slack) OnFailure() error {
} }
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS // ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Slack) OnSuccess() error { func (u *Slack) OnSuccess(data map[string]interface{}) error {
if u.Enabled { if u.Enabled {
utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method)) utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification. %v", u.Method, data))
//domain := data["Domain"]
//expected := data["Expected"]
//expectedStatus := data["ExpectedStatus"]
failures := data["Failures"]
response := data["LastResponse"]
fullMessage := fmt.Sprintf(`{ "attachments": [ { "fallback": "Service is currently offline", "text": "Service is currently offline", "fields": [ { "title": "Issue", "value": "%v", "short": true }, { "title": "Response", "value": "%v", "short": true } ], "color": "#FF0000", "thumb_url": "http://example.com/path/to/thumb.png", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png", "ts": %v } ] }`, failures, response, time.Now().Unix())
slackMessages = append(slackMessages, fullMessage)
// Do checking or any successful things here // Do checking or any successful things here
} }
return nil return nil