-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslack.go
99 lines (80 loc) · 2.47 KB
/
slack.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type SlackPayload struct {
Type string `json:"type"`
Event SlackEvent `json:"event,omitempty"`
Challenge string `json:"challenge,omitempty"`
}
type SlackAttachment struct {
Id int32 `json:"id,omitempty"`
Color string `json:"color,omitempty"`
Fallback string `json:"fallback,omitempty"`
Text string `json:"text,omitempty"`
Footer string `json:"footer,omitempty"`
}
type SlackBlock struct {
Type string `json:"rich_text,omitempty"`
BlockID string `json:"block_id,omitempty"`
}
type SlackEvent struct {
Type string `json:"type"`
Subtype string `json:"subtype"`
Text string `json:"text"`
TS string `json:"ts"`
BotID string `json:"bot_id"`
Blocks []SlackBlock `json:"blocks,omitempty"`
Attachments []SlackAttachment `json:"attachments,omitempty"`
Channel string `json:"channel"`
EventTS string `json:"event_ts,omitempty"`
ChannelType string `json:"channel_type"`
}
type SlackUser struct {
Ok bool `json:"ok"`
Error string `json:"error"`
User struct {
Id string `json:"id"`
Profile struct {
Email string `json:"email"`
} `json:"profile"`
} `json:"user,omitempty"`
}
type SlackReponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
func fetchSlackUser(userEmail string) (*SlackUser, error) {
client := getClient()
var user SlackUser
endpoint := fmt.Sprintf("https://slack.com/api/users.lookupByEmail?email=%s", userEmail)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Bearer "+SLACK_BOT_OAUTH_TOKEN)
fmt.Printf("Bot is fetching Slack user profile: %s\n", endpoint)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("GET user Failed %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
client.CloseIdleConnections()
json.Unmarshal(body, &user)
if !user.Ok {
return nil, fmt.Errorf("Did not find a Slack user matching the email, exception %v", user.Error)
}
fmt.Printf("Slack userID found ''%v'' for user ''%v'' (OK result %v)\n", user.User.Id, user.User.Profile.Email, user.Ok)
return &user, nil
}