forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_notify.go
82 lines (70 loc) · 2.57 KB
/
line_notify.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
package notifiers
import (
"fmt"
"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"
"net/url"
"strings"
"time"
)
var _ notifier.Notifier = (*lineNotifier)(nil)
const (
lineNotifyMethod = "line_notify"
)
type lineNotifier struct {
*notifications.Notification
}
func (l *lineNotifier) Select() *notifications.Notification {
return l.Notification
}
func (l *lineNotifier) Valid(values notifications.Values) error {
return nil
}
var LineNotify = &lineNotifier{¬ifications.Notification{
Method: lineNotifyMethod,
Title: "LINE Notify",
Description: "LINE Notify will send notifications to your LINE Notify account when services are offline or online. Based on the <a href=\"https://notify-bot.line.me/doc/en/\">LINE Notify API</a>.",
Author: "Kanin Peanviriyakulkit",
AuthorUrl: "https://github.com/dogrocker",
Icon: "far fa-bell",
Limits: 60,
Form: []notifications.NotificationForm{{
Type: "text",
Title: "Access Token",
Placeholder: "Insert your Line Notify Access Token here.",
DbField: "api_secret",
}}},
}
// Send will send a HTTP Post with the Authorization to the notify-api.line.me server. It accepts type: string
func (l *lineNotifier) sendMessage(message string) (string, error) {
v := url.Values{}
v.Set("message", message)
headers := []string{fmt.Sprintf("Authorization=Bearer %v", l.ApiSecret.String)}
content, _, err := utils.HttpRequest("https://notify-api.line.me/api/notify", "POST", "application/x-www-form-urlencoded", headers, strings.NewReader(v.Encode()), time.Duration(10*time.Second), true, nil)
return string(content), err
}
// OnFailure will trigger failing service
func (l *lineNotifier) OnFailure(s services.Service, f failures.Failure) (string, error) {
msg := fmt.Sprintf("Your service '%v' is currently offline! %s", s.Name, f.Issue)
out, err := l.sendMessage(msg)
return out, err
}
// OnSuccess will trigger successful service
func (l *lineNotifier) OnSuccess(s services.Service) (string, error) {
msg := fmt.Sprintf("Service %s is online!", s.Name)
out, err := l.sendMessage(msg)
return out, err
}
// OnTest triggers when this notifier has been saved
func (l *lineNotifier) OnTest() (string, error) {
msg := fmt.Sprintf("Testing if Line Notifier is working!")
_, err := l.sendMessage(msg)
return msg, err
}
// OnSave will trigger when this notifier is saved
func (l *lineNotifier) OnSave() (string, error) {
return "", nil
}