Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for single or multiple discord webhook urls per feed #1

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions cmd/brassite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ func runWorker(feed brassite.Feed) {
}

// Deliver to Discord
if feed.Delivery.DiscordWebhookUrl != "" {
err := brassite.DeliverToDiscord(ctx, feed.Delivery.DiscordWebhookUrl, feedItem, feed.Logo)
if err != nil {
slog.Error("Failed to deliver to Discord", slog.String("feed_name", feed.Name), slog.Any("error", err))

sentry.GetHubFromContext(ctx).CaptureException(err)
if len(feed.Delivery.DiscordWebhookUrl.Values) > 0 {
for _, url := range feed.Delivery.DiscordWebhookUrl.Values {
err := brassite.DeliverToDiscord(ctx, url, feedItem, feed.Logo)
if err != nil {
slog.Error("Failed to deliver to Discord", slog.String("feed_name", feed.Name), slog.Any("error", err))

sentry.GetHubFromContext(ctx).CaptureException(err)
}
}
}

Expand Down
64 changes: 62 additions & 2 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,73 @@ type BasicAuth struct {

type Delivery struct {
// Discord webhook URL
DiscordWebhookUrl string `json:"discord_webhook_url" yaml:"discord_webhook_url" toml:"discord_webhook_url"`
DiscordWebhookUrl DiscordWebhookUrl `json:"discord_webhook_url" yaml:"discord_webhook_url" toml:"discord_webhook_url"`
// Telegram bot token
TelegramBotToken string `json:"telegram_bot_token" yaml:"telegram_bot_token" toml:"telegram_bot_token"`
// Telegram chat ID
TelegramChatId string `json:"telegram_chat_id" yaml:"telegram_chat_id" toml:"telegram_chat_id"`
}

type DiscordWebhookUrl struct {
Values []string
}

// References: https://github.com/go-yaml/yaml/issues/100
//
// Custom unmarshaller to support reading a field as string or array of strings
func (d *DiscordWebhookUrl) UnmarshalYAML(unmarshal func(any) error) error {
var multi []string
err := unmarshal(&multi)
if err != nil {
var single string
err := unmarshal(&single)
if err != nil {
return err
}
d.Values = make([]string, 1)
d.Values[0] = single
} else {
d.Values = multi
}
return nil
}

func (d *DiscordWebhookUrl) UnmarshalJSON(data []byte) error {
var multi []string
err := json5.Unmarshal(data, &multi)
if err != nil {
var single string
err := json5.Unmarshal(data, &single)
if err != nil {
return err
}
d.Values = make([]string, 1)
d.Values[0] = single
} else {
d.Values = multi
}
return nil
}

func (d *DiscordWebhookUrl) UnmarshalTOML(data any) error {
multi, ok := data.([]any)
if ok {
var multiStrs []string
for _, item := range multi {
str, _ := item.(string)
multiStrs = append(multiStrs, str)
}
d.Values = multiStrs
return nil
} else if single, ok := data.(string); ok {
d.Values = make([]string, 1)
d.Values[0] = single
return nil
}

return fmt.Errorf("the value %v is not a string or []string", data)
}

func ParseConfiguration(configPath string) (Configuration, error) {
if configPath == "" {
return Configuration{}, fmt.Errorf("config path is empty")
Expand Down Expand Up @@ -130,7 +190,7 @@ func (c Configuration) Validate() (ok bool, issues *ValidationError) {
ok = false
}
}
if feed.Delivery.DiscordWebhookUrl == "" && feed.Delivery.TelegramBotToken == "" {
if len(feed.Delivery.DiscordWebhookUrl.Values) == 0 && feed.Delivery.TelegramBotToken == "" {
issues.AddIssue(fmt.Sprintf("feeds.%d.delivery", i), "at least one delivery method is required (otherwise what's the point?)")
ok = false
}
Expand Down
Loading