-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (110 loc) · 3.05 KB
/
Copy pathmain.go
File metadata and controls
130 lines (110 loc) · 3.05 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
// Exit codes used by NZBGet
const POSTPROCESS_SUCCESS = 93
const POSTPROCESS_ERROR = 94
const POSTPROCESS_NONE = 95
func getEnv(key string, defaultValue string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultValue
}
func sendSlackMessage(msg string, token string, channel string) bool {
url := "https://slack.com/api/chat.postMessage"
msqJsonMap := map[string]string{
"username": "NZBGet",
"icon_emoji": ":seal:",
"unfurl_links": "false",
"channel": channel,
"text": msg,
}
msgJson, _ := json.Marshal(msqJsonMap)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(msgJson))
if err != nil {
fmt.Printf("[ERROR] error creating http request: %s", err.Error())
return false
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("[ERROR] error sending Slack message: %s", err.Error())
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("[ERROR] error sending Slack message: got status code: %s", resp.Status)
return false
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("[ERROR] error reading Slack responce body: %s", err.Error())
return false
}
var respJsonMap map[string]interface{}
if err := json.Unmarshal(body, &respJsonMap); err != nil {
fmt.Printf("[ERROR] error parsing Slack responce body JSON: %s", err.Error())
return false
}
if status, ok := respJsonMap["ok"].(bool); ok {
if !status {
log.Printf("[ERROR] error sending Slack message: %v", respJsonMap["error"])
return false
}
}
return true
}
func main() {
// get settings from environment
token := getEnv("NZBPO_SLACKTOKEN", "")
channel := getEnv("NZBPO_SLACKCHANNEL", "")
sendNotification := getEnv("NZBPO_SENDNOTIFICATION", "")
// check settings
if token == "" || channel == "" {
fmt.Println("[ERROR] Please set up token and channel")
os.Exit(POSTPROCESS_ERROR)
}
// list all environment variables and their values if debug enabled
debug := getEnv("NZBOP_SLACKNOTIFY_DEBUG", "")
if debug == "Yes" {
fmt.Println("[DETAIL] environment:")
for _, env := range os.Environ() {
fmt.Printf("[DETAIL] %s\n", env)
}
}
// process commands
command := getEnv("NZBCP_COMMAND", "")
if command == "ConnectionTest" {
if sendSlackMessage("Connection test", token, channel) {
os.Exit(POSTPROCESS_SUCCESS)
} else {
os.Exit(POSTPROCESS_ERROR)
}
}
// process script
status := getEnv("NZBPP_STATUS", "")
send := true
if status == "SUCCESS/ALL" && sendNotification == "OnFailure" {
send = false
}
if send {
nzbname := getEnv("NZBPP_NZBNAME", "")
msg := fmt.Sprintf("Download of \"%s\" completed.\nStatus: %s", nzbname, status)
if sendSlackMessage(msg, token, channel) {
os.Exit(POSTPROCESS_SUCCESS)
} else {
os.Exit(POSTPROCESS_ERROR)
}
}
os.Exit(POSTPROCESS_SUCCESS)
}