Add option to connect using rtm.connect API
Florin Patan
6 years ago
0 | 0 | package slack |
1 | 1 | |
2 | 2 | import ( |
3 | "encoding/json" | |
3 | 4 | "fmt" |
4 | 5 | "net/url" |
6 | "time" | |
5 | 7 | ) |
6 | 8 | |
7 | 9 | // StartRTM calls the "rtm.start" endpoint and returns the provided URL and the full Info |
36 | 38 | // |
37 | 39 | // To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` |
38 | 40 | // on it. |
39 | func (api *Client) ConnectRTM() (info *Info, websocketURL string, slackResponse WebResponse, err error) { | |
41 | func (api *Client) ConnectRTM() (info *Info, websocketURL string, err error) { | |
40 | 42 | response := &infoResponseFull{} |
41 | 43 | err = post("rtm.connect", url.Values{"token": {api.config.token}}, response, api.debug) |
42 | 44 | if err != nil { |
43 | return nil, "", response.WebResponse, fmt.Errorf("post: %s", err) | |
45 | return nil, "", fmt.Errorf("post: %s", err) | |
44 | 46 | } |
45 | 47 | if !response.Ok { |
46 | return nil, "", response.WebResponse, response.Error | |
48 | return nil, "", response.Error | |
47 | 49 | } |
48 | 50 | |
49 | 51 | // websocket.Dial does not accept url without the port (yet) |
52 | 54 | api.Debugln("Using URL:", response.Info.URL) |
53 | 55 | websocketURL, err = websocketizeURLPort(response.Info.URL) |
54 | 56 | if err != nil { |
55 | return nil, "", response.WebResponse, fmt.Errorf("parsing response URL: %s", err) | |
57 | return nil, "", fmt.Errorf("parsing response URL: %s", err) | |
56 | 58 | } |
57 | 59 | |
58 | return &response.Info, websocketURL, response.WebResponse, nil | |
60 | return &response.Info, websocketURL, nil | |
59 | 61 | } |
60 | 62 | |
61 | 63 | // NewRTM returns a RTM, which provides a fully managed connection to |
62 | // Slack's websocket-based Real-Time Messaging protocol./ | |
64 | // Slack's websocket-based Real-Time Messaging protocol. | |
63 | 65 | func (api *Client) NewRTM() *RTM { |
64 | return newRTM(api) | |
66 | return api.NewRTMWithOptions(nil) | |
65 | 67 | } |
68 | ||
69 | // NewRTMWithOptions returns a RTM, which provides a fully managed connection to | |
70 | // Slack's websocket-based Real-Time Messaging protocol. | |
71 | // This also allows to configure various options available for RTM API. | |
72 | func (api *Client) NewRTMWithOptions(options *RTMOptions) *RTM { | |
73 | result := &RTM{ | |
74 | Client: *api, | |
75 | IncomingEvents: make(chan RTMEvent, 50), | |
76 | outgoingMessages: make(chan OutgoingMessage, 20), | |
77 | pings: make(map[int]time.Time), | |
78 | isConnected: false, | |
79 | wasIntentional: true, | |
80 | killChannel: make(chan bool), | |
81 | forcePing: make(chan bool), | |
82 | rawEvents: make(chan json.RawMessage), | |
83 | idGen: NewSafeID(1), | |
84 | } | |
85 | ||
86 | if options != nil { | |
87 | result.useRTMStart = options.UseRTMStart | |
88 | } else { | |
89 | result.useRTMStart = true | |
90 | } | |
91 | ||
92 | return result | |
93 | } |
16 | 16 | // RTM represents a managed websocket connection. It also supports |
17 | 17 | // all the methods of the `Client` type. |
18 | 18 | // |
19 | // Create this element with Client's NewRTM(). | |
19 | // Create this element with Client's NewRTM() or NewRTMWithOptions(*RTMOptions) | |
20 | 20 | type RTM struct { |
21 | 21 | idGen IDGenerator |
22 | 22 | pings map[int]time.Time |
37 | 37 | |
38 | 38 | // UserDetails upon connection |
39 | 39 | info *Info |
40 | ||
41 | // useRTMStart should be set to true if you want to use | |
42 | // rtm.start to connect to Slack, otherwise it will use | |
43 | // rtm.connect | |
44 | useRTMStart bool | |
40 | 45 | } |
41 | 46 | |
42 | // NewRTM returns a RTM, which provides a fully managed connection to | |
43 | // Slack's websocket-based Real-Time Messaging protocol. | |
44 | func newRTM(api *Client) *RTM { | |
45 | return &RTM{ | |
46 | Client: *api, | |
47 | IncomingEvents: make(chan RTMEvent, 50), | |
48 | outgoingMessages: make(chan OutgoingMessage, 20), | |
49 | pings: make(map[int]time.Time), | |
50 | isConnected: false, | |
51 | wasIntentional: true, | |
52 | killChannel: make(chan bool), | |
53 | forcePing: make(chan bool), | |
54 | rawEvents: make(chan json.RawMessage), | |
55 | idGen: NewSafeID(1), | |
56 | } | |
47 | // RTMOptions allows configuration of various options available for RTM messaging | |
48 | // | |
49 | // This structure will evolve in time so please make sure you are always using the | |
50 | // named keys for every entry available as per Go 1 compatibility promise adding fields | |
51 | // to this structure should not be considered a breaking change. | |
52 | type RTMOptions struct { | |
53 | // UseRTMStart set to true in order to use rtm.start or false to use rtm.connect | |
54 | // As of 11th July 2017 you should prefer setting this to false, see: | |
55 | // https://api.slack.com/changelog/2017-04-start-using-rtm-connect-and-stop-using-rtm-start | |
56 | UseRTMStart bool | |
57 | 57 | } |
58 | 58 | |
59 | 59 | // Disconnect and wait, blocking until a successful disconnection. |
28 | 28 | connectionCount++ |
29 | 29 | // start trying to connect |
30 | 30 | // the returned err is already passed onto the IncomingEvents channel |
31 | info, conn, err := rtm.connect(connectionCount) | |
31 | info, conn, err := rtm.connect(connectionCount, rtm.useRTMStart) | |
32 | 32 | // if err != nil then the connection is sucessful - otherwise it is |
33 | 33 | // fatal |
34 | 34 | if err != nil { |
63 | 63 | // connect attempts to connect to the slack websocket API. It handles any |
64 | 64 | // errors that occur while connecting and will return once a connection |
65 | 65 | // has been successfully opened. |
66 | func (rtm *RTM) connect(connectionCount int) (*Info, *websocket.Conn, error) { | |
66 | // If useRTMStart is false then it uses rtm.connect to create the connection, | |
67 | // otherwise it uses rtm.start. | |
68 | func (rtm *RTM) connect(connectionCount int, useRTMStart bool) (*Info, *websocket.Conn, error) { | |
67 | 69 | // used to provide exponential backoff wait time with jitter before trying |
68 | 70 | // to connect to slack again |
69 | 71 | boff := &backoff{ |
80 | 82 | ConnectionCount: connectionCount, |
81 | 83 | }} |
82 | 84 | // attempt to start the connection |
83 | info, conn, err := rtm.startRTMAndDial() | |
85 | info, conn, err := rtm.startRTMAndDial(useRTMStart) | |
84 | 86 | if err == nil { |
85 | 87 | return info, conn, nil |
86 | 88 | } |
104 | 106 | } |
105 | 107 | } |
106 | 108 | |
107 | // startRTMAndDial attemps to connect to the slack websocket. It returns the | |
108 | // full information returned by the "rtm.start" method on the slack API. | |
109 | func (rtm *RTM) startRTMAndDial() (*Info, *websocket.Conn, error) { | |
110 | info, url, err := rtm.StartRTM() | |
109 | // startRTMAndDial attempts to connect to the slack websocket. If useRTMStart is true, | |
110 | // then it returns the full information returned by the "rtm.start" method on the | |
111 | // slack API. Else it uses the "rtm.connect" method to connect | |
112 | func (rtm *RTM) startRTMAndDial(useRTMStart bool) (*Info, *websocket.Conn, error) { | |
113 | var info *Info | |
114 | var url string | |
115 | var err error | |
116 | ||
117 | if useRTMStart { | |
118 | info, url, err = rtm.StartRTM() | |
119 | } else { | |
120 | info, url, err = rtm.ConnectRTM() | |
121 | } | |
111 | 122 | if err != nil { |
112 | 123 | return nil, nil, err |
113 | 124 | } |