2020-04-09 01:47:27 +00:00
package notifiers
import (
"fmt"
2020-10-12 22:30:21 +00:00
"net/url"
"strings"
"time"
2020-04-09 01:47:27 +00:00
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
2020-07-22 19:07:42 +00:00
"github.com/statping/statping/types/null"
2020-04-09 01:47:27 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/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
}
2020-08-03 05:48:35 +00:00
func ( t * pushover ) Valid ( values notifications . Values ) error {
return nil
}
2020-04-09 01:47:27 +00:00
var Pushover = & pushover { & notifications . 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 ,
2020-07-22 19:07:42 +00:00
SuccessData : null . NewNullString ( ` Your service ' {{ .Service .Name }} ' is currently online! ` ) ,
FailureData : null . NewNullString ( ` Your service ' {{ .Service .Name }} ' is currently offline! ` ) ,
2020-06-15 04:46:28 +00:00
DataType : "text" ,
2020-04-09 01:47:27 +00:00
Form : [ ] notifications . NotificationForm { {
Type : "text" ,
Title : "User Token" ,
2020-06-22 07:13:57 +00:00
Placeholder : "Insert your Pushover User Token" ,
2020-04-09 01:47:27 +00:00
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 ,
2020-06-22 07:13:57 +00:00
} , {
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" } ,
2020-04-09 01:47:27 +00:00
} ,
} } ,
}
2020-06-25 01:58:21 +00:00
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 :
2020-06-26 04:08:12 +00:00
return "0"
2020-06-25 01:58:21 +00:00
}
}
2020-04-09 01:47:27 +00:00
// Send will send a HTTP Post to the Pushover API. It accepts type: string
2020-04-11 05:59:51 +00:00
func ( t * pushover ) sendMessage ( message string ) ( string , error ) {
2020-04-09 01:47:27 +00:00
v := url . Values { }
2020-07-22 19:07:42 +00:00
v . Set ( "token" , t . ApiSecret . String )
v . Set ( "user" , t . ApiKey . String )
2020-04-09 01:47:27 +00:00
v . Set ( "message" , message )
2020-07-22 19:07:42 +00:00
v . Set ( "priority" , priority ( t . Var1 . String ) )
if t . Var2 . String != "" {
v . Set ( "sound" , t . Var2 . String )
2020-06-25 01:58:21 +00:00
}
2020-04-09 01:47:27 +00:00
rb := strings . NewReader ( v . Encode ( ) )
2020-05-20 06:41:50 +00:00
content , _ , err := utils . HttpRequest ( pushoverUrl , "POST" , "application/x-www-form-urlencoded" , nil , rb , time . Duration ( 10 * time . Second ) , true , nil )
2020-04-09 01:47:27 +00:00
if err != nil {
2020-04-11 05:59:51 +00:00
return "" , err
2020-04-09 01:47:27 +00:00
}
2020-04-11 05:59:51 +00:00
return string ( content ) , err
2020-04-09 01:47:27 +00:00
}
// OnFailure will trigger failing service
2020-06-26 04:08:12 +00:00
func ( t * pushover ) OnFailure ( s services . Service , f failures . Failure ) ( string , error ) {
2020-07-22 19:07:42 +00:00
message := ReplaceVars ( t . FailureData . String , s , f )
2020-06-15 07:46:27 +00:00
out , err := t . sendMessage ( message )
return out , err
2020-04-09 01:47:27 +00:00
}
// OnSuccess will trigger successful service
2020-06-26 04:08:12 +00:00
func ( t * pushover ) OnSuccess ( s services . Service ) ( string , error ) {
2020-07-22 19:07:42 +00:00
message := ReplaceVars ( t . SuccessData . String , s , failures . Failure { } )
2020-06-15 07:46:27 +00:00
out , err := t . sendMessage ( message )
return out , err
2020-04-09 01:47:27 +00:00
}
// OnTest will test the Pushover SMS messaging
2020-04-11 05:59:51 +00:00
func ( t * pushover ) OnTest ( ) ( string , error ) {
2020-06-15 07:46:27 +00:00
example := services . Example ( true )
msg := fmt . Sprintf ( "Testing the Pushover Notifier, Your service '%s' is currently offline! Error: %s" , example . Name , exampleFailure . Issue )
2020-04-11 05:59:51 +00:00
content , err := t . sendMessage ( msg )
return content , err
2020-04-09 01:47:27 +00:00
}
2020-06-21 06:52:07 +00:00
// OnSave will trigger when this notifier is saved
func ( t * pushover ) OnSave ( ) ( string , error ) {
return "" , nil
}