-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage-gotify.go
112 lines (101 loc) · 3.25 KB
/
message-gotify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
var config GotifyConfig
var message GotifyMessage
var configPath = "/data/etc/gotify.json"
type GotifyConfig struct {
ApiKey string
ServerUrl string
MotionDetectedTitle string
MotionDetectedMessage string
MotionDetectedPriority int
MediaUploadedTitle string
MediaUploadedMessage string
MediaUploadedPriority int
}
type GotifyMessage struct {
Message string `json:"message"`
Priority int `json:"priority"`
Title string `json:"title"`
}
func main() {
if os.Getenv("MGDEBUG") == "1" {
configPath = "./gotify.json"
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
defaultConfig := GotifyConfig{
ApiKey: "4p1k3y",
ServerUrl: "https://gotify.urhost.example",
MotionDetectedTitle: "{cN} Motion",
MotionDetectedMessage: "Motion was detected by {cN}",
MotionDetectedPriority: 2,
MediaUploadedTitle: "Media Uploaded",
MediaUploadedMessage: "{cN} uploaded file {f}",
MediaUploadedPriority: 2,
}
defaultConfigJson, _ := json.Marshal(defaultConfig)
err = ioutil.WriteFile(configPath, defaultConfigJson, 0600)
handleError("json marshal failed: ", err)
println("Default config generated at " + configPath)
os.Exit(0)
}
configFile, err := ioutil.ReadFile(configPath)
handleError("Failed to read config file: ", err)
err = json.Unmarshal(configFile, &config)
handleError("Failed to parse config file: ", err)
flag.Parse()
event := flag.Arg(0)
cameraName := flag.Arg(1)
filename := flag.Arg(2)
if event != "motion" && (event != "media" && filename != "") && cameraName != "" {
println("usage: message-gotify <motion|media> cameraname filename")
os.Exit(1)
}
if event == "motion" {
message = createMessage(config.MotionDetectedTitle, config.MotionDetectedMessage, config.MotionDetectedPriority, cameraName, filename)
} else {
message = createMessage(config.MediaUploadedTitle, config.MediaUploadedMessage, config.MediaUploadedPriority, cameraName, filename)
}
jsonMessage, err := json.Marshal(&message)
handleError("json marshal failed: ", err)
req, err := http.NewRequest("POST", config.ServerUrl+"/message", bytes.NewBuffer(jsonMessage))
handleError("Failed to create request: ", err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Gotify-Key", config.ApiKey)
resp, err := http.DefaultClient.Do(req)
handleError("HTTP POST failed: ", err)
if resp.StatusCode != 200 {
handleError("", errors.New("received a non 200 response"))
}
}
func handleError(message string, err error) {
if err != nil {
_, err2 := fmt.Fprint(os.Stderr, message, err)
if err2 != nil {
println(err)
println(err2)
}
os.Exit(1)
}
}
func createMessage(messageTitle string, messageText string, priority int, cameraName string, filename string) GotifyMessage {
messageTitle = strings.Replace(messageTitle, "{f}", filename, -1)
messageTitle = strings.Replace(messageTitle, "{cN}", cameraName, -1)
messageText = strings.Replace(messageText, "{f}", filename, -1)
messageText = strings.Replace(messageText, "{cN}", cameraName, -1)
return GotifyMessage{
Title: messageTitle,
Message: messageText,
Priority: priority,
}
}