diff options
author | Wim <wim@42.be> | 2018-01-08 22:41:38 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-01-09 00:07:55 +0100 |
commit | be898b44c3c057c7b70efb83f66b7bbbdd5e9276 (patch) | |
tree | 01c8983913bcae177b5e0e015de9a45abe8369a7 /vendor/github.com/nlopes/slack/rtm.go | |
parent | 4828c434439949f62a6e219ab9947c221a06b111 (diff) | |
download | matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.gz matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.tar.bz2 matterbridge-msglm-be898b44c3c057c7b70efb83f66b7bbbdd5e9276.zip |
Update vendor (slack)
Diffstat (limited to 'vendor/github.com/nlopes/slack/rtm.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/rtm.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/vendor/github.com/nlopes/slack/rtm.go b/vendor/github.com/nlopes/slack/rtm.go new file mode 100644 index 00000000..bb3cde1b --- /dev/null +++ b/vendor/github.com/nlopes/slack/rtm.go @@ -0,0 +1,88 @@ +package slack + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "time" +) + +// StartRTM calls the "rtm.start" endpoint and returns the provided URL and the full Info block. +// +// To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it. +func (api *Client) StartRTM() (info *Info, websocketURL string, err error) { + 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) { + response := &infoResponseFull{} + 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) + return &response.Info, response.Info.URL, nil +} + +// 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) + if err != nil { + return nil, "", fmt.Errorf("post: %s", err) + } + if !response.Ok { + return nil, "", response.Error + } + api.Debugln("Using URL:", response.Info.URL) + return &response.Info, response.Info.URL, nil +} + +// NewRTM returns a RTM, which provides a fully managed connection to +// Slack's websocket-based Real-Time Messaging protocol. +func (api *Client) NewRTM() *RTM { + 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), + disconnected: make(chan struct{}), + 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 +} |