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
106130func (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
145177func (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}
0 commit comments