Skip to content

Commit 927fd65

Browse files
committed
Merge remote-tracking branch 'upstream/master' into feat-downtime
2 parents 3fa38f3 + 3410b3f commit 927fd65

File tree

7 files changed

+310
-4
lines changed

7 files changed

+310
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ format:
2525
find . -name "*.go" -not -path "*/vendor/*" -not -path "*/.git/*" -not -path "*/volumes/*" | xargs gofmt -s -d -w
2626

2727
clean:
28-
rm tob tob.exe *.txt
28+
rm -f tob tob.exe *.txt

cmd/tob/main.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"runtime"
89
"syscall"
910

1011
"github.com/telkomdev/tob"
1112
"github.com/telkomdev/tob/config"
1213
"github.com/telkomdev/tob/notificators/discord"
1314
"github.com/telkomdev/tob/notificators/email"
15+
"github.com/telkomdev/tob/notificators/slack"
16+
"github.com/telkomdev/tob/notificators/telegram"
1417
)
1518

1619
func main() {
@@ -22,7 +25,7 @@ func main() {
2225
}
2326

2427
if args.ShowVersion {
25-
fmt.Printf("%s version %s\n", os.Args[0], tob.Version)
28+
fmt.Printf("%s version %s (runtime: %s)\n", os.Args[0], tob.Version, runtime.Version())
2629
os.Exit(0)
2730
}
2831

@@ -61,7 +64,26 @@ func main() {
6164
os.Exit(1)
6265
}
6366

64-
notificators := []tob.Notificator{emailNotificator, discordNotificator}
67+
// slack notificator
68+
slackNotificator, err := slack.NewSlack(configs)
69+
if err != nil {
70+
fmt.Println("error: ", err)
71+
os.Exit(1)
72+
}
73+
74+
// telegram notificator
75+
telegramNotificator, err := telegram.NewTelegram(configs)
76+
if err != nil {
77+
fmt.Println("error: ", err)
78+
os.Exit(1)
79+
}
80+
81+
notificators := []tob.Notificator{
82+
emailNotificator,
83+
discordNotificator,
84+
slackNotificator,
85+
telegramNotificator,
86+
}
6587

6688
ctx, cancel := context.WithCancel(context.Background())
6789
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
@@ -96,6 +118,6 @@ func waitNotify(kill chan os.Signal, runner *tob.Runner) {
96118
select {
97119
case <-kill:
98120
runner.Stop() <- true
99-
fmt.Println("kill process")
121+
fmt.Println("kill tob")
100122
}
101123
}

config_example.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
"enable": true
4343
},
4444

45+
"elasticsearch_cluster_one": {
46+
"kind": "web",
47+
"url": "http://elastic:devpass@localhost:9200/_cluster/health?pretty",
48+
"checkInterval": 10,
49+
"enable": true
50+
},
51+
4552
"dummy_one": {
4653
"kind": "dummy",
4754
"url": "https://minio-storage.com/minio/health/ready",
@@ -55,9 +62,23 @@
5562
"name": "BoyBot",
5663
"url": "https://discord.com/api/webhooks/YOUR_API_ID/YOUR_API_ID",
5764
"avatarUrl": "https://i.imgur.com/KHzN7QR.png",
65+
"mentions": [
66+
"@8XXXXXXXXXXXXXXXXX",
67+
"@here"
68+
],
5869
"enable": true
5970
},
6071

72+
"slack": {
73+
"name": "BoyBot",
74+
"webhookUrl": "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXX",
75+
"mentions": [
76+
"@UXXXXXXXX",
77+
"!here"
78+
],
79+
"enable": false
80+
},
81+
6182
"email": {
6283
"authEmail": "[email protected]",
6384
"authPassword": "xxxx",
@@ -66,6 +87,12 @@
6687
"from": "[email protected]",
6788
6889
"subject": "Tob Notification",
90+
"enable": false
91+
},
92+
93+
"telegram": {
94+
"botToken": "XXXXXXXXXX:XXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXXX",
95+
"groupId": "-111111111",
6996
"enable": true
7097
}
7198
},

notificators/discord/discord.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"github.com/telkomdev/tob/config"
89
"github.com/telkomdev/tob/httpx"
10+
"strings"
911
)
1012

1113
// DiscordMessage represent discord request
@@ -27,6 +29,7 @@ type Discord struct {
2729
name string
2830
avatarURL string
2931
headers map[string]string
32+
mentions []string
3033
enabled bool
3134
}
3235

