-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
50 lines (38 loc) · 909 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
49
50
package slack
import (
"encoding/json"
"errors"
"github.com/stewart/slack/types"
)
type apiResponse struct {
OK bool `json:"ok"`
URL string `json:"url"`
Error string `json:"error"`
// im.open response
Channel struct {
ID string `json:"id"`
} `json:"channel"`
// collections
Members []types.User `json:"members"`
Channels []types.Channel `json:"channels"`
IMs []types.IM `json:"ims"`
}
func call(action, token string, params map[string]string) (*apiResponse, error) {
resp := &apiResponse{}
body, err := get(action, token, params)
if err != nil {
return resp, err
}
json.Unmarshal(body, &resp)
if !resp.OK {
return resp, errors.New("slack error: " + resp.Error)
}
return resp, nil
}
func startRtm(token string) (string, error) {
resp, err := call("rtm.start", token, map[string]string{})
if err != nil {
return "", err
}
return resp.URL, nil
}