own implementation of websockets for slack

This commit is contained in:
Flummi 2019-11-16 21:11:06 +01:00
parent f48e458f55
commit dcc316ff3c
2 changed files with 27 additions and 35 deletions

View File

@ -11,7 +11,6 @@
"author": "Flummi", "author": "Flummi",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies", "flumm-fetch-cookies": "git+https://gitfap.de/keinBot/flumm-fetch-cookies"
"ws": "^7.2.0"
} }
} }

View File

@ -1,6 +1,7 @@
import fetch from "flumm-fetch-cookies"; import https from "https";
import url from "url";
import EventEmitter from "events"; import EventEmitter from "events";
import ws from "ws"; import fetch from "flumm-fetch-cookies";
export default class slack extends EventEmitter { export default class slack extends EventEmitter {
constructor(options) { constructor(options) {
@ -9,14 +10,14 @@ export default class slack extends EventEmitter {
this.token = options.token || null; this.token = options.token || null;
this.set = this.options.set || "all"; this.set = this.options.set || "all";
this.network = "Slack"; this.network = "Slack";
this.api = `https://slack.com/api`; this.api = "https://slack.com/api";
this.socket = null; this.socket = null;
this.server = { this.server = {
set: this.set, set: this.set,
channel: null, channel: null,
user: null, user: null,
wss: { wss: {
link: null, url: null,
socket: null socket: null
}, },
me: {} me: {}
@ -32,34 +33,29 @@ export default class slack extends EventEmitter {
if (!res.ok) if (!res.ok)
throw this.emit("data", ["error", res.description]); // more infos throw this.emit("data", ["error", res.description]); // more infos
this.server.me = { this.server.wss.url = url.parse(res.url);
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.socket = new ws(this.server.wss.link, { https.get({
perMessageDeflate: false hostname: this.server.wss.url.host,
}); path: this.server.wss.url.path,
port: 443,
this.server.wss.socket.on("error", error => { headers: {
console.error(error); "Upgrade": "websocket",
}); "Connection": "Upgrade",
"Sec-WebSocket-Version": 13,
this.server.wss.socket.on("message", async data => { "Sec-WebSocket-Key": new Buffer.from(Array(16).fill().map(e => Math.round(Math.random() * 0xFF))).toString("base64")
data = JSON.parse(data); }
}).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") if(data.type !== "message")
return false; return false;
return this.emit("data", ["message", this.reply(data)]); return this.emit("data", ["message", this.reply(data)]);
})
}); });
console.log(res);
} }
async send(channel, text) { async send(channel, text) {
@ -94,9 +90,6 @@ export default class slack extends EventEmitter {
format(msg) { format(msg) {
return msg.toString() return msg.toString()
.split("<").join("&lt;")
.split(">").join("&gt;")
.split("&").join("&amp;")
.replace(/\[b\](.*?)\[\/b\]/g, "*$1*") // bold .replace(/\[b\](.*?)\[\/b\]/g, "*$1*") // bold
.replace(/\[s\](.*?)\[\/s\]/g, "~$1~") // strike .replace(/\[s\](.*?)\[\/s\]/g, "~$1~") // strike
.replace(/\[i\](.*?)\[\/i\]/g, "_$1_") // italic .replace(/\[i\](.*?)\[\/i\]/g, "_$1_") // italic