-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackapi.go
82 lines (68 loc) · 1.66 KB
/
slackapi.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
package main
import (
"net/url"
"net/http"
"io/ioutil"
"log"
"encoding/json"
)
type OAuth struct {
OK bool `json:"ok"`
Url string `json:"url"`
Team string `json:"team"`
User string `json:"user"`
TeamId string `json:"team_id"`
UserId string `json:"user_id"`
}
type API struct {
Token string
Notification Notification
Channels []Channel
}
type Notification struct {
ChannelName string
Time string
}
const baseSlackURL string = "https://slack.com/api/"
func (api *API) getSlackAPI(values url.Values, endpoint string) (b []byte, err error) {
res, err := http.Get(baseSlackURL + endpoint + "?" + values.Encode())
if err != nil {
return nil, err
} else {
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
}
func (api *API) UpdateChannels() {
values := url.Values{
"token": {api.Token},
"pretty": {"1"},
}
b, _ := api.getSlackAPI(values, "channels.list")
data := new(ChannelsList)
if err := json.Unmarshal(b, data); err != nil {
log.Println("JSON Unmarshal error", err)
return
}
api.Channels = data.Channels
}
func (api *API) GetChannelMessages(channel Channel, ts string) []Message {
values := url.Values{}
values.Add("token", api.Token)
values.Add("channel", channel.Id)
values.Add("count", "1000")
if ts != "" {
values.Add("oldest", ts)
}
values.Add("pretty", "1")
b, _ := api.getSlackAPI(values, "channels.history")
data := new(ChannelHistory)
if err := json.Unmarshal(b, data); err != nil {
log.Println("JSON Unmarshal error", err)
return nil
}
return data.Messages
}
func (api *API) PostSlackAPI(values url.Values, endpoint string) {
http.PostForm(baseSlackURL+endpoint+"?", values)
}