-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
144 lines (117 loc) · 3.06 KB
/
client.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
package slack
import (
"net/http"
"github.com/gorilla/websocket"
"github.com/stewart/slack/events"
"github.com/stewart/slack/types"
)
// 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 New(token string) *Client {
return &Client{
Token: token,
Incoming: make(chan interface{}),
Errors: make(chan error, 1),
messageID: 1,
}
}
// alias for New
func NewClient(token string) *Client {
return New(token)
}
// 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 := startRtm(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) Listen() {
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
}
}()
}
// alias for Listen
func (client *Client) Loop() {
client.Listen()
}
// Attempts to open an IM with the specified User.
// If successful, will return a DM ID to use when sending messages to that user.
func (client *Client) OpenIM(user string) (string, error) {
return openIm(client.Token, user)
}
// 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
}
// Gets a list of team members.
func (client *Client) ListUsers() ([]types.User, error) {
return listUsers(client.Token)
}
// Gets a list of team channels.
func (client *Client) ListChannels() ([]types.Channel, error) {
return listChannels(client.Token)
}
// Gets a list of IMs the user is connected to.
func (client *Client) ListIms() ([]types.IM, error) {
return listIms(client.Token)
}
func (client *Client) Ping() error {
msg := struct {
ID int `json:"id"`
Type string `json:"type"`
}{client.messageID, "ping"}
if err := client.conn.WriteJSON(msg); err != nil {
return err
}
client.messageID++
return nil
}