Skip to content

Commit

Permalink
Merge pull request #1 from nayyara-airlangga/multiple-webhook-url
Browse files Browse the repository at this point in the history
feat: add support for single or multiple discord webhook urls per feed
  • Loading branch information
aldy505 authored May 30, 2024
2 parents cc6708f + 2d82ada commit 0506f19
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
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

0 comments on commit 0506f19

Please sign in to comment.