From 0a7117c88f9dd17f9601e33251d25d14a6108190 Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Mon, 15 Oct 2018 15:17:07 -0600 Subject: [PATCH 1/6] include slack ws event This adds the event sent by slack when the server is going to disconnect the websocket. Add ability to handle the event. --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index f3e1cf8..eaea438 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,10 @@ class Bot extends EventEmitter { console.log(e); } }.bind(this)); + + this.ws.on('goodbye', function(data) { + this.emit('goodbye', data) + }) } /** From f6363ac1aba93e3f6e9ca5a5ff8d6c2a1a1eb5a1 Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Mon, 15 Oct 2018 15:23:23 -0600 Subject: [PATCH 2/6] added semi-colons Stuck in ES6 mind-set.. oops --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index eaea438..c5d50fa 100644 --- a/index.js +++ b/index.js @@ -69,8 +69,8 @@ class Bot extends EventEmitter { }.bind(this)); this.ws.on('goodbye', function(data) { - this.emit('goodbye', data) - }) + this.emit('goodbye', data); + }); } /** From 83723b9a4251f612117d7055baee62ced0a833cd Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Mon, 15 Oct 2018 15:26:23 -0600 Subject: [PATCH 3/6] removed illegal trailing white-space (using github UI editor) --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c5d50fa..26c02bf 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ class Bot extends EventEmitter { console.log(e); } }.bind(this)); - + this.ws.on('goodbye', function(data) { this.emit('goodbye', data); }); From a473beacb828eb123f7eb55fd422c3cee9012b2d Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Thu, 25 Oct 2018 15:14:07 -0600 Subject: [PATCH 4/6] changing PR to edit README figured a workaround that would require minimal change to the repo and has gone through rigorous PR review by my company. (confirmation that it works happened today) logs dont lie --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index 26c02bf..f3e1cf8 100644 --- a/index.js +++ b/index.js @@ -67,10 +67,6 @@ class Bot extends EventEmitter { console.log(e); } }.bind(this)); - - this.ws.on('goodbye', function(data) { - this.emit('goodbye', data); - }); } /** From 21d3a08eba32da98c5d2f51737bd4e4a382da32e Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Thu, 25 Oct 2018 15:24:13 -0600 Subject: [PATCH 5/6] add silent disconnect handling to readme this will assist *many* people dealing with the frustration of silent disconnects. Server will still run but the bot will show as offline. --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 7cfa697..6301bbb 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,44 @@ bot.on('message', function(data) { console.log(data); }); ``` +## Usage for catching 'silent disconnect' +Creates bot and handles bot functionality. Simplest way to handle. +```js +let bot +const startBot = () => { + bot = new SlackBot({ + token: // bot token, + name: // app name + }) + bot.on('start', err => { + if (err) { + // login function is what actually starts the RTM connection. set retry to 10 secs if connection fails + setTimeout(bot.login(), 10000) + } + // logs RTM connection success + logger.info(`bot has started successfully`) + }) + bot.on('close', err => { + try { + // this will attempt to reconnect before failing the bot. Will also log the error that occurred. + if (err) { + // execute start function (includes creating bot again and refreshing RTM session + startBot() + } + } catch (err) { + // logs bot reconnect error + logger.error(`Bot crashed.. \n ${err}`) + } + }) + bot.on('error', err => { + // handle error event + }) + bot.on('message', async message => { + // handling message event + } +} +startBot() +``` ###Response Handler The simplest way for handling response is callback function, which is specified as a last argument: From b09bab585dd059d1835bd9d6712d43c30c8b10fb Mon Sep 17 00:00:00 2001 From: Ian Sabey Date: Thu, 25 Oct 2018 15:29:11 -0600 Subject: [PATCH 6/6] removed error and message event handlers realized the code styling and event handling for my code was less generic (not everyone has one handle function for message event). removed and noted that any bot event handler should be in the startBot() function --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6301bbb..68e9878 100644 --- a/README.md +++ b/README.md @@ -120,12 +120,11 @@ const startBot = () => { logger.error(`Bot crashed.. \n ${err}`) } }) - bot.on('error', err => { - // handle error event - }) - bot.on('message', async message => { - // handling message event - } + + // any other bot event handler/functionality should be included in the start function. + . + . + . } startBot() ```