-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (182 loc) · 5.31 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
/*
# Telegram
Parameters:
- message
Environment variables:
- NOTIFY_TELEGRAM_TOKEN
- NOTIFY_TELEGRAM_CHAT_ID
*/
func notifyTelegram(message string) {
// Load environment variables
telegramToken, ok := os.LookupEnv("NOTIFY_TELEGRAM_TOKEN")
if !ok {
log.Fatal("NOTIFY_TELEGRAM_TOKEN environment variable is unset")
}
telegramChatID, ok := os.LookupEnv("NOTIFY_TELEGRAM_CHAT_ID")
if !ok {
log.Fatal("NOTIFY_TELEGRAM_CHAT_ID environment variable is unset")
}
endpoint := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramToken)
payload := fmt.Sprintf(`{"chat_id": "%s","text":"%s"}`, telegramChatID, message)
req, err := http.NewRequest("POST", endpoint, strings.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
log.Print("Telegram notified")
}
/*
# Line Notify
Parameters:
- message
Environment variables:
- NOTIFY_LINE_NOTIFY_TOKEN
*/
func notifyLineNotify(message string) {
// Load environment variables
lineNotifyToken, ok := os.LookupEnv("NOTIFY_LINE_NOTIFY_TOKEN")
if !ok {
log.Fatal("NOTIFY_LINE_NOTIFY_TOKEN environment variable is unset")
}
endpoint := "https://notify-api.line.me/api/notify"
data := url.Values{"message": {message}}
body := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", endpoint, body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", lineNotifyToken))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
log.Print("Line Notify notified")
}
/*
# Hangouts Chat
Parameters:
- message
Environment variables:
- NOTIFY_HANGOUTS_CHAT_WEBHOOK
*/
func notifyHangoutsChat(message string) {
// Load environment variables
hangoutsChatWebhook, ok := os.LookupEnv("NOTIFY_HANGOUTS_CHAT_WEBHOOK")
if !ok {
log.Fatal("NOTIFY_HANGOUTS_CHAT_WEBHOOK environment variable is unset")
}
payload := fmt.Sprintf(`{"text":"%s"}`, message)
req, err := http.NewRequest("POST", hangoutsChatWebhook, strings.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
log.Print("Hangouts Chat notified")
}
/*
# Slack
Parameters:
- message
Environment variables:
- NOTIFY_SLACK_WEBHOOK
*/
func notifySlack(message string) {
// Load environment variables
hangoutsChatWebhook, ok := os.LookupEnv("NOTIFY_SLACK_WEBHOOK")
if !ok {
log.Fatal("NOTIFY_SLACK_WEBHOOK environment variable is unset")
}
payload := fmt.Sprintf(`{"text":"%s"}`, message)
req, err := http.NewRequest("POST", hangoutsChatWebhook, strings.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
log.Print("Slack notified")
}
/*
# Discord
Parameters:
- message
Environment variables:
- NOTIFY_DISCORD_WEBHOOK
*/
func notifyDiscord(message string) {
// Load environment variables
hangoutsChatWebhook, ok := os.LookupEnv("NOTIFY_DISCORD_WEBHOOK")
if !ok {
log.Fatal("NOTIFY_DISCORD_WEBHOOK environment variable is unset")
}
payload := fmt.Sprintf(`{"content":"%s"}`, message)
req, err := http.NewRequest("POST", hangoutsChatWebhook, strings.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
log.Print("Discord notified")
}
func main() {
// Subcommands
telegramCmd := flag.NewFlagSet("telegram", flag.ExitOnError)
telegramMessage := telegramCmd.String("message", "This is a test message", "message")
lineNotifyCmd := flag.NewFlagSet("linenotify", flag.ExitOnError)
lineNotifyMessage := lineNotifyCmd.String("message", "This is a test message", "message")
hangoutsChatCmd := flag.NewFlagSet("hangoutschat", flag.ExitOnError)
hangoutsChatMessage := hangoutsChatCmd.String("message", "This is a test message", "message")
slackCmd := flag.NewFlagSet("slack", flag.ExitOnError)
slackMessage := slackCmd.String("message", "This is a test message", "message")
discordCmd := flag.NewFlagSet("discord", flag.ExitOnError)
discordMessage := discordCmd.String("message", "This is a test message", "message")
if len(os.Args) < 2 {
log.Println("Expected 'telegram', 'linenotify', 'hangoutschat', 'slack', or 'discord'")
os.Exit(1)
}
switch os.Args[1] {
case "telegram":
telegramCmd.Parse(os.Args[2:])
notifyTelegram(*telegramMessage)
case "linenotify":
lineNotifyCmd.Parse(os.Args[2:])
notifyLineNotify(*lineNotifyMessage)
case "hangoutschat":
hangoutsChatCmd.Parse(os.Args[2:])
notifyHangoutsChat(*hangoutsChatMessage)
case "slack":
slackCmd.Parse(os.Args[2:])
notifySlack(*slackMessage)
case "discord":
discordCmd.Parse(os.Args[2:])
notifyDiscord(*discordMessage)
default:
log.Println("Expected 'telegram', 'linenotify', 'hangoutschat', 'slack', or 'discord'")
os.Exit(1)
}
}