-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"fmt"
"github.com/int128/slack"
"log"
"os"
"time"
)
const (
EnvWebhookUrl = "SLACK_WEBHOOK"
EnvIcon = "SLACK_ICON"
EnvChannel = "SLACK_CHANNEL"
EnvUsername = "SLACK_USERNAME"
EnvMessage = "SLACK_MESSAGE"
EnvBody = "SLACK_ATTACHMENT"
)
func main() {
webhookUrl := os.Getenv(EnvWebhookUrl)
if webhookUrl == "" {
stdErr("Webhook url is required")
}
attachmentStruct := slack.Attachment{
Timestamp: time.Now().Unix(),
MrkdwnIn: []string{"text"},
}
body := []byte(os.Getenv(EnvBody))
err := json.Unmarshal(body, &attachmentStruct)
if err != nil {
log.Fatalf("Failed to parse attachment: %s", err)
}
var msg = &slack.Message{
Username: os.Getenv(EnvUsername),
IconEmoji: os.Getenv(EnvIcon),
Channel: os.Getenv(EnvChannel),
Text: os.Getenv(EnvMessage),
Attachments: []slack.Attachment{
attachmentStruct,
},
}
if err := slack.Send(webhookUrl, msg); err != nil {
stdErr(fmt.Sprintf("Could not send the message to Slack: %s", err))
}
}
func stdErr(message string) {
fmt.Fprintln(os.Stderr, message)
os.Exit(1)
}