-
Notifications
You must be signed in to change notification settings - Fork 56
/
webhooks.go
124 lines (100 loc) · 3.51 KB
/
webhooks.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
package libknary
import (
"bytes"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"time"
)
func sendMsg(msg string) {
// closes https://github.com/sudosammy/knary/issues/20
re := regexp.MustCompile(`\r?\n`)
msg = re.ReplaceAllString(msg, "\\n")
msg = strings.ReplaceAll(msg, "\"", "\\\"")
if os.Getenv("SLACK_WEBHOOK") != "" {
jsonMsg := []byte(`{"username":"knary","icon_emoji":":bird:","text":"` + msg + `"}`)
_, err := http.Post(os.Getenv("SLACK_WEBHOOK"), "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
if os.Getenv("PUSHOVER_TOKEN") != "" && os.Getenv("PUSHOVER_USER") != "" {
jsonMsg := []byte(`{"token":"` + os.Getenv("PUSHOVER_TOKEN") + `","user":"` + os.Getenv("PUSHOVER_USER") + `","message":"` + msg + `"}`)
_, err := http.Post("https://api.pushover.net/1/messages.json/", "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
if os.Getenv("TELEGRAM_CHATID") != "" && os.Getenv("TELEGRAM_BOT_TOKEN") != "" {
msg = strings.ReplaceAll(msg, "```From:", "\nFrom:")
re = regexp.MustCompile("```\\n?")
msg = re.ReplaceAllString(msg, "")
jsonMsg := []byte(`{"chat_id": "` + os.Getenv("TELEGRAM_CHATID") + `", "text": "` + msg + `"}`)
_, err := http.Post("https://api.telegram.org/bot"+os.Getenv("TELEGRAM_BOT_TOKEN")+"/sendMessage", "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
if os.Getenv("LARK_WEBHOOK") != "" {
re = regexp.MustCompile("```\\n?")
msg = re.ReplaceAllString(msg, "")
jsonMsg := []byte("{\n")
if larkSecret := os.Getenv("LARK_SECRET"); larkSecret != "" {
// Generate signature
timestamp := time.Now().Unix()
sig, err := SignLark(os.Getenv("LARK_SECRET"), timestamp)
if err != nil {
Printy(err.Error(), 2)
}
// Add fields to payload
sigFields := fmt.Sprintf(""+
" \"timestamp\": \"%d\",\n"+
" \"sign\": \"%s\",\n", timestamp, sig)
jsonMsg = append(jsonMsg, sigFields...)
}
// Escape hell. Probably could have just backticked lol.
postBody := fmt.Sprintf(""+
" \"msg_type\": \"post\",\n"+
" \"content\": {\n"+
" \"post\": {\n"+
" \"en_us\": {\n"+
" \"title\": \"Knary Triggered 🐦\",\n"+
" \"content\": [\n"+
" [\n"+
" {\n"+
" \"tag\": \"text\",\n"+
" \"text\": \"%s\"\n"+
" }\n"+
" ]\n"+
" ]\n"+
" }\n"+
" }\n"+
" }\n"+
"}", msg)
jsonMsg = append(jsonMsg, postBody...)
_, err := http.Post(os.Getenv("LARK_WEBHOOK"), "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
if os.Getenv("DISCORD_WEBHOOK") != "" {
jsonMsg := []byte(`{"username":"knary","text":"` + msg + `"}`)
_, err := http.Post(os.Getenv("DISCORD_WEBHOOK")+"/slack", "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
if os.Getenv("TEAMS_WEBHOOK") != "" {
// swap ``` with <pre> for MS teams :face-with-rolling-eyes:
msg = strings.Replace(msg, "```", "</pre>", 2)
msg = strings.Replace(msg, "</pre>", "<pre>", 1)
jsonMsg := []byte(`{"text":"` + msg + `"}`)
_, err := http.Post(os.Getenv("TEAMS_WEBHOOK"), "application/json", bytes.NewBuffer(jsonMsg))
if err != nil {
Printy(err.Error(), 2)
}
}
// should be simple enough to add support for other webhooks here
}