diff --git a/README.md b/README.md index 9834c3b..ac8c661 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ npm install slackbots - `postMessageToGroup(name, message [, params, callback])` (return: promise) - posts a message to private group by name, - `postMessageToChannel(name, message [, params, callback])` (return: promise) - posts a message to channel by name. - `openIm(userId)` (return: promise) - opens a direct message channel with another member in the team +- `reconnect()` - reconnects the slackbot to the slack channel after websocket connection times out ## Usage ```js @@ -120,4 +121,3 @@ bot.postMessageToUser('user', 'hi').always(function(data) { // ... }) ``` - diff --git a/index.js b/index.js index 5c921b4..2bdcf4d 100644 --- a/index.js +++ b/index.js @@ -72,6 +72,18 @@ class Bot extends EventEmitter { }.bind(this)); } + /** + * Reconnect to Real Time Messaging api after connection is dropped + */ + reconnect() { + this._api('rtm.connect').then((data) => { + this.wsUrl = data.url; + this.connect(); + }).fail((data) => { + this.emit('error', new Error(data.error ? data.error : data)); + }).done(); + } + /** * Get channels * @returns {vow.Promise}