forked from hrntknr/ntf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package backends | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func init() { | ||
backends["pushbullet"] = NewPushbullet() | ||
} | ||
|
||
const PUSHBULET_API_URL = "https://api.pushbullet.com/v2/pushes" | ||
|
||
type PushbulletConfig struct { | ||
Token string `mapstructure:"token" validate:"required"` | ||
} | ||
|
||
type Pushbullet struct { | ||
} | ||
|
||
func NewPushbullet() BackendInterface { | ||
return &Pushbullet{} | ||
} | ||
|
||
func (*Pushbullet) GetConfig() interface{} { | ||
return PushbulletConfig{} | ||
} | ||
|
||
func (*Pushbullet) Send(configIface interface{}, title string, message string, status *bool) error { | ||
config, ok := configIface.(PushbulletConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid config") | ||
} | ||
body := map[string]interface{}{ | ||
"type": "note", | ||
"title": title, | ||
"body": message, | ||
} | ||
jsonBody, err := json.Marshal(body) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest("POST", PUSHBULET_API_URL, bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.Token)) | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode < 200 || res.StatusCode >= 300 { | ||
return fmt.Errorf("pushbullet: %s", res.Status) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package backends | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func init() { | ||
backends["pushover"] = NewPushover() | ||
} | ||
|
||
const PUSHOVER_API_TOKEN = "abughxjjtuofgt89bz21mibut67j5t" | ||
const PUSHOVER_API_URL = "https://api.pushover.net/1/messages.json" | ||
|
||
type PushoverConfig struct { | ||
UserKey string `mapstructure:"user_key" validate:"required"` | ||
Device *string `mapstructure:"device" validate:"omitempty"` | ||
Priority *string `mapstructure:"priority" validate:"omitempty,oneof=emergency high normal low lowest"` | ||
Retry *int `mapstructure:"retry" validate:"omitempty,min=30"` | ||
Expire *int `mapstructure:"expire" validate:"omitempty,min=0,max=10800"` | ||
} | ||
|
||
type Pushover struct { | ||
} | ||
|
||
func NewPushover() BackendInterface { | ||
return &Pushover{} | ||
} | ||
|
||
func (*Pushover) GetConfig() interface{} { | ||
return PushoverConfig{} | ||
} | ||
|
||
func (*Pushover) Send(configIface interface{}, title string, message string, status *bool) error { | ||
config, ok := configIface.(PushoverConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid config") | ||
} | ||
|
||
body := map[string]interface{}{ | ||
"token": PUSHOVER_API_TOKEN, | ||
"user": config.UserKey, | ||
"title": title, | ||
"message": message, | ||
} | ||
if config.Device != nil { | ||
body["device"] = *config.Device | ||
} | ||
if config.Priority != nil { | ||
switch *config.Priority { | ||
case "emergency": | ||
body["priority"] = 2 | ||
case "high": | ||
body["priority"] = 1 | ||
case "normal": | ||
body["priority"] = 0 | ||
case "low": | ||
body["priority"] = -1 | ||
case "lowest": | ||
body["priority"] = -2 | ||
} | ||
} | ||
if config.Retry != nil { | ||
body["retry"] = *config.Retry | ||
} | ||
if config.Expire != nil { | ||
body["expire"] = *config.Expire | ||
} | ||
jsonBody, err := json.Marshal(body) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := http.Post(PUSHOVER_API_URL, "application/json", bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode < 200 || res.StatusCode >= 300 { | ||
return fmt.Errorf("pushover: %s", res.Status) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package backends | ||
|
||
import ( | ||
"fmt" | ||
"log/syslog" | ||
) | ||
|
||
func init() { | ||
backends["syslog"] = NewSyslog() | ||
} | ||
|
||
type SyslogConfig struct { | ||
Facility *string `mapstructure:"facility" validate:"omitempty,oneof=kern user mail daemon auth syslog lpr news uucp cron authpriv ftp local0 local1 local2 local3 local4 local5 local6 local7"` | ||
Severity *string `mapstructure:"severity" validate:"omitempty,oneof=emerg alert crit err warning notice info debug"` | ||
} | ||
|
||
type Syslog struct { | ||
} | ||
|
||
func NewSyslog() BackendInterface { | ||
return &Syslog{} | ||
} | ||
|
||
func (*Syslog) GetConfig() interface{} { | ||
return SyslogConfig{} | ||
} | ||
|
||
func (*Syslog) Send(configIface interface{}, title string, message string, status *bool) error { | ||
config, ok := configIface.(SyslogConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid config") | ||
} | ||
var facility syslog.Priority | ||
if config.Facility != nil { | ||
switch *config.Facility { | ||
case "kern": | ||
facility = syslog.LOG_KERN | ||
case "user": | ||
facility = syslog.LOG_USER | ||
case "mail": | ||
facility = syslog.LOG_MAIL | ||
case "daemon": | ||
facility = syslog.LOG_DAEMON | ||
case "auth": | ||
facility = syslog.LOG_AUTH | ||
case "syslog": | ||
facility = syslog.LOG_SYSLOG | ||
case "lpr": | ||
facility = syslog.LOG_LPR | ||
case "news": | ||
facility = syslog.LOG_NEWS | ||
case "uucp": | ||
facility = syslog.LOG_UUCP | ||
case "cron": | ||
facility = syslog.LOG_CRON | ||
case "authpriv": | ||
facility = syslog.LOG_AUTHPRIV | ||
case "ftp": | ||
facility = syslog.LOG_FTP | ||
case "local0": | ||
facility = syslog.LOG_LOCAL0 | ||
case "local1": | ||
facility = syslog.LOG_LOCAL1 | ||
case "local2": | ||
facility = syslog.LOG_LOCAL2 | ||
case "local3": | ||
facility = syslog.LOG_LOCAL3 | ||
case "local4": | ||
facility = syslog.LOG_LOCAL4 | ||
case "local5": | ||
facility = syslog.LOG_LOCAL5 | ||
case "local6": | ||
facility = syslog.LOG_LOCAL6 | ||
case "local7": | ||
facility = syslog.LOG_LOCAL7 | ||
} | ||
} else { | ||
facility = syslog.LOG_USER | ||
} | ||
var severity syslog.Priority | ||
if config.Severity != nil { | ||
switch *config.Severity { | ||
case "emerg": | ||
severity = syslog.LOG_EMERG | ||
case "alert": | ||
severity = syslog.LOG_ALERT | ||
case "crit": | ||
severity = syslog.LOG_CRIT | ||
case "err": | ||
severity = syslog.LOG_ERR | ||
case "warning": | ||
severity = syslog.LOG_WARNING | ||
case "notice": | ||
severity = syslog.LOG_NOTICE | ||
case "info": | ||
severity = syslog.LOG_INFO | ||
case "debug": | ||
severity = syslog.LOG_DEBUG | ||
} | ||
} else { | ||
severity = syslog.LOG_INFO | ||
} | ||
|
||
logger, err := syslog.New(facility|severity, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer logger.Close() | ||
logger.Info(message) | ||
|
||
return nil | ||
} |