forked from thatoddmailbox/slack-status-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_slack.go
111 lines (92 loc) · 2.42 KB
/
api_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
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const API_Slack_Domain = "https://slack.com/api/"
var API_Slack_AuthToken string
var API_Slack_Connected bool
var API_Slack_ClientID string
var API_Slack_ClientSecret string
var API_Slack_Me map[string]interface{}
type API_Slack_StatusInfo struct {
StatusText string `json:"status_text"`
StatusEmoji string `json:"status_emoji"`
}
func API_Slack_Request(requestType string, path string, params url.Values) (map[string]interface{}, error) {
if path != "oauth.access" {
params.Add("token", API_Slack_AuthToken)
}
url := API_Slack_Domain + path
if (requestType == "GET") {
url = url + "?" + params.Encode()
}
var requestBody io.Reader
if (requestType == "POST") {
requestBody = strings.NewReader(params.Encode())
}
req, err := http.NewRequest(requestType, url, requestBody)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
strResponse, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
decodedResponse := map[string]interface{}{}
err = json.Unmarshal(strResponse, &decodedResponse)
if err != nil {
return nil, err
}
return decodedResponse, nil
}
func API_Slack_SignedIn() (bool) {
response, err := API_Slack_Request("GET", "auth.test", url.Values{})
if err != nil || response["ok"] == false {
return false
}
API_Slack_Me = response
return true
}
func API_Slack_SignOut() {
}
func API_Slack_GetRedirectPath() (string) {
return "https://slack.com/oauth/authorize?client_id=" + API_Slack_ClientID + "&scope=users.profile:read%20users.profile:write"
}
func API_Slack_GetTokenFromCode(code string) (string, error) {
resp, err := API_Slack_Request("GET", "oauth.access", url.Values{
"client_id": { API_Slack_ClientID },
"client_secret": { API_Slack_ClientSecret },
"code": { code },
})
if err != nil {
return "", err
}
return resp["access_token"].(string), nil
}
func API_Slack_UpdateStatus(info API_Slack_StatusInfo) (error) {
statusText, err := json.Marshal(info)
if err != nil {
return err
}
_, err = API_Slack_Request("GET", "users.profile.set", url.Values{
"profile": { string(statusText) },
})
return err
}
func API_Slack_Init() {
API_Slack_AuthToken = Storage_Get("slack-auth")
if API_Slack_AuthToken == "" {
API_Slack_AuthToken = "invalid"
}
API_Slack_Connected = API_Slack_SignedIn()
}