forked from stewart/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
48 lines (38 loc) · 808 Bytes
/
api.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
package slack
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
const endpoint = "https://slack.com/api/"
type apiResponse struct {
OK bool `json:"ok"`
URL string `json:"url"`
Error string `json:"error"`
}
func requestRTM(token string) (string, error) {
route, err := url.Parse(endpoint + "rtm.start")
if err != nil {
return "", err
}
params := url.Values{}
params.Add("token", token)
route.RawQuery = params.Encode()
response, err := http.Get(route.String())
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
return "", err
}
resp := apiResponse{}
json.Unmarshal(body, &resp)
if !resp.OK {
return "", errors.New("slack error: " + resp.Error)
}
return resp.URL, nil
}