-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebhook.go
129 lines (113 loc) · 3.37 KB
/
webhook.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
package main
import (
"encoding/json"
"log"
"net/url"
"github.com/valyala/fasthttp"
)
var (
liveUrl = "" // Set in main.go after env is parsed
webhookUrl = "" // Set in main.go after env is parsed
webhookLevel = WebhookLevelSimple // Set in main.go after env is parsed
)
type EmbedColor int
const (
EmbedColorGreen EmbedColor = 4388248
EmbedColorOrange EmbedColor = 14587196
EmbedColorBlue EmbedColor = 6591981
)
type WebhookLevel int64
const (
WebhookLevelAll WebhookLevel = iota // Log each beat + the below
WebhookLevelSimple // Log new devices + the below
WebhookLevelLongAbsence // Log absences longer than 1h
WebhookLevelNone // Don't log anything
)
func (wl WebhookLevel) String() string {
switch wl {
case WebhookLevelAll:
return "ALL"
case WebhookLevelSimple:
return "SIMPLE"
case WebhookLevelLongAbsence:
return "LONG_ABSENCE"
case WebhookLevelNone:
return "NONE"
default:
return WebhookLevelSimple.String()
}
}
func PostMessage(title, description string, color EmbedColor, level WebhookLevel) {
if len(webhookUrl) == 0 || len(liveUrl) == 0 {
return // hasn't been set in config/.env
}
// Log level too low
if webhookLevel > level {
return
}
type discordAuthor struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
}
type discordThumbnail struct {
Url string `json:"url,omitempty"`
}
type discordField struct {
Title string `json:"name,omitempty"`
Description string `json:"value,omitempty"`
}
type discordEmbed struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Color EmbedColor `json:"color,omitempty"`
Author discordAuthor `json:"author,omitempty"`
Thumbnail discordThumbnail `json:"thumbnail,omitempty"`
Fields []discordField `json:"fields,omitempty"`
}
type discordMessage struct {
Content string `json:"content,omitempty"`
AvatarUrl string `json:"avatar_url,omitempty"`
Username string `json:"username,omitempty"`
Embeds []discordEmbed `json:"embeds,omitempty"`
}
host := serverName
if u, err := url.Parse(liveUrl); err == nil {
host = u.Host
}
avatar := liveUrl + "/favicon.png"
author := discordAuthor{host, liveUrl, avatar}
message := discordMessage{AvatarUrl: avatar, Username: host, Embeds: []discordEmbed{{
Title: title,
Description: description,
Color: color,
Author: author,
}}}
j, err := json.Marshal(message)
if err != nil {
if *debug {
log.Printf("Error creating webhook message json: %s", err)
}
return // If this method fails, it doesn't matter to users
}
if *debug {
log.Printf("Posting webhook json: %s", j)
}
req := fasthttp.AcquireRequest()
req.SetBody(j)
req.Header.SetMethod(fasthttp.MethodPost)
req.Header.SetContentType(jsonMime)
req.SetRequestURI(webhookUrl)
res := fasthttp.AcquireResponse()
if err := fasthttp.Do(req, res); err != nil {
fasthttp.ReleaseRequest(req)
log.Printf("Error posting webhook: %s", err)
return
}
fasthttp.ReleaseRequest(req)
resBody := res.Body()
if *debug && len(resBody) > 0 {
log.Printf("Webhook response: %s", resBody)
}
fasthttp.ReleaseResponse(res) // When done with resBody
}