neue tglib

This commit is contained in:
Flummi 2017-12-03 17:37:55 +01:00
parent fea6228c14
commit b0439bee63

View File

@ -2,43 +2,86 @@ import { logger } from "../log.js";
import { getLevel } from "../admin.js"; import { getLevel } from "../admin.js";
import { spurdo } from "../spurdo.js"; import { spurdo } from "../spurdo.js";
const tgapi = require("node-telegram-bot-api") const rp = require("request-promise")
, EventEmitter = require("events").EventEmitter , EventEmitter = require("events").EventEmitter;
, util = require("util");
export class tg { export class tg extends EventEmitter {
constructor(options) { constructor(options) {
EventEmitter.call(this); super();
this.options = options || {}; this.options = options || {};
this.token = options.token || null; this.token = options.token || null;
this.options.polling = options.polling || true; this.options.pollrate = options.pollrate || 1000;
this.client = new tgapi(this.options.token, this.options); this.api = `https://api.telegram.org/bot${this.token}`;
this.lastUpdate = 0;
this.server = { this.server = {
channel: new Map(), channel: new Map(),
user: new Map(), user: new Map(),
me: { me: {},
nickname: "Uwe"
},
spurdo: false spurdo: false
}; };
this.connect().then(() => {
this.client.on("message", msg => { this.poller = setInterval(() => { this.poll(); }, this.options.pollrate);
if (!this.server.user.has(msg.from.first_name))
this.server.user.set(msg.from.username || msg.from.first_name, {
nick: msg.from.first_name,
username: msg.from.username,
account: msg.from.id.toString(),
prefix: `${msg.from.username}!${msg.from.id.toString()}`,
id: msg.from.id
});
if (msg.date >= (~~(Date.now() / 1000) - 10) && msg.text !== undefined)
this.emit("data", ["message", this.reply(msg)]);
}); });
} }
send(id, msg) { connect() {
this.client.sendMessage(id, msg, { parse_mode: "HTML" }); return new Promise((resolve, reject) => {
rp(`${this.api}/getMe`, { json: true })
.then(res => {
if(res.ok) {
this.me = res.result;
this.server.me = {
nick: 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
reject();
})
.catch(err => 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 => console.log(err));
}
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 => console.log(err));
} }
reply(tmp) { reply(tmp) {
return { return {
@ -80,4 +123,3 @@ export class tg {
; ;
} }
} }
util.inherits(tg, EventEmitter);