neue tglib
This commit is contained in:
parent
fea6228c14
commit
b0439bee63
|
@ -2,43 +2,86 @@ import { logger } from "../log.js";
|
|||
import { getLevel } from "../admin.js";
|
||||
import { spurdo } from "../spurdo.js";
|
||||
|
||||
const tgapi = require("node-telegram-bot-api")
|
||||
, EventEmitter = require("events").EventEmitter
|
||||
, util = require("util");
|
||||
const rp = require("request-promise")
|
||||
, EventEmitter = require("events").EventEmitter;
|
||||
|
||||
export class tg {
|
||||
export class tg extends EventEmitter {
|
||||
constructor(options) {
|
||||
EventEmitter.call(this);
|
||||
super();
|
||||
this.options = options || {};
|
||||
this.token = options.token || null;
|
||||
this.options.polling = options.polling || true;
|
||||
this.client = new tgapi(this.options.token, this.options);
|
||||
|
||||
this.options.pollrate = options.pollrate || 1000;
|
||||
this.api = `https://api.telegram.org/bot${this.token}`;
|
||||
this.lastUpdate = 0;
|
||||
this.server = {
|
||||
channel: new Map(),
|
||||
user: new Map(),
|
||||
me: {
|
||||
nickname: "Uwe"
|
||||
},
|
||||
me: {},
|
||||
spurdo: false
|
||||
};
|
||||
|
||||
this.client.on("message", msg => {
|
||||
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)]);
|
||||
this.connect().then(() => {
|
||||
this.poller = setInterval(() => { this.poll(); }, this.options.pollrate);
|
||||
});
|
||||
}
|
||||
send(id, msg) {
|
||||
this.client.sendMessage(id, msg, { parse_mode: "HTML" });
|
||||
connect() {
|
||||
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) {
|
||||
return {
|
||||
|
@ -80,4 +123,3 @@ export class tg {
|
|||
;
|
||||
}
|
||||
}
|
||||
util.inherits(tg, EventEmitter);
|
Loading…
Reference in New Issue
Block a user