-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack-bot.go
70 lines (58 loc) · 1.89 KB
/
slack-bot.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
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// SlackPayload represents the JSON payload structure for sending a message to Slack
type SlackPayload struct {
Channel string `json:"channel"`
Text string `json:"text"`
}
// sendSlackMessage sends a message to a Slack channel using the Slack API
func sendSlackMessage(message string) {
url := "https://slack.com/api/chat.postMessage"
slack_token := config.SlackOAuthToken
slack_channel := config.SlackChannelID
if slack_token == "" {
resultQueue <- TaskResult{application: "slack", status: false, reason: "SLACK_OAUTH_TOKEN is not set"}
return
}
if slack_channel == "" {
resultQueue <- TaskResult{application: "slack", status: false, reason: "SLACK_CHANNEL_ID is not set"}
return
}
payload := SlackPayload{
Channel: slack_channel,
Text: message,
}
payloadBytes, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
resultQueue <- TaskResult{application: "slack", status: false, reason: fmt.Sprintf("failed to create request: %v", err)}
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+slack_token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
resultQueue <- TaskResult{application: "slack", status: false, reason: fmt.Sprintf("failed to send request: %v", err)}
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
resultQueue <- TaskResult{application: "slack", status: false, reason: fmt.Sprintf("failed to read response body: %v", err)}
return
}
if resp.StatusCode != http.StatusOK {
resultQueue <- TaskResult{application: "slack", status: false, reason: fmt.Sprintf("received non-OK response: %s", body)}
return
} else {
resultQueue <- TaskResult{application: "slack", status: true, reason: ""}
return
}
}