slack.. beta

This commit is contained in:
Flummi 2019-11-16 07:44:39 +01:00
parent 59a2b77176
commit d5c25ddabd
2 changed files with 98 additions and 3 deletions

View File

@ -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"
}
}

95
src/clients/slack.mjs Normal file
View File

@ -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)
};
}
}