forked from statping-ng/statping-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushover.go
138 lines (123 loc) · 4.11 KB
/
pushover.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package notifiers
import (
"fmt"
"net/url"
"strings"
"time"
"github.com/statping-ng/statping-ng/types/failures"
"github.com/statping-ng/statping-ng/types/notifications"
"github.com/statping-ng/statping-ng/types/notifier"
"github.com/statping-ng/statping-ng/types/null"
"github.com/statping-ng/statping-ng/types/services"
"github.com/statping-ng/statping-ng/utils"
)
const (
pushoverUrl = "https://api.pushover.net/1/messages.json"
)
var _ notifier.Notifier = (*pushover)(nil)
type pushover struct {
*notifications.Notification
}
func (t *pushover) Select() *notifications.Notification {
return t.Notification
}
func (t *pushover) Valid(values notifications.Values) error {
return nil
}
var Pushover = &pushover{¬ifications.Notification{
Method: "pushover",
Title: "Pushover",
Description: "Use Pushover to receive push notifications. You will need to create a <a href=\"https://pushover.net/apps/build\">New Application</a> on Pushover before using this notifier.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Icon: "fa dot-circle",
Delay: time.Duration(10 * time.Second),
Limits: 60,
SuccessData: null.NewNullString(`Your service '{{.Service.Name}}' is currently online!`),
FailureData: null.NewNullString(`Your service '{{.Service.Name}}' is currently offline!`),
DataType: "text",
Form: []notifications.NotificationForm{{
Type: "text",
Title: "User Token",
Placeholder: "Insert your Pushover User Token",
DbField: "api_key",
Required: true,
}, {
Type: "text",
Title: "Application API Key",
Placeholder: "Create an Application and insert the API Key here",
DbField: "api_secret",
Required: true,
}, {
Type: "list",
Title: "Priority",
Placeholder: "Set the notification priority level",
DbField: "Var1",
Required: true,
ListOptions: []string{"Lowest", "Low", "Normal", "High", "Emergency"},
}, {
Type: "list",
Title: "Notification Sound",
Placeholder: "Choose a sound for this Pushover notification",
DbField: "Var2",
Required: true,
ListOptions: []string{"none", "pushover", "bike", "bugle", "cashregister", "classical", "cosmic", "falling", "gamelan", "incoming", "intermissioon", "magic", "mechanical", "painobar", "siren", "spacealarm", "tugboat", "alien", "climb", "persistent", "echo", "updown"},
},
}},
}
func priority(val string) string {
switch strings.ToLower(val) {
case "lowest":
return "-2"
case "low":
return "-1"
case "normal":
return "0"
case "high":
return "1"
case "emergency":
return "2"
default:
return "0"
}
}
// Send will send a HTTP Post to the Pushover API. It accepts type: string
func (t *pushover) sendMessage(message string) (string, error) {
v := url.Values{}
v.Set("token", t.ApiSecret.String)
v.Set("user", t.ApiKey.String)
v.Set("message", message)
v.Set("priority", priority(t.Var1.String))
if t.Var2.String != "" {
v.Set("sound", t.Var2.String)
}
rb := strings.NewReader(v.Encode())
content, _, err := utils.HttpRequest(pushoverUrl, "POST", "application/x-www-form-urlencoded", nil, rb, time.Duration(10*time.Second), true, nil)
if err != nil {
return "", err
}
return string(content), err
}
// OnFailure will trigger failing service
func (t *pushover) OnFailure(s services.Service, f failures.Failure) (string, error) {
message := ReplaceVars(t.FailureData.String, s, f)
out, err := t.sendMessage(message)
return out, err
}
// OnSuccess will trigger successful service
func (t *pushover) OnSuccess(s services.Service) (string, error) {
message := ReplaceVars(t.SuccessData.String, s, failures.Failure{})
out, err := t.sendMessage(message)
return out, err
}
// OnTest will test the Pushover SMS messaging
func (t *pushover) OnTest() (string, error) {
example := services.Example(true)
msg := fmt.Sprintf("Testing the Pushover Notifier, Your service '%s' is currently offline! Error: %s", example.Name, exampleFailure.Issue)
content, err := t.sendMessage(msg)
return content, err
}
// OnSave will trigger when this notifier is saved
func (t *pushover) OnSave() (string, error) {
return "", nil
}