| 1 |
package discord |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 |
) |
| 12 |
|
| 13 |
// Config holds the settings required for sending Discord notifications. |
| 14 |
type Config struct { |
| 15 |
standard.EnumlessConfig |
| 16 |
|
| 17 |
WebhookID string `url:"host"` |
| 18 |
Token string `url:"user"` |
| 19 |
Title string ` default:"" key:"title"` |
| 20 |
Username string ` default:"" key:"username" desc:"Override the webhook default username"` |
| 21 |
Avatar string ` default:"" key:"avatar,avatarurl" desc:"Override the webhook default avatar with specified URL"` |
| 22 |
Color uint ` default:"0x50D9ff" key:"color" desc:"The color of the left border for plain messages" base:"16"` |
| 23 |
ColorError uint ` default:"0xd60510" key:"colorError" desc:"The color of the left border for error messages" base:"16"` |
| 24 |
ColorWarn uint ` default:"0xffc441" key:"colorWarn" desc:"The color of the left border for warning messages" base:"16"` |
| 25 |
ColorInfo uint ` default:"0x2488ff" key:"colorInfo" desc:"The color of the left border for info messages" base:"16"` |
| 26 |
ColorDebug uint ` default:"0x7b00ab" key:"colorDebug" desc:"The color of the left border for debug messages" base:"16"` |
| 27 |
SplitLines bool ` default:"Yes" key:"splitLines" desc:"Whether to send each line as a separate embedded item"` |
| 28 |
JSON bool ` default:"No" key:"json" desc:"Whether to send the whole message as the JSON payload instead of using it as the 'content' field"` |
| 29 |
ThreadID string ` default:"" key:"thread_id" desc:"The thread ID to send the message to"` |
| 30 |
} |
| 31 |
|
| 32 |
// Scheme defines the protocol identifier for this service's configuration URL. |
| 33 |
const Scheme = "discord" |
| 34 |
|
| 35 |
// GetURL generates a URL from the current configuration values. |
| 36 |
func (c *Config) GetURL() *url.URL { |
| 37 |
resolver := format.NewPropKeyResolver(c) |
| 38 |
|
| 39 |
return c.getURL(&resolver) |
| 40 |
} |
| 41 |
|
| 42 |
// LevelColors returns an array of colors indexed by MessageLevel. |
| 43 |
func (c *Config) LevelColors() [types.MessageLevelCount]uint { |
| 44 |
var colors [types.MessageLevelCount]uint |
| 45 |
|
| 46 |
colors[types.Unknown] = c.Color |
| 47 |
colors[types.Error] = c.ColorError |
| 48 |
colors[types.Warning] = c.ColorWarn |
| 49 |
colors[types.Info] = c.ColorInfo |
| 50 |
colors[types.Debug] = c.ColorDebug |
| 51 |
|
| 52 |
return colors |
| 53 |
} |
| 54 |
|
| 55 |
// SetURL updates the configuration from a URL representation. |
| 56 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 57 |
resolver := format.NewPropKeyResolver(c) |
| 58 |
|
| 59 |
return c.setURL(&resolver, serviceURL) |
| 60 |
} |
| 61 |
|
| 62 |
// getURL constructs a URL from configuration using the provided resolver. |
| 63 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 64 |
result := &url.URL{ |
| 65 |
User: url.User(c.Token), |
| 66 |
Host: c.WebhookID, |
| 67 |
Scheme: Scheme, |
| 68 |
RawQuery: format.BuildQuery(resolver), |
| 69 |
ForceQuery: false, |
| 70 |
} |
| 71 |
|
| 72 |
if c.JSON { |
| 73 |
result.Path = "/raw" |
| 74 |
} |
| 75 |
|
| 76 |
return result |
| 77 |
} |
| 78 |
|
| 79 |
// setURL updates the configuration from a URL using the provided resolver. |
| 80 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 81 |
c.WebhookID = serviceURL.Host |
| 82 |
c.Token = serviceURL.User.Username() |
| 83 |
|
| 84 |
if serviceURL.Path != "" { |
| 85 |
switch serviceURL.Path { |
| 86 |
case "/raw": |
| 87 |
c.JSON = true |
| 88 |
default: |
| 89 |
return ErrIllegalURLArgument |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if c.WebhookID == "" { |
| 94 |
return ErrMissingWebhookID |
| 95 |
} |
| 96 |
|
| 97 |
if len(c.Token) < 1 { |
| 98 |
return ErrMissingToken |
| 99 |
} |
| 100 |
|
| 101 |
for key, vals := range serviceURL.Query() { |
| 102 |
if key == "thread_id" { |
| 103 |
// Trim whitespace from thread_id |
| 104 |
c.ThreadID = strings.TrimSpace(vals[0]) |
| 105 |
|
| 106 |
continue |
| 107 |
} |
| 108 |
|
| 109 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 110 |
return fmt.Errorf("setting config value for key %s: %w", key, err) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
// CreatePostURLFromConfig builds a POST URL from the Discord configuration. |
| 118 |
func CreatePostURLFromConfig(config *Config) string { |
| 119 |
if config.WebhookID == "" || config.Token == "" { |
| 120 |
return "" // Invalid cases are caught in doSend |
| 121 |
} |
| 122 |
// Trim whitespace to prevent malformed URLs |
| 123 |
webhookID := strings.TrimSpace(config.WebhookID) |
| 124 |
token := strings.TrimSpace(config.Token) |
| 125 |
|
| 126 |
postURL := fmt.Sprintf("%s/%s/%s", HooksBaseURL, webhookID, token) |
| 127 |
|
| 128 |
query := url.Values{} |
| 129 |
|
| 130 |
if config.ThreadID != "" { |
| 131 |
// Append thread_id as a query parameter |
| 132 |
query.Set("thread_id", strings.TrimSpace(config.ThreadID)) |
| 133 |
} |
| 134 |
|
| 135 |
if len(query) > 0 { |
| 136 |
return postURL + "?" + query.Encode() |
| 137 |
} |
| 138 |
|
| 139 |
return postURL |
| 140 |
} |