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