From 15493fb6bae37a1f8fa5e8fbfb670088f7505e86 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Fri, 19 Jun 2020 03:32:12 -0700 Subject: [PATCH] added Statping dedicated email notifier --- notifiers/statping_emailer.go | 103 ++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 notifiers/statping_emailer.go diff --git a/notifiers/statping_emailer.go b/notifiers/statping_emailer.go new file mode 100644 index 00000000..67ab3de2 --- /dev/null +++ b/notifiers/statping_emailer.go @@ -0,0 +1,103 @@ +package notifiers + +import ( + "bytes" + "encoding/json" + "errors" + "github.com/statping/statping/types/core" + "github.com/statping/statping/types/failures" + "github.com/statping/statping/types/notifications" + "github.com/statping/statping/types/notifier" + "github.com/statping/statping/types/services" + "github.com/statping/statping/utils" + "time" +) + +var _ notifier.Notifier = (*statpingEmailer)(nil) + +const ( + statpingEmailerName = "statping_emailer" + statpingEmailerHost = "https://news.statping.com" +) + +type statpingEmailer struct { + *notifications.Notification +} + +func (s *statpingEmailer) Select() *notifications.Notification { + return s.Notification +} + +var statpingMailer = &statpingEmailer{¬ifications.Notification{ + Method: statpingEmailerName, + Title: "Statping Emailer", + Description: "Send an email when a service becomes offline or back online using Statping's email service.", + Author: "Hunter Long", + AuthorUrl: "https://github.com/hunterlong", + Delay: time.Duration(10 * time.Second), + Icon: "fab fa-slack", + RequestInfo: "Slack allows you to customize your own messages with many complex components. Checkout the Slack Message API to learn how you can create your own.", + Limits: 60, + Form: []notifications.NotificationForm{{ + Type: "email", + Title: "Send to Email Address", + Placeholder: "Insert your email address", + DbField: "Host", + Required: true, + }}}, +} + +// Send will send a HTTP Post to the slack webhooker API. It accepts type: string +func (s *statpingEmailer) sendStatpingEmail(msg statpingMail) (string, error) { + data, _ := json.Marshal(msg) + resp, _, err := utils.HttpRequest(statpingEmailerHost+"/notifier", "POST", "application/json", nil, bytes.NewBuffer(data), time.Duration(10*time.Second), true, nil) + if err != nil { + return "", err + } + return string(resp), nil +} + +func (s *statpingEmailer) OnTest() (string, error) { + example := services.Example(true) + testMsg := ReplaceVars(s.SuccessData, example, nil) + contents, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(testMsg)), time.Duration(10*time.Second), true, nil) + if err != nil { + return "", err + } + defer resp.Body.Close() + if string(contents) != "ok" { + return string(contents), errors.New("the slack response was incorrect, check the URL") + } + return string(contents), nil +} + +type statpingMail struct { + Email string `json:"email"` + Core *core.Core `json:"core"` + Service *services.Service `json:"service"` + Failure *failures.Failure `json:"failure,omitempty"` +} + +// OnFailure will trigger failing service +func (s *statpingEmailer) OnFailure(srv *services.Service, f *failures.Failure) (string, error) { + ee := statpingMail{ + Email: s.Host, + Core: core.App, + Service: srv, + Failure: f, + } + out, err := s.sendStatpingEmail(ee) + return out, err +} + +// OnSuccess will trigger successful service +func (s *statpingEmailer) OnSuccess(srv *services.Service) (string, error) { + ee := statpingMail{ + Email: s.Host, + Core: core.App, + Service: srv, + Failure: nil, + } + out, err := s.sendStatpingEmail(ee) + return out, err +}