5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-27 02:01:35 +00:00
matterbridge/vendor/github.com/nlopes/slack/rtm.go

89 lines
3.1 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
2017-07-16 12:29:46 +00:00
"context"
"encoding/json"
2016-09-05 14:34:37 +00:00
"fmt"
"net/url"
2017-07-16 12:29:46 +00:00
"time"
2016-09-05 14:34:37 +00:00
)
2017-07-16 12:29:46 +00:00
// StartRTM calls the "rtm.start" endpoint and returns the provided URL and the full Info block.
2016-09-05 14:34:37 +00:00
//
2017-07-16 12:29:46 +00:00
// To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it.
2016-09-05 14:34:37 +00:00
func (api *Client) StartRTM() (info *Info, websocketURL string, err error) {
2017-07-16 12:29:46 +00:00
return api.StartRTMContext(context.Background())
}
// StartRTMContext calls the "rtm.start" endpoint and returns the provided URL and the full Info block with a custom context.
//
// To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it.
func (api *Client) StartRTMContext(ctx context.Context) (info *Info, websocketURL string, err error) {
2016-09-05 14:34:37 +00:00
response := &infoResponseFull{}
2017-07-16 12:29:46 +00:00
err = post(ctx, "rtm.start", url.Values{"token": {api.config.token}}, response, api.debug)
if err != nil {
return nil, "", fmt.Errorf("post: %s", err)
}
if !response.Ok {
return nil, "", response.Error
}
api.Debugln("Using URL:", response.Info.URL)
2018-01-08 21:41:38 +00:00
return &response.Info, response.Info.URL, nil
2017-07-16 12:29:46 +00:00
}
// ConnectRTM calls the "rtm.connect" endpoint and returns the provided URL and the compact Info block.
//
// To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it.
func (api *Client) ConnectRTM() (info *Info, websocketURL string, err error) {
return api.ConnectRTMContext(context.Background())
}
// ConnectRTM calls the "rtm.connect" endpoint and returns the provided URL and the compact Info block with a custom context.
//
// To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it.
func (api *Client) ConnectRTMContext(ctx context.Context) (info *Info, websocketURL string, err error) {
response := &infoResponseFull{}
err = post(ctx, "rtm.connect", url.Values{"token": {api.config.token}}, response, api.debug)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, "", fmt.Errorf("post: %s", err)
}
if !response.Ok {
return nil, "", response.Error
}
api.Debugln("Using URL:", response.Info.URL)
2018-01-08 21:41:38 +00:00
return &response.Info, response.Info.URL, nil
2016-09-05 14:34:37 +00:00
}
// NewRTM returns a RTM, which provides a fully managed connection to
2017-07-16 12:29:46 +00:00
// Slack's websocket-based Real-Time Messaging protocol.
2016-09-05 14:34:37 +00:00
func (api *Client) NewRTM() *RTM {
2017-07-16 12:29:46 +00:00
return api.NewRTMWithOptions(nil)
}
// NewRTMWithOptions returns a RTM, which provides a fully managed connection to
// Slack's websocket-based Real-Time Messaging protocol.
// This also allows to configure various options available for RTM API.
func (api *Client) NewRTMWithOptions(options *RTMOptions) *RTM {
result := &RTM{
Client: *api,
IncomingEvents: make(chan RTMEvent, 50),
outgoingMessages: make(chan OutgoingMessage, 20),
pings: make(map[int]time.Time),
isConnected: false,
wasIntentional: true,
killChannel: make(chan bool),
2018-01-08 21:41:38 +00:00
disconnected: make(chan struct{}),
2017-07-16 12:29:46 +00:00
forcePing: make(chan bool),
rawEvents: make(chan json.RawMessage),
idGen: NewSafeID(1),
}
if options != nil {
result.useRTMStart = options.UseRTMStart
} else {
result.useRTMStart = true
}
return result
2016-09-05 14:34:37 +00:00
}