-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord.go
More file actions
137 lines (129 loc) · 3.35 KB
/
discord.go
File metadata and controls
137 lines (129 loc) · 3.35 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
131
132
133
134
135
136
137
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"strconv"
"time"
)
// DisordNotifier represents a nofifier for discord.
type DisordNotifier struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}
// DiscordMessage represents the payload structure used to send messages to a Discord webhook.
type DiscordMessage struct {
Content string `json:"content,omitempty"`
Username string `json:"username,omitempty"`
Embeds []DiscordEmbed `json:"embeds,omitempty"`
}
// DiscordEmbed represents embedded fields in DiscordMessage.
type DiscordEmbed struct {
Title string `json:"title,omitempty"`
Color int `json:"color,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
}
// Send dispatches a message to a discord webhook.
func (d *DisordNotifier) Send(ctx context.Context, data DiscordMessage) error {
payload, err := json.Marshal(data)
if err != nil {
log.Println("marshal discord message", err)
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.URL, bytes.NewBuffer(payload))
if err != nil {
log.Println("new request", err)
return err
}
log.Println("discord send", d.URL, string(payload))
req.Header.Set("Content-Type", "application/json")
client := http.Client{Timeout: time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
log.Println("status", resp.Status, "response", string(bytes))
return nil
}
func sendDiscordStatusNotification(ctx context.Context, notification []byte, status Status, ok int) error {
var discord DisordNotifier
if err := json.Unmarshal(notification, &discord); err != nil {
return err
}
data := DiscordMessage{
Content: "Uptime Status Alert",
Username: "Uptime",
Embeds: []DiscordEmbed{
{
Title: status.Site,
Color: discordBlue,
URL: status.URL,
Description: status.URL,
},
{
Title: "Status",
Description: status.Status,
},
},
}
if status.StatusCode != ok {
data.Embeds[0].Color = discordRed
}
return discord.Send(ctx, data)
}
func sendDiscordCertExpiryNotification(ctx context.Context, notification []byte, status Status) error {
var discord DisordNotifier
if err := json.Unmarshal(notification, &discord); err != nil {
return err
}
data := DiscordMessage{
Content: "Uptime Cert Expiry Alert",
Username: "Uptime",
Embeds: []DiscordEmbed{
{
Title: status.Site,
Color: discordRed,
URL: status.URL,
Description: status.URL,
},
{
Title: "CertExpiry",
Description: strconv.Itoa(status.CertExpiry),
},
},
}
return discord.Send(ctx, data)
}
func sendDiscordTestNotification(ctx context.Context, notification []byte) error {
var discord DisordNotifier
if err := json.Unmarshal(notification, &discord); err != nil {
return err
}
data := DiscordMessage{
Content: "Test Alert",
Username: "Uptime",
Embeds: []DiscordEmbed{
{
Title: "test message",
Color: discordRed,
URL: "https://example.com",
Description: "https://example.com",
},
{
Title: "Addition Details",
Description: "status info",
},
},
}
return discord.Send(ctx, data)
}