| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gorilla/websocket" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | websocketDefaultTimeout = 10 * time.Second |
| 14 | defaultPingInterval = 30 * time.Second |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | rtmEventTypeAck = "" |
| 19 | rtmEventTypeHello = "hello" |
| 20 | rtmEventTypeGoodbye = "goodbye" |
| 21 | rtmEventTypePong = "pong" |
| 22 | rtmEventTypeDesktopNotification = "desktop_notification" |
| 23 | ) |
| 24 | |
| 25 | // StartRTM calls the "rtm.start" endpoint and returns the provided URL and the full Info block. |
| 26 | // |
| 27 | // To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it. |
| 28 | func (api *Client) StartRTM() (info *Info, websocketURL string, err error) { |
| 29 | ctx, cancel := context.WithTimeout(context.Background(), websocketDefaultTimeout) |
| 30 | defer cancel() |
| 31 | |
| 32 | return api.StartRTMContext(ctx) |
| 33 | } |
| 34 | |
| 35 | // StartRTMContext calls the "rtm.start" endpoint and returns the provided URL and the full Info block with a custom context. |
| 36 | // |
| 37 | // To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it. |
| 38 | func (api *Client) StartRTMContext(ctx context.Context) (info *Info, websocketURL string, err error) { |
| 39 | response := &infoResponseFull{} |
| 40 | err = api.postMethod(ctx, "rtm.start", url.Values{"token": {api.token}}, response) |
| 41 | if err != nil { |
| 42 | return nil, "", err |
| 43 | } |
| 44 | |
| 45 | api.Debugln("Using URL:", response.Info.URL) |
| 46 | return &response.Info, response.Info.URL, response.Err() |
| 47 | } |
| 48 | |
| 49 | // ConnectRTM calls the "rtm.connect" endpoint and returns the provided URL and the compact Info block. |
| 50 | // |
| 51 | // To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it. |
| 52 | func (api *Client) ConnectRTM() (info *Info, websocketURL string, err error) { |
| 53 | ctx, cancel := context.WithTimeout(context.Background(), websocketDefaultTimeout) |
| 54 | defer cancel() |
| 55 | |
| 56 | return api.ConnectRTMContext(ctx) |
| 57 | } |
| 58 | |
| 59 | // ConnectRTMContext calls the "rtm.connect" endpoint and returns the |
| 60 | // provided URL and the compact Info block with a custom context. |
| 61 | // |
| 62 | // To have a fully managed Websocket connection, use `NewRTM`, and call `ManageConnection()` on it. |
| 63 | func (api *Client) ConnectRTMContext(ctx context.Context) (info *Info, websocketURL string, err error) { |
| 64 | response := &infoResponseFull{} |
| 65 | err = api.postMethod(ctx, "rtm.connect", url.Values{"token": {api.token}}, response) |
| 66 | if err != nil { |
| 67 | api.Debugf("Failed to connect to RTM: %s", err) |
| 68 | return nil, "", err |
| 69 | } |
| 70 | |
| 71 | api.Debugln("Using URL:", response.Info.URL) |
| 72 | return &response.Info, response.Info.URL, response.Err() |
| 73 | } |
| 74 | |
| 75 | // RTMOption options for the managed RTM. |
| 76 | type RTMOption func(*RTM) |
| 77 | |
| 78 | // RTMOptionUseStart as of 11th July 2017 you should prefer setting this to false, see: |
| 79 | // https://api.slack.com/changelog/2017-04-start-using-rtm-connect-and-stop-using-rtm-start |
| 80 | func RTMOptionUseStart(b bool) RTMOption { |
| 81 | return func(rtm *RTM) { |
| 82 | rtm.useRTMStart = b |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // RTMOptionDialer takes a gorilla websocket Dialer and uses it as the |
| 87 | // Dialer when opening the websocket for the RTM connection. |
| 88 | func RTMOptionDialer(d *websocket.Dialer) RTMOption { |
| 89 | return func(rtm *RTM) { |
| 90 | rtm.dialer = d |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // RTMOptionPingInterval determines how often to deliver a ping message to slack. |
| 95 | func RTMOptionPingInterval(d time.Duration) RTMOption { |
| 96 | return func(rtm *RTM) { |
| 97 | rtm.pingInterval = d |
| 98 | rtm.resetDeadman() |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // RTMOptionConnParams installs parameters to embed into the connection URL. |
| 103 | func RTMOptionConnParams(connParams url.Values) RTMOption { |
| 104 | return func(rtm *RTM) { |
| 105 | rtm.connParams = connParams |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // NewRTM returns a RTM, which provides a fully managed connection to |
| 110 | // Slack's websocket-based Real-Time Messaging protocol. |
| 111 | func (api *Client) NewRTM(options ...RTMOption) *RTM { |
| 112 | result := &RTM{ |
| 113 | Client: *api, |
| 114 | IncomingEvents: make(chan RTMEvent, 50), |
| 115 | outgoingMessages: make(chan OutgoingMessage, 20), |
| 116 | pingInterval: defaultPingInterval, |
| 117 | pingDeadman: time.NewTimer(deadmanDuration(defaultPingInterval)), |
| 118 | killChannel: make(chan bool), |
| 119 | disconnected: make(chan struct{}), |
| 120 | disconnectedm: &sync.Once{}, |
| 121 | forcePing: make(chan bool), |
| 122 | idGen: NewSafeID(1), |
| 123 | mu: &sync.Mutex{}, |
| 124 | } |
| 125 | |
| 126 | for _, opt := range options { |
| 127 | opt(result) |
| 128 | } |
| 129 | |
| 130 | return result |
| 131 | } |