153 lines
4.5 KiB
JavaScript
153 lines
4.5 KiB
JavaScript
import { logger } from "../log";
|
|
import { getLevel } from "../admin";
|
|
import { spurdo } from "../spurdo";
|
|
|
|
import rp from "request-promise";
|
|
import EventEmitter from "events";
|
|
|
|
export class tg extends EventEmitter {
|
|
constructor(options) {
|
|
super();
|
|
this.options = options || {};
|
|
this.token = options.token || null;
|
|
this.options.pollrate = options.pollrate || 1000;
|
|
this.network = "Telegram";
|
|
this.api = `https://api.telegram.org/bot${this.token}`;
|
|
this.lastUpdate = 0;
|
|
this.server = {
|
|
channel: new Map(),
|
|
user: new Map(),
|
|
me: {},
|
|
spurdo: false
|
|
};
|
|
this.connect().then(() => {
|
|
this.poller = setInterval(() => { this.poll(); }, this.options.pollrate);
|
|
});
|
|
}
|
|
connect() {
|
|
return new Promise((resolve, reject) => {
|
|
rp(`${this.api}/getMe`, { json: true })
|
|
.then(res => {
|
|
if(res.ok) {
|
|
this.me = res.result;
|
|
this.server.me = {
|
|
nickname: res.result.first_name,
|
|
username: res.result.username,
|
|
account: res.result.id.toString(),
|
|
prefix: `${res.result.username}!${res.result.id.toString()}`,
|
|
id: res.result.id.toString()
|
|
};
|
|
resolve();
|
|
}
|
|
else {
|
|
logger.error(`(${this.network}) ${res}`);
|
|
reject();
|
|
}
|
|
})
|
|
.catch(err => {
|
|
logger.error(`(${this.network}) ${err.message}`);
|
|
reject();
|
|
});
|
|
});
|
|
}
|
|
poll() {
|
|
rp(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`, { json:true })
|
|
.then(res => {
|
|
if(res.ok && res.result.length > 0) {
|
|
res = res.result[res.result.length-1];
|
|
this.lastUpdate = res.update_id + 1;
|
|
if (res.message.date >= ~~(Date.now() / 1000) - 10) {
|
|
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
|
|
this.server.user.set(res.message.from.username || res.message.from.first_name, {
|
|
nick: res.message.from.first_name,
|
|
username: res.message.from.username,
|
|
account: res.message.from.id.toString(),
|
|
prefix: `${res.message.from.username}!${res.message.from.id.toString()}`,
|
|
id: res.message.from.id
|
|
});
|
|
}
|
|
this.emit("data", ["message", this.reply(res.message)]);
|
|
}
|
|
}
|
|
})
|
|
.catch(err => {
|
|
logger.error(`(${this.network}) ${err.message}`);
|
|
});
|
|
}
|
|
send(chatid, msg, reply = null) {
|
|
const opts = {
|
|
method: 'POST',
|
|
uri: `${this.api}/sendMessage`,
|
|
body: {
|
|
chat_id: chatid,
|
|
text: msg,
|
|
parse_mode: "HTML"
|
|
},
|
|
json: true
|
|
};
|
|
if(reply)
|
|
opts.body.reply_to_message_id = reply;
|
|
rp(opts)
|
|
.then(res => {})
|
|
.catch(err => {
|
|
logger.error(`(${this.network}) ${err.message}`);
|
|
});
|
|
}
|
|
reply(tmp) {
|
|
return {
|
|
type: "tg",
|
|
network: "Telegram",
|
|
channel: tmp.chat.title,
|
|
channelid: tmp.chat.id,
|
|
user: {
|
|
prefix: `${tmp.from.username}!${tmp.from.id}`,
|
|
nick: tmp.from.first_name,
|
|
username: tmp.from.username,
|
|
account: tmp.from.id.toString(),
|
|
level: getLevel("Telegram", {
|
|
prefix: `${tmp.from.username}!${tmp.from.id}`,
|
|
nick: tmp.from.first_name,
|
|
username: tmp.from.username,
|
|
account: tmp.from.id.toString()
|
|
})
|
|
},
|
|
self: this.server,
|
|
message: tmp.text,
|
|
time: tmp.date,
|
|
raw: tmp,
|
|
reply: msg => this.send(tmp.chat.id, this.format(msg)),
|
|
replyAction: msg => this.send(tmp.chat.id, this.format(`Uwe ${msg}`)),
|
|
replyNotice: msg => this.send(tmp.chat.id, this.format(msg)),
|
|
_user: this.server.user
|
|
};
|
|
}
|
|
format(msg) {
|
|
if(this.server.spurdo)
|
|
msg = spurdo(msg);
|
|
return msg.toString()
|
|
.replace("<", "<")
|
|
.replace(">", ">")
|
|
.replace(/\[b\](.*?)\[\/b\]/g, "<b>$1</b>") // bold
|
|
.replace(/\[i\](.*?)\[\/i\]/g, "<i>$1</i>") // italic
|
|
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2")
|
|
;
|
|
}
|
|
}
|
|
|
|
Map.prototype.hasi = function(val) {
|
|
for (let [key] of this)
|
|
if(key.toLowerCase() === val.toLowerCase())
|
|
return true;
|
|
return false;
|
|
};
|
|
Map.prototype.geti = function(val) {
|
|
for (let [key, value] of this)
|
|
if(key.toLowerCase() === val.toLowerCase())
|
|
return value;
|
|
return false;
|
|
};
|
|
Map.prototype.deli = function(val) {
|
|
for (let [key] of this)
|
|
if(key.toLowerCase() === val.toLowerCase())
|
|
this.delete(key);
|
|
}; |