Skip to content

Commit b92e7f7

Browse files
committed
discord: multiple discord feature
1 parent fe4aa6d commit b92e7f7

File tree

4 files changed

+116
-74
lines changed

4 files changed

+116
-74
lines changed

cmd/tob/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func main() {
4545
}
4646

4747
// init Notificator
48-
notificators, err := tob.InitNotificatorFactory(configs)
48+
notificators, err := tob.InitNotificatorFactory(configs, args.Verbose)
4949
if err != nil {
5050
fmt.Println("error: ", err)
5151
os.Exit(1)

notificator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ type Notificator interface {
2323
}
2424

2525
// InitNotificatorFactory will init all notificator
26-
func InitNotificatorFactory(configs config.Config) ([]Notificator, error) {
26+
func InitNotificatorFactory(configs config.Config, verbose bool) ([]Notificator, error) {
2727
// discord notificator
28-
discordNotificator, err := discord.NewDiscord(configs)
28+
discordNotificator, err := discord.NewDiscord(configs, verbose, Logger)
2929
if err != nil {
3030
return nil, err
3131
}

notificators/discord/discord.go

Lines changed: 110 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"github.com/telkomdev/tob/config"
99
"github.com/telkomdev/tob/httpx"
10+
"io"
11+
"log"
1012
"strings"
1113
)
1214

@@ -23,8 +25,8 @@ type DiscordResponse struct {
2325
Message string `json:"message"`
2426
}
2527

26-
// Discord represent discord notificator
27-
type Discord struct {
28+
// DiscordConfig represent discord config
29+
type DiscordConfig struct {
2830
threadURL string
2931
name string
3032
avatarURL string
@@ -33,67 +35,89 @@ type Discord struct {
3335
enabled bool
3436
}
3537

38+
// Discord represent discord notificator
39+
type Discord struct {
40+
configs []DiscordConfig
41+
logger *log.Logger
42+
verbose bool
43+
}
44+
3645
// NewDiscord Discord's constructor
37-
func NewDiscord(configs config.Config) (*Discord, error) {
46+
func NewDiscord(configs config.Config, verbose bool, logger *log.Logger) (*Discord, error) {
3847
notificatorConfigInterface, ok := configs["notificator"]
3948
if !ok {
4049
return nil, errors.New("error: cannot find notificator field in the config file")
4150
}
4251

4352
notificatorConfig := notificatorConfigInterface.(map[string]interface{})
4453

45-
discordConfigInterface, ok := notificatorConfig["discord"]
54+
discordConfigInterfaces, ok := notificatorConfig["discord"]
4655
if !ok {
4756
return nil, errors.New("error: cannot find discord field in the config file")
4857
}
4958

50-
discordConfig := discordConfigInterface.(map[string]interface{})
59+
var discordConfigs []DiscordConfig
60+
discordConfigList := discordConfigInterfaces.([]interface{})
61+
for _, discordConfigInterface := range discordConfigList {
62+
discordConfig, ok := discordConfigInterface.(map[string]interface{})
63+
if !ok {
64+
return nil, errors.New("error: discord config is not valid")
65+
}
5166

52-
name, ok := discordConfig["name"].(string)
53-
if !ok {
54-
return nil, errors.New("error: cannot find discord name field in the config file")
55-
}
67+
name, ok := discordConfig["name"].(string)
68+
if !ok {
69+
return nil, errors.New("error: cannot find discord name field in the config file")
70+
}
5671

57-
threadURL, ok := discordConfig["url"].(string)
58-
if !ok {
59-
return nil, errors.New("error: cannot find discord url field in the config file")
60-
}
72+
threadURL, ok := discordConfig["url"].(string)
73+
if !ok {
74+
return nil, errors.New("error: cannot find discord url field in the config file")
75+
}
6176

62-
avatarURL, ok := discordConfig["avatarUrl"].(string)
63-
if !ok {
64-
return nil, errors.New("error: cannot find discord avatarUrl field in the config file")
65-
}
77+
avatarURL, ok := discordConfig["avatarUrl"].(string)
78+
if !ok {
79+
return nil, errors.New("error: cannot find discord avatarUrl field in the config file")
80+
}
6681

67-
mentionsInterface, ok := discordConfig["mentions"].([]interface{})
68-
if !ok {
69-
return nil, errors.New("error: cannot find discord mentions field in the config file")
70-
}
82+
mentionsInterface, ok := discordConfig["mentions"].([]interface{})
83+
if !ok {
84+
return nil, errors.New("error: cannot find discord mentions field in the config file")
85+
}
86+
87+
var mentions []string
88+
for _, mentionInterface := range mentionsInterface {
89+
mention, ok := mentionInterface.(string)
90+
if !ok {
91+
return nil, errors.New("error: mention field is not valid string")
92+
}
7193

72-
var mentions []string
73-
for _, mentionInterface := range mentionsInterface {
74-
mention, ok := mentionInterface.(string)
94+
mentions = append(mentions, mention)
95+
}
96+
97+
enabled, ok := discordConfig["enable"].(bool)
7598
if !ok {
76-
return nil, errors.New("error: mention field is not valid string")
99+
return nil, errors.New("error: cannot find discord enable field in the config file")
77100
}
78101

79-
mentions = append(mentions, mention)
80-
}
102+
headers := make(map[string]string)
103+
headers["Content-Type"] = "application/json"
81104

82-
enabled, ok := discordConfig["enable"].(bool)
83-
if !ok {
84-
return nil, errors.New("error: cannot find discord enable field in the config file")
85-
}
105+
conf := DiscordConfig{
106+
name: name,
107+
threadURL: threadURL,
108+
avatarURL: avatarURL,
109+
enabled: enabled,
110+
headers: headers,
111+
mentions: mentions,
112+
}
86113

87-
headers := make(map[string]string)
88-
headers["Content-Type"] = "application/json"
114+
discordConfigs = append(discordConfigs, conf)
115+
}
89116

90117
return &Discord{
91-
name: name,
92-
threadURL: threadURL,
93-
avatarURL: avatarURL,
94-
enabled: enabled,
95-
headers: headers,
96-
mentions: mentions,
118+
configs: discordConfigs,
119+
logger: logger,
120+
verbose: verbose,
97121
}, nil
98122
}
99123

@@ -104,44 +128,59 @@ func (d *Discord) Provider() string {
104128

105129
// Send will send notification
106130
func (d *Discord) Send(msg string) error {
107-
var messageBuilder strings.Builder
108-
109-
messageBuilder.WriteString("Hey ")
110-
for _, mention := range d.mentions {
111-
if strings.Contains(mention, "here") {
112-
messageBuilder.WriteString(fmt.Sprintf("%s", mention))
113-
} else {
114-
messageBuilder.WriteString(fmt.Sprintf("<%s>", mention))
131+
for _, conf := range d.configs {
132+
if conf.enabled {
133+
var messageBuilder strings.Builder
134+
135+
messageBuilder.WriteString("Hey ")
136+
for _, mention := range conf.mentions {
137+
if strings.Contains(mention, "here") {
138+
messageBuilder.WriteString(fmt.Sprintf("%s", mention))
139+
} else {
140+
messageBuilder.WriteString(fmt.Sprintf("<%s>", mention))
141+
}
142+
143+
messageBuilder.WriteString(", ")
144+
}
145+
146+
messageBuilder.WriteString(" ")
147+
messageBuilder.WriteString(msg)
148+
149+
content := messageBuilder.String()
150+
151+
discordMessage := DiscordMessage{
152+
Username: conf.name,
153+
AvatarURL: conf.avatarURL,
154+
Content: content,
155+
}
156+
157+
messageJSON, err := json.Marshal(discordMessage)
158+
if err != nil {
159+
return err
160+
}
161+
162+
go func(threadURL string, body io.Reader, headers map[string]string, timeout int) {
163+
_, err = httpx.HTTPPost(threadURL, body, headers, timeout)
164+
if err != nil {
165+
if d.verbose {
166+
d.logger.Println(err)
167+
}
168+
}
169+
}(conf.threadURL, bytes.NewBuffer(messageJSON), conf.headers, 5)
115170
}
116-
117-
messageBuilder.WriteString(", ")
118-
}
119-
120-
messageBuilder.WriteString(" ")
121-
messageBuilder.WriteString(msg)
122-
123-
msg = messageBuilder.String()
124-
125-
discordMessage := DiscordMessage{
126-
Username: d.name,
127-
AvatarURL: d.avatarURL,
128-
Content: msg,
129-
}
130-
131-
messageJSON, err := json.Marshal(discordMessage)
132-
if err != nil {
133-
return err
134-
}
135-
136-
_, err = httpx.HTTPPost(d.threadURL, bytes.NewBuffer(messageJSON), d.headers, 5)
137-
if err != nil {
138-
return err
139171
}
140172

141173
return nil
142174
}
143175

144176
// IsEnabled will return enable status
145177
func (d *Discord) IsEnabled() bool {
146-
return d.enabled
178+
if len(d.configs) > 0 {
179+
temp := d.configs[0].enabled
180+
for _, conf := range d.configs {
181+
temp = temp || conf.enabled
182+
}
183+
return temp
184+
}
185+
return false
147186
}

runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func (r *Runner) InitServices() error {
142142
service.Enable(serviceEnabled)
143143
service.SetConfig(conf)
144144

145+
// by default service is recovered
146+
service.SetRecover(true)
147+
145148
err = service.Connect()
146149
if err != nil {
147150
return err

0 commit comments

Comments
 (0)