forked from stewart/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
165 lines (132 loc) · 3.31 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package slack
import (
"net/http"
"github.com/gorilla/websocket"
"github.com/stewart/slack/events"
"net/url"
"io/ioutil"
"encoding/json"
"errors"
)
// A Client maintains a WebSocket connection to the Slack RTM API, spitting out
// events on the Incoming channel as they come in.
type Client struct {
Token string
Connected bool
Incoming chan interface{}
Errors chan error
conn *websocket.Conn
// this gets incremented anytime we send a message
messageID int
}
// Creates a new Client instance with the provided authentication token.
func NewClient(token string) *Client {
return &Client{
Token: token,
Incoming: make(chan interface{}),
Errors: make(chan error, 1),
messageID: 1,
}
}
// Connects to Slack's RTM WebSocket API by requesting a connection URL via the
// `rtm.start` API method.
//
// Once complete, the Client is considered "connect" to Slack.
func (client *Client) Connect() error {
url, err := requestRTM(client.Token)
if err != nil {
return err
}
dialer := websocket.Dialer{}
headers := http.Header{}
conn, _, err := dialer.Dial(url, headers)
if err != nil {
return err
}
client.conn = conn
client.Connected = true
return nil
}
// Wrapper around a goroutine that listens for incoming messages, parsing them
// into their correct event type, then tosses them (as an `interface{}`) into
// the client.Incoming channel.
func (client *Client) Loop() {
conn := client.conn
go func() {
for {
_, msg, err := conn.ReadMessage()
if err != nil {
client.Errors <- err
return
}
message, err := events.Parse(msg)
if err != nil {
client.Errors <- err
return
}
client.Incoming <- message
}
}()
}
// Sends a message to the provided channel, with the provided text.
func (client *Client) SendMessage(channel, text string) error {
msg := struct {
ID int `json:"id"`
Type string `json:"type"`
Channel string `json:"channel"`
Text string `json:"text"`
}{client.messageID, "message", channel, text}
if err := client.conn.WriteJSON(msg); err != nil {
return err
}
client.messageID++
return nil
}
func (client *Client) SendDirectMessage(user_id string, text string) error {
dm_id, _ := client.OpenDM(user_id)
msg := struct {
ID int `json:"id"`
Type string `json:"type"`
Channel string `json:"channel"`
Text string `json:"text"`
}{client.messageID, "message", dm_id, text}
if err := client.conn.WriteJSON(msg); err != nil {
return err
}
client.messageID++
return nil
}
func (client *Client) OpenDM (user_id string) (string, error) {
route, err := url.Parse(endpoint + "im.open")
if err != nil {
return "", err
}
params := url.Values{}
params.Add("token", client.Token)
params.Add("user", user_id)
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 := openDMResponse{}
json.Unmarshal(body, &resp)
if !resp.OK {
return "", errors.New("slack error: " + resp.Error)
}
return resp.Channel.ID, nil
}
type openDMResponse struct {
OK bool `json:"ok"`
NoOp bool `json:"no_op"`
AlreadyOpen bool `json:"already_open"`
Channel struct {
ID string `json:"id"`
} `json:"channel"`
Error string `json:"error"`
}