From d5c25ddabdcf426184198f80af89a3d05fdeb1ba Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 16 Nov 2019 07:44:39 +0100 Subject: [PATCH] slack.. beta --- package.json | 6 +-- src/clients/slack.mjs | 95 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/clients/slack.mjs diff --git a/package.json b/package.json index 99f184b..524597c 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,7 @@ "version": "1.0.1", "description": "lul", "main": "src/index.mjs", - "scripts": { - }, + "scripts": {}, "repository": { "type": "git", "url": "git@gitfap.de:keinBot/cuffeo.git" @@ -12,6 +11,7 @@ "author": "Flummi", "license": "ISC", "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" } } diff --git a/src/clients/slack.mjs b/src/clients/slack.mjs new file mode 100644 index 0000000..659e6de --- /dev/null +++ b/src/clients/slack.mjs @@ -0,0 +1,95 @@ +import fetch from "flumm-fetch-cookies"; +import EventEmitter from "events"; +import ws from "ws"; + +export default class slack extends EventEmitter { + constructor(options) { + super(); + this.options = options || {}; + this.token = options.token || null; + this.set = this.options.set || "all"; + this.network = "Slack"; + this.api = `https://slack.com/api`; + this.socket = null; + this.server = { + set: this.set, + channel: null, + user: null, + wss: { + link: null, + socket: null + }, + me: {} + }; + + return (async () => { + await this.connect(); + return this; + })(); + } + async connect() { + const res = await (await fetch(`${this.api}/rtm.connect?token=${this.token}`)).json(); + 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.socket = new ws(this.server.wss.link, { + perMessageDeflate: false + }); + + 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 }) { + return this.server.wss.socket.send(JSON.stringify({ + type: "message", + channel: channel, + text: text + })); + } + + reply(tmp) { + return { + type: "slack", + network: "Slack", + channel: tmp.channel, // get channelname + channelid: tmp.channel, + user: { + prefix: `${tmp.user}!${tmp.user}`, // get username + nick: tmp.user, // get username + username: tmp.user, // get username + account: tmp.user + }, + self: this.server, + message: tmp.text, + time: ~~(Date.now() / 1000), + raw: tmp, + reply: msg => this.send(tmp.channel, msg, tmp.message_id), + replyAction: msg => this.send(tmp.channel, `Uwe ${msg}`, tmp.message_id), + replyNotice: msg => this.send(tmp.channel, msg, tmp.message_id) + }; + } + +}