From dcc316ff3c30d9a939002f5116c195e4a4f0158e Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 16 Nov 2019 21:11:06 +0100 Subject: [PATCH] own implementation of websockets for slack --- package.json | 3 +-- src/clients/slack.mjs | 59 +++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 524597c..1fe20f0 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "author": "Flummi", "license": "ISC", "dependencies": { - "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies", - "ws": "^7.2.0" + "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies" } } diff --git a/src/clients/slack.mjs b/src/clients/slack.mjs index d03ab72..9da05e6 100644 --- a/src/clients/slack.mjs +++ b/src/clients/slack.mjs @@ -1,6 +1,7 @@ -import fetch from "flumm-fetch-cookies"; +import https from "https"; +import url from "url"; import EventEmitter from "events"; -import ws from "ws"; +import fetch from "flumm-fetch-cookies"; export default class slack extends EventEmitter { constructor(options) { @@ -9,14 +10,14 @@ export default class slack extends EventEmitter { this.token = options.token || null; this.set = this.options.set || "all"; this.network = "Slack"; - this.api = `https://slack.com/api`; + this.api = "https://slack.com/api"; this.socket = null; this.server = { set: this.set, channel: null, user: null, wss: { - link: null, + url: null, socket: null }, me: {} @@ -32,34 +33,29 @@ export default class slack extends EventEmitter { if (!res.ok) throw this.emit("data", ["error", res.description]); // more infos - this.server.me = { - id: res.self.id, - nickname: res.self.name, - }; - this.server.wss.link = res.url; - this.server.team = res.team; - this.server.channel = res.channel; + this.server.wss.url = url.parse(res.url); - this.server.wss.socket = new ws(this.server.wss.link, { - perMessageDeflate: false + https.get({ + hostname: this.server.wss.url.host, + path: this.server.wss.url.path, + port: 443, + headers: { + "Upgrade": "websocket", + "Connection": "Upgrade", + "Sec-WebSocket-Version": 13, + "Sec-WebSocket-Key": new Buffer.from(Array(16).fill().map(e => Math.round(Math.random() * 0xFF))).toString("base64") + } + }).on("upgrade", (_, sock) => { + this.server.wss.socket = sock; + + this.server.wss.socket.on("data", data => { + data = JSON.parse(data.slice(data.indexOf("{")).toString()); + if(data.type !== "message") + return false; + + return this.emit("data", ["message", this.reply(data)]); + }) }); - - this.server.wss.socket.on("error", error => { - console.error(error); - }); - - this.server.wss.socket.on("message", async data => { - data = JSON.parse(data); - - if(data.type !== "message") - return false; - - return this.emit("data", ["message", this.reply(data)]); - }); - - console.log(res); - - } async send(channel, text) { @@ -94,9 +90,6 @@ export default class slack extends EventEmitter { format(msg) { return msg.toString() - .split("<").join("<") - .split(">").join(">") - .split("&").join("&") .replace(/\[b\](.*?)\[\/b\]/g, "*$1*") // bold .replace(/\[s\](.*?)\[\/s\]/g, "~$1~") // strike .replace(/\[i\](.*?)\[\/i\]/g, "_$1_") // italic