statping/notifiers/line_notify.go

92 lines
3.0 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-31 06:33:21 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-31 06:33:21 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package notifiers
import (
"fmt"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/failures"
"github.com/hunterlong/statping/types/notifications"
"github.com/hunterlong/statping/types/services"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/utils"
2018-08-31 06:33:21 +00:00
"net/url"
"strings"
2018-11-25 03:56:09 +00:00
"time"
2018-08-31 06:33:21 +00:00
)
2020-03-09 15:15:15 +00:00
var _ notifications.Notifier = (*lineNotifier)(nil)
2018-08-31 06:33:21 +00:00
const (
2020-03-09 15:15:15 +00:00
lineNotifyMethod = "line_notify"
2018-08-31 06:33:21 +00:00
)
2018-10-06 05:00:40 +00:00
type lineNotifier struct {
2020-03-04 10:29:00 +00:00
*notifications.Notification
2018-09-12 04:14:22 +00:00
}
2020-03-04 10:29:00 +00:00
var LineNotify = &lineNotifier{&notifications.Notification{
2018-10-06 05:05:50 +00:00
Method: lineNotifyMethod,
Title: "LINE Notify",
2019-01-17 21:11:43 +00:00
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",
2018-10-21 16:22:26 +00:00
Icon: "far fa-bell",
2020-03-04 10:29:00 +00:00
Form: []notifications.NotificationForm{{
2018-09-12 04:14:22 +00:00
Type: "text",
Title: "Access Token",
Placeholder: "Insert your Line Notify Access Token here.",
DbField: "api_secret",
}}},
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// Send will send a HTTP Post with the Authorization to the notify-api.line.me server. It accepts type: string
2018-10-06 05:00:40 +00:00
func (u *lineNotifier) Send(msg interface{}) error {
message := msg.(string)
v := url.Values{}
v.Set("message", message)
headers := []string{fmt.Sprintf("Authorization=Bearer %v", u.ApiSecret)}
_, _, 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)
2018-11-25 03:56:09 +00:00
return err
2018-08-31 06:33:21 +00:00
}
2020-03-04 10:29:00 +00:00
func (u *lineNotifier) Select() *notifications.Notification {
return u.Notification
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
2020-03-04 10:29:00 +00:00
func (u *lineNotifier) OnFailure(s *services.Service, f *failures.Failure) {
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
2019-03-04 17:18:50 +00:00
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
2020-03-04 10:29:00 +00:00
func (u *lineNotifier) OnSuccess(s *services.Service) {
if !s.Online || !s.SuccessNotified {
var msg string
msg = s.DownText
2019-03-04 17:18:50 +00:00
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
u.AddQueue(fmt.Sprintf("service_%v", s.Id), msg)
}
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSave triggers when this notifier has been saved
2018-10-06 05:00:40 +00:00
func (u *lineNotifier) OnSave() error {
msg := fmt.Sprintf("Notification %v is receiving updated information.", u.Method)
utils.Log.Infoln(msg)
2019-10-24 23:17:05 +00:00
u.AddQueue("saved", msg)
2018-08-31 06:33:21 +00:00
return nil
}