own implementation of websockets for slack
This commit is contained in:
parent
f48e458f55
commit
dcc316ff3c
|
@ -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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
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) {
|
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("<")
|
|
||||||
.split(">").join(">")
|
|
||||||
.split("&").join("&")
|
|
||||||
.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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user