-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiscord.go
More file actions
167 lines (148 loc) · 3.88 KB
/
Copy pathdiscord.go
File metadata and controls
167 lines (148 loc) · 3.88 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
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
package channels
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/escoffier-labs/agent-notify/internal/canonical"
)
// Compile-time assertion that *Discord satisfies the Channel interface.
var _ Channel = (*Discord)(nil)
// Color values for Discord embed sidebar (RGB ints).
const (
colorInfo = 0x3498DB // blue
colorWarn = 0xF1C40F // yellow
colorError = 0xE74C3C // red
colorSuccess = 0x2ECC71 // green
)
type Discord struct {
name string
webhookURL string
client *http.Client
}
func NewDiscord(name, webhookURL string, timeout time.Duration) *Discord {
return &Discord{
name: name,
webhookURL: webhookURL,
client: &http.Client{Timeout: timeout},
}
}
func (d *Discord) Name() string { return d.name }
func (d *Discord) Type() string { return "discord" }
type discordEmbed struct {
Title string `json:"title,omitempty"`
Description string `json:"description"`
Color int `json:"color"`
Footer *discordFooter `json:"footer,omitempty"`
Fields []discordField `json:"fields,omitempty"`
}
type discordFooter struct {
Text string `json:"text"`
}
type discordField struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline"`
}
type discordRequest struct {
Embeds []discordEmbed `json:"embeds"`
}
func (d *Discord) Send(ctx context.Context, m canonical.Message) error {
embed, err := fitDiscordEmbed(m)
if err != nil {
return err
}
payload := discordRequest{Embeds: []discordEmbed{embed}}
body, err := json.Marshal(payload)
if err != nil {
return encodingError(d.Type())
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, d.webhookURL, bytes.NewReader(body))
if err != nil {
return requestError(d.Type())
}
req.Header.Set("Content-Type", "application/json")
resp, err := d.client.Do(req)
if err != nil {
return transportError(d.Type(), "send", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return statusError(d.Type(), resp.StatusCode)
}
return nil
}
func colorFor(level string) int {
switch level {
case "warn":
return colorWarn
case "error":
return colorError
case "success":
return colorSuccess
default:
return colorInfo
}
}
func fitDiscordEmbed(m canonical.Message) (discordEmbed, error) {
title, ok := truncateRunes(titleFor(m), DiscordTitleMax)
if !ok {
return discordEmbed{}, payloadLimitError("discord")
}
desc, ok := truncateRunes(m.Body, DiscordDescriptionMax)
if !ok {
return discordEmbed{}, payloadLimitError("discord")
}
embed := discordEmbed{
Title: title,
Description: desc,
Color: colorFor(m.Level),
}
if m.Source != "" {
footer, ok := truncateRunes(m.Source, DiscordFooterMax)
if !ok {
return discordEmbed{}, payloadLimitError("discord")
}
embed.Footer = &discordFooter{Text: footer}
}
if len(m.Tags) > 0 {
tags, ok := truncateRunes(strings.Join(m.Tags, ", "), DiscordFieldValueMax)
if !ok {
return discordEmbed{}, payloadLimitError("discord")
}
embed.Fields = []discordField{{
Name: "tags",
Value: tags,
Inline: true,
}}
}
if embedCharCount(embed) > DiscordEmbedTotalMax {
// Shrink description until the documented total embed ceiling fits.
overhead := embedCharCount(embed) - len(embed.Description)
budget := DiscordEmbedTotalMax - overhead
if budget < len(truncateSuffix) {
return discordEmbed{}, payloadLimitError("discord")
}
desc, ok = truncateRunes(m.Body, budget)
if !ok {
return discordEmbed{}, payloadLimitError("discord")
}
embed.Description = desc
if embedCharCount(embed) > DiscordEmbedTotalMax {
return discordEmbed{}, payloadLimitError("discord")
}
}
return embed, nil
}
func embedCharCount(e discordEmbed) int {
n := len(e.Title) + len(e.Description)
if e.Footer != nil {
n += len(e.Footer.Text)
}
for _, f := range e.Fields {
n += len(f.Name) + len(f.Value)
}
return n
}