-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotifier.go
67 lines (54 loc) · 1.38 KB
/
notifier.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type BotMessage struct {
UserID string `json:"userId"`
Message string `json:"text"`
Attachments string `json:"attachments"`
}
type RequestPermalink struct {
Permalink string `json:"permalink"`
}
func notify(message *BotMessage) error {
return pushNotification(message)
}
func pushNotification(event *BotMessage) error {
client := getClient()
payload := url.Values{}
payload.Set("token", SLACK_BOT_OAUTH_TOKEN)
payload.Set("channel", event.UserID)
payload.Set("text", event.Message)
payload.Set("attachments", event.Attachments)
endpoint := "https://slack.com/api/chat.postMessage"
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(payload.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if resp.StatusCode != 200 {
return fmt.Errorf("POST notification Failed %v", resp.StatusCode)
}
if err != nil {
return err
}
defer resp.Body.Close()
var response SlackReponse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
client.CloseIdleConnections()
json.Unmarshal(body, &response)
fmt.Printf("Bot is notifying Slack user id: %v\n", string(event.UserID))
if response.Ok == false {
return fmt.Errorf(response.Error)
}
return nil
}