@@ -61,6 +64,21 @@ func NewDiscord(configs config.Config) (*Discord, error) {
6164
return nil, errors.New("error: cannot find discord avatarUrl field in the config file")
6265
}
6366

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+
}
71+
72+
var mentions []string
73+
for _, mentionInterface := range mentionsInterface {
74+
mention, ok := mentionInterface.(string)
75+
if !ok {
76+
return nil, errors.New("error: mention field is not valid string")
77+
}
78+
79+
mentions = append(mentions, mention)
80+
}
81+
6482
enabled, ok := discordConfig["enable"].(bool)
6583
if !ok {
6684
return nil, errors.New("error: cannot find discord enable field in the config file")
@@ -75,6 +93,7 @@ func NewDiscord(configs config.Config) (*Discord, error) {
7593
avatarURL: avatarURL,
7694
enabled: enabled,
7795
headers: headers,
96+
mentions: mentions,
7897
}, nil
7998
}
8099

@@ -85,6 +104,24 @@ func (d *Discord) Provider() string {
85104

86105
// Send will send notification
87106
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))
115+
}
116+
117+
messageBuilder.WriteString(", ")
118+
}
119+
120+
messageBuilder.WriteString(" ")
121+
messageBuilder.WriteString(msg)
122+
123+
msg = messageBuilder.String()
124+
88125
discordMessage := DiscordMessage{
89126
Username: d.name,
90127
AvatarURL: d.avatarURL,

notificators/slack/slack.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package slack
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"github.com/telkomdev/tob/config"
9+
"github.com/telkomdev/tob/httpx"
10+
"strings"
11+
)
12+
13+
// https://api.slack.com/messaging/sending
14+
15+
// SlackMessage represent Slack request
16+
type SlackMessage struct {
17+
Text string `json:"text"`
18+
}
19+
20+
// SlackResponse represent Slack response
21+
type SlackResponse struct {
22+
Code uint `json:"code"`
23+
Message string `json:"message"`
24+
}
25+
26+
// Slack represent Slack notificator
27+
type Slack struct {
28+
webhookURL string
29+
headers map[string]string
30+
mentions []string
31+
enabled bool
32+
}
33+
34+
// NewSlack Slack's constructor
35+
func NewSlack(configs config.Config) (*Slack, error) {
36+
notificatorConfigInterface, ok := configs["notificator"]
37+
if !ok {
38+
return nil, errors.New("error: cannot find notificator field in the config file")
39+
}
40+
41+
notificatorConfig := notificatorConfigInterface.(map[string]interface{})
42+
43+
slackConfigInterface, ok := notificatorConfig["slack"]
44+
if !ok {
45+
return nil, errors.New("error: cannot find Slack field in the config file")
46+
}
47+
48+
slackConfig := slackConfigInterface.(map[string]interface{})
49+
50+
webhookURL, ok := slackConfig["webhookUrl"].(string)
51+
if !ok {
52+
return nil, errors.New("error: cannot find Slack webhookUrl field in the config file")
53+
}
54+
55+
mentionsInterface, ok := slackConfig["mentions"].([]interface{})
56+
if !ok {
57+
return nil, errors.New("error: cannot find Slack mentions field in the config file")
58+
}
59+
60+
var mentions []string
61+
for _, mentionInterface := range mentionsInterface {
62+
mention, ok := mentionInterface.(string)
63+
if !ok {
64+
return nil, errors.New("error: mention field is not valid string")
65+
}
66+
67+
mentions = append(mentions, mention)
68+
}
69+
70+
enabled, ok := slackConfig["enable"].(bool)
71+
if !ok {
72+
return nil, errors.New("error: cannot find Slack enable field in the config file")
73+
}
74+
75+
headers := make(map[string]string)
76+
headers["Content-Type"] = "application/json"
77+
78+
return &Slack{
79+
webhookURL: webhookURL,
80+
enabled: enabled,
81+
headers: headers,
82+
mentions: mentions,
83+
}, nil
84+
}
85+
86+
// Provider will return Notificator provider
87+
func (d *Slack) Provider() string {
88+
return "slack"
89+
}
90+
91+
// Send will send notification
92+
func (d *Slack) Send(msg string) error {
93+
var messageBuilder strings.Builder
94+
95+
messageBuilder.WriteString("Hey ")
96+
for _, mention := range d.mentions {
97+
messageBuilder.WriteString(fmt.Sprintf("<%s>", mention))
98+
messageBuilder.WriteString(", ")
99+
}
100+
101+
messageBuilder.WriteString(" ")
102+
messageBuilder.WriteString(msg)
103+
104+
msg = messageBuilder.String()
105+
106+
slackMessage := SlackMessage{
107+
Text: msg,
108+
}
109+
110+
messageJSON, err := json.Marshal(slackMessage)
111+
if err != nil {
112+
return err
113+
}
114+
115+
_, err = httpx.HTTPPost(d.webhookURL, bytes.NewBuffer(messageJSON), d.headers, 5)
116+
if err != nil {
117+
return err
118+
}
119+
120+
return nil
121+
}
122+
123+
// IsEnabled will return enable status
124+
func (d *Slack) IsEnabled() bool {
125+
return d.enabled
126+
}

0 commit comments

Comments
 (0)