forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatping_emailer.go
106 lines (93 loc) · 2.93 KB
/
statping_emailer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
}
func (s *statpingEmailer) Valid(values notifications.Values) error {
return nil
}
var statpingMailer = &statpingEmailer{¬ifications.Notification{
Method: statpingEmailerName,
Title: "Email",
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.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(10 * time.Second),
Icon: "fas envelope-square",
Limits: 60,
Form: []notifications.NotificationForm{{
Type: "email",
Title: "Send to Email Address",
Placeholder: "[email protected]",
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) {
return "", nil
}
type statpingMail struct {
Email string `json:"email"`
Core core.Core `json:"core,omitempty"`
Service services.Service `json:"service,omitempty"`
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.String,
Core: *core.App,
Service: srv,
Failure: f,
}
return s.sendStatpingEmail(ee)
}
// OnSuccess will trigger successful service
func (s *statpingEmailer) OnSuccess(srv services.Service) (string, error) {
ee := statpingMail{
Email: s.Host.String,
Core: *core.App,
Service: srv,
Failure: failures.Failure{},
}
return s.sendStatpingEmail(ee)
}
// OnSave will trigger when this notifier is saved
func (s *statpingEmailer) OnSave() (string, error) {
ee := statpingMail{
Email: s.Host.String,
Core: *core.App,
Service: services.Service{},
Failure: failures.Failure{},
}
out, err := s.sendStatpingEmail(ee)
log.Println("statping emailer response", out)
return out, err
}