2020-03-12 08:09:25 +00:00
package notifiers
2018-08-31 06:33:21 +00:00
import (
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/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-14 03:13:20 +00:00
var _ notifier . Notifier = ( * lineNotifier ) ( nil )
2020-03-09 15:15:15 +00:00
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-14 03:13:20 +00:00
* notifications . Notification
2018-09-12 04:14:22 +00:00
}
2020-03-14 03:13:20 +00:00
func ( l * lineNotifier ) Select ( ) * notifications . Notification {
return l . Notification
}
var LineNotify = & lineNotifier { & notifications . Notification {
2018-10-06 05:05:50 +00:00
Method : lineNotifyMethod ,
2018-09-15 01:18:21 +00:00
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>." ,
2018-09-15 01:18:21 +00:00
Author : "Kanin Peanviriyakulkit" ,
AuthorUrl : "https://github.com/dogrocker" ,
2018-10-21 16:22:26 +00:00
Icon : "far fa-bell" ,
2020-03-17 03:13:07 +00:00
Limits : 60 ,
2020-03-14 03:13:20 +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
2020-04-11 05:59:51 +00:00
func ( l * lineNotifier ) sendMessage ( message string ) ( string , error ) {
2018-09-15 01:18:21 +00:00
v := url . Values { }
v . Set ( "message" , message )
2020-03-23 02:50:30 +00:00
headers := [ ] string { fmt . Sprintf ( "Authorization=Bearer %v" , l . ApiSecret ) }
2020-04-11 05:59:51 +00:00
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 )
return string ( content ) , err
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
2020-03-23 02:50:30 +00:00
func ( l * lineNotifier ) OnFailure ( s * services . Service , f * failures . Failure ) error {
2018-09-15 01:18:21 +00:00
msg := fmt . Sprintf ( "Your service '%v' is currently offline!" , s . Name )
2020-04-11 05:59:51 +00:00
_ , err := l . sendMessage ( msg )
return err
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
2020-03-23 02:50:30 +00:00
func ( l * lineNotifier ) OnSuccess ( s * services . Service ) error {
2020-03-14 03:13:20 +00:00
msg := fmt . Sprintf ( "Service %s is online!" , s . Name )
2020-04-11 05:59:51 +00:00
_ , err := l . sendMessage ( msg )
return err
2018-08-31 06:33:21 +00:00
}
2020-03-14 03:13:20 +00:00
// OnTest triggers when this notifier has been saved
2020-04-11 05:59:51 +00:00
func ( l * lineNotifier ) OnTest ( ) ( string , error ) {
2020-03-14 03:13:20 +00:00
msg := fmt . Sprintf ( "Testing if Line Notifier is working!" )
2020-04-11 05:59:51 +00:00
_ , err := l . sendMessage ( msg )
return msg , err
2018-08-31 06:33:21 +00:00
}