2018-08-31 06:33:21 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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"
2018-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/core/notifier"
2018-08-31 06:33:21 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"net/http"
"net/url"
"strings"
)
const (
2018-10-06 05:05:50 +00:00
lineNotifyMethod = "line notify"
2018-08-31 06:33:21 +00:00
)
2018-10-06 05:00:40 +00:00
type lineNotifier struct {
2018-09-12 04:14:22 +00:00
* notifier . Notification
}
2018-10-06 05:00:40 +00:00
var lineNotify = & lineNotifier { & notifier . Notification {
2018-10-06 05:05:50 +00:00
Method : lineNotifyMethod ,
2018-09-15 01:18:21 +00:00
Title : "LINE Notify" ,
Description : "LINE Notify will send notifications to your LINE Notify account when services are offline or online. Baed on the <a href=\"https://notify-bot.line.me/doc/en/\">LINE Notify API</a>." ,
Author : "Kanin Peanviriyakulkit" ,
AuthorUrl : "https://github.com/dogrocker" ,
2018-09-12 04:14:22 +00:00
Form : [ ] notifier . NotificationForm { {
Type : "text" ,
Title : "Access Token" ,
Placeholder : "Insert your Line Notify Access Token here." ,
DbField : "api_secret" ,
} } } ,
2018-08-31 06:33:21 +00:00
}
// DEFINE YOUR NOTIFICATION HERE.
func init ( ) {
2018-09-12 04:14:22 +00:00
err := notifier . AddNotifier ( lineNotify )
2018-09-10 09:01:04 +00:00
if err != nil {
2018-09-15 01:18:21 +00:00
panic ( err )
2018-09-10 09:01:04 +00:00
}
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 {
2018-09-15 01:18:21 +00:00
message := msg . ( string )
client := new ( http . Client )
v := url . Values { }
v . Set ( "message" , message )
req , err := http . NewRequest ( "POST" , "https://notify-api.line.me/api/notify" , strings . NewReader ( v . Encode ( ) ) )
req . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %v" , u . GetValue ( "api_secret" ) ) )
req . Header . Add ( "Accept" , "application/json" )
req . Header . Add ( "Content-Type" , "application/x-www-form-urlencoded" )
_ , err = client . Do ( req )
if err != nil {
return err
}
2018-08-31 06:33:21 +00:00
return nil
}
2018-10-06 05:00:40 +00:00
func ( u * lineNotifier ) Select ( ) * notifier . Notification {
2018-09-15 01:18:21 +00:00
return u . Notification
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
2018-10-06 05:00:40 +00:00
func ( u * lineNotifier ) OnFailure ( s * types . Service , f * types . Failure ) {
2018-09-15 01:18:21 +00:00
msg := fmt . Sprintf ( "Your service '%v' is currently offline!" , s . Name )
u . AddQueue ( msg )
2018-09-20 09:46:51 +00:00
u . Online = false
2018-08-31 06:33:21 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
2018-10-06 05:00:40 +00:00
func ( u * lineNotifier ) OnSuccess ( s * types . Service ) {
2018-09-20 09:46:51 +00:00
if ! u . Online {
msg := fmt . Sprintf ( "Your service '%v' is back online!" , s . Name )
u . AddQueue ( msg )
}
u . Online = true
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 {
2018-08-31 06:33:21 +00:00
utils . Log ( 1 , fmt . Sprintf ( "Notification %v is receiving updated information." , u . Method ) )
// Do updating stuff here
return nil
}