2020-06-19 10:32:12 +00:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:48:35 +00:00
|
|
|
func (s *statpingEmailer) Valid(values notifications.Values) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:32:12 +00:00
|
|
|
var statpingMailer = &statpingEmailer{¬ifications.Notification{
|
|
|
|
Method: statpingEmailerName,
|
2020-06-21 07:16:13 +00:00
|
|
|
Title: "Email",
|
2020-06-20 00:57:34 +00:00
|
|
|
Description: "Send an email when a service becomes offline or back online using Statping's email service. You will need to verify your email address.",
|
2020-06-19 10:32:12 +00:00
|
|
|
Author: "Hunter Long",
|
|
|
|
AuthorUrl: "https://github.com/hunterlong",
|
|
|
|
Delay: time.Duration(10 * time.Second),
|
2020-06-21 06:52:07 +00:00
|
|
|
Icon: "fas envelope-square",
|
2020-06-19 10:32:12 +00:00
|
|
|
Limits: 60,
|
|
|
|
Form: []notifications.NotificationForm{{
|
|
|
|
Type: "email",
|
|
|
|
Title: "Send to Email Address",
|
2020-07-02 18:41:46 +00:00
|
|
|
Placeholder: "info@statping.com",
|
2020-06-19 10:32:12 +00:00
|
|
|
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) {
|
2020-06-21 06:52:07 +00:00
|
|
|
return "", nil
|
2020-06-19 10:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type statpingMail struct {
|
2020-06-26 04:08:12 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
Core core.Core `json:"core,omitempty"`
|
|
|
|
Service services.Service `json:"service,omitempty"`
|
|
|
|
Failure failures.Failure `json:"failure,omitempty"`
|
2020-06-19 10:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnFailure will trigger failing service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (s *statpingEmailer) OnFailure(srv services.Service, f failures.Failure) (string, error) {
|
2020-06-19 10:32:12 +00:00
|
|
|
ee := statpingMail{
|
2020-07-22 19:07:42 +00:00
|
|
|
Email: s.Host.String,
|
2020-06-26 04:08:12 +00:00
|
|
|
Core: *core.App,
|
2020-06-19 10:32:12 +00:00
|
|
|
Service: srv,
|
|
|
|
Failure: f,
|
|
|
|
}
|
2020-06-21 06:52:07 +00:00
|
|
|
return s.sendStatpingEmail(ee)
|
2020-06-19 10:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnSuccess will trigger successful service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (s *statpingEmailer) OnSuccess(srv services.Service) (string, error) {
|
2020-06-19 10:32:12 +00:00
|
|
|
ee := statpingMail{
|
2020-07-22 19:07:42 +00:00
|
|
|
Email: s.Host.String,
|
2020-06-26 04:08:12 +00:00
|
|
|
Core: *core.App,
|
2020-06-19 10:32:12 +00:00
|
|
|
Service: srv,
|
2020-06-26 04:08:12 +00:00
|
|
|
Failure: failures.Failure{},
|
2020-06-19 10:32:12 +00:00
|
|
|
}
|
2020-06-21 06:52:07 +00:00
|
|
|
return s.sendStatpingEmail(ee)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnSave will trigger when this notifier is saved
|
|
|
|
func (s *statpingEmailer) OnSave() (string, error) {
|
|
|
|
ee := statpingMail{
|
2020-07-22 19:07:42 +00:00
|
|
|
Email: s.Host.String,
|
2020-06-26 04:08:12 +00:00
|
|
|
Core: *core.App,
|
|
|
|
Service: services.Service{},
|
|
|
|
Failure: failures.Failure{},
|
2020-06-21 06:52:07 +00:00
|
|
|
}
|
2020-06-19 10:32:12 +00:00
|
|
|
out, err := s.sendStatpingEmail(ee)
|
2020-06-21 06:52:07 +00:00
|
|
|
log.Println("statping emailer response", out)
|
2020-06-19 10:32:12 +00:00
|
|
|
return out, err
|
|
|
|
}
|