Skip to content

Commit 0844b26

Browse files
committed
feat: make send to discord more robust
1 parent 7aa6ce2 commit 0844b26

File tree

5 files changed

+670
-336
lines changed

5 files changed

+670
-336
lines changed

config.go

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
)
6-
7-
type Telegram struct {
8-
ChatId string `yaml:"chat_id"`
9-
BotToken string `yaml:"token"`
10-
ThreadId string `yaml:"thread_id"`
11-
}
12-
13-
type DiscordDefaults struct {
14-
Username string `yaml:"username"`
15-
AvatarURL string `yaml:"avatar_url"`
16-
}
17-
18-
type Discord struct {
19-
WebhookURL string `yaml:"webhook_url"`
20-
DiscordDefaults
21-
}
22-
23-
type SubClient struct {
24-
AppId int `yaml:"app_id"`
25-
Telegram Telegram `yaml:"telegram"`
26-
Discord Discord `yaml:"discord"`
27-
}
28-
29-
// Config is user plugin configuration
30-
type Config struct {
31-
Clients []SubClient `yaml:"clients"`
32-
GotifyHost string `yaml:"gotify_host"`
33-
GotifyClientToken string `yaml:"token"`
34-
DiscordDefaults DiscordDefaults `yaml:"discord"`
35-
}
36-
37-
// DefaultConfig implements plugin.Configurer
38-
func (c *Plugin) DefaultConfig() interface{} {
39-
return &Config{
40-
Clients: []SubClient{
41-
SubClient{
42-
AppId: 0,
43-
Telegram: Telegram{
44-
ChatId: "-100123456789",
45-
BotToken: "YourBotTokenHere",
46-
ThreadId: "OptionalThreadIdHere",
47-
},
48-
Discord: Discord{
49-
WebhookURL: "",
50-
DiscordDefaults: DiscordDefaults{
51-
Username: "DefaultUsername",
52-
AvatarURL: "DefaultAvatarURL",
53-
},
54-
},
55-
},
56-
},
57-
DiscordDefaults: DiscordDefaults{
58-
Username: "DefaultUsername",
59-
AvatarURL: "DefaultAvatarURL",
60-
},
61-
GotifyHost: "ws://localhost:80",
62-
GotifyClientToken: "ExampleToken",
63-
}
64-
}
65-
66-
// ValidateAndSetConfig implements plugin.Configurer
67-
func (c *Plugin) ValidateAndSetConfig(config interface{}) error {
68-
newConfig := config.(*Config)
69-
70-
if newConfig.GotifyClientToken == "ExampleToken" {
71-
return fmt.Errorf("gotify client token is required")
72-
}
73-
for i, client := range newConfig.Clients {
74-
if client.AppId == 0 {
75-
return fmt.Errorf("gotify app id is required for client %d", i)
76-
}
77-
// Require at least one destination: Telegram or Discord
78-
if client.Telegram.BotToken == "" && client.Discord.WebhookURL == "" {
79-
return fmt.Errorf("either telegram or discord must be configured for client %d", i)
80-
}
81-
82-
if client.Telegram.BotToken != "" {
83-
if client.Telegram.ChatId == "" {
84-
return fmt.Errorf("telegram chat id is required for client %d", i)
85-
}
86-
}
87-
88-
if client.Discord.WebhookURL != "" {
89-
// very basic validation
90-
if len(client.Discord.WebhookURL) < 8 {
91-
return fmt.Errorf("discord webhook url seems invalid for client %d", i)
92-
}
93-
}
94-
}
95-
96-
c.config = newConfig
97-
return nil
98-
}
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Telegram struct {
8+
ChatId string `yaml:"chat_id"`
9+
BotToken string `yaml:"token"`
10+
ThreadId string `yaml:"thread_id"`
11+
}
12+
13+
type DiscordDefaults struct {
14+
Username string `yaml:"username"`
15+
AvatarURL string `yaml:"avatar_url"`
16+
}
17+
18+
type Discord struct {
19+
WebhookURL string `yaml:"webhook_url"`
20+
DiscordDefaults
21+
}
22+
23+
type SubClient struct {
24+
AppId int `yaml:"app_id"`
25+
Telegram Telegram `yaml:"telegram"`
26+
Discord Discord `yaml:"discord"`
27+
}
28+
29+
// Config is user plugin configuration
30+
type Config struct {
31+
Clients []SubClient `yaml:"clients"`
32+
GotifyHost string `yaml:"gotify_host"`
33+
GotifyClientToken string `yaml:"token"`
34+
DiscordDefaults DiscordDefaults `yaml:"discord"`
35+
}
36+
37+
// DefaultConfig implements plugin.Configurer
38+
func (c *Plugin) DefaultConfig() interface{} {
39+
return &Config{
40+
Clients: []SubClient{
41+
SubClient{
42+
AppId: 0,
43+
Telegram: Telegram{
44+
ChatId: "-100123456789",
45+
BotToken: "YourBotTokenHere",
46+
ThreadId: "OptionalThreadIdHere",
47+
},
48+
Discord: Discord{
49+
WebhookURL: "",
50+
DiscordDefaults: DiscordDefaults{
51+
Username: "DefaultUsername",
52+
AvatarURL: "DefaultAvatarURL",
53+
},
54+
},
55+
},
56+
},
57+
DiscordDefaults: DiscordDefaults{
58+
Username: "DefaultUsername",
59+
AvatarURL: "DefaultAvatarURL",
60+
},
61+
GotifyHost: "ws://localhost:80",
62+
GotifyClientToken: "ExampleToken",
63+
}
64+
}
65+
66+
// ValidateAndSetConfig implements plugin.Configurer
67+
func (c *Plugin) ValidateAndSetConfig(config interface{}) error {
68+
newConfig := config.(*Config)
69+
70+
if newConfig.GotifyClientToken == "ExampleToken" {
71+
return fmt.Errorf("gotify client token is required")
72+
}
73+
for i, client := range newConfig.Clients {
74+
if client.AppId == 0 {
75+
return fmt.Errorf("gotify app id is required for client %d", i)
76+
}
77+
// Require at least one destination: Telegram or Discord
78+
if client.Telegram.BotToken == "" && client.Discord.WebhookURL == "" {
79+
return fmt.Errorf("either telegram or discord must be configured for client %d", i)
80+
}
81+
82+
if client.Telegram.BotToken != "" {
83+
if client.Telegram.ChatId == "" {
84+
return fmt.Errorf("telegram chat id is required for client %d", i)
85+
}
86+
}
87+
88+
if client.Discord.WebhookURL != "" {
89+
// very basic validation
90+
if len(client.Discord.WebhookURL) < 8 {
91+
return fmt.Errorf("discord webhook url seems invalid for client %d", i)
92+
}
93+
}
94+
}
95+
96+
c.config = newConfig
97+
return nil
98+
}

0 commit comments

Comments
 (0)