This commit is contained in:
Flummi 2017-11-08 22:52:20 +01:00
parent 1d44d7baa1
commit 8f3624d75f
3 changed files with 51 additions and 28 deletions

47
src/inc/clients/tg.js Normal file
View File

@ -0,0 +1,47 @@
let tgapi = require('node-telegram-bot-api');
let EventEmitter = require("events").EventEmitter;
let util = require("util");
class tg {
constructor(options) {
EventEmitter.call(this);
this.options = options || {};
this.token = options.token || null;
this.options.polling = options.polling || true;
this.client = new tgapi(this.options.token, this.options);
this.client.on('message', msg => {
let time = ~~(Date.now() / 1000);
if (msg.date >= (time - 10))
this.emit('message', this.reply(msg));
});
}
send(id, msg) {
this.client.sendMessage(id, msg);
}
reply(tmp) {
return {
type: "tg",
channel: tmp.chat.title,
channelid: tmp.chat.id,
user: {
nick: tmp.from.first_name,
username: tmp.from.username
},
message: tmp.text,
time: tmp.date,
reply: function (msg) {
this.send(tmp.chat.id, msg);
},
replyAction: function (msg) {
this.send(tmp.chat.id, msg);
},
replyNotice: function (msg) {
this.send(tmp.chat.id, msg);
}
};
}
}
module.exports = tg;
util.inherits(tg, EventEmitter);

View File

@ -1,8 +1,9 @@
import { cfg, read } from './cfg.js';
import { loadEvents } from './lib.js';
let irclib = require('./irc.js');
let tglib = require('node-telegram-bot-api');
let irclib = require('./clients/irc.js');
let tglib = require('./clients/tg.js')
//let tglib = require('node-telegram-bot-api');
//const safeEval = require('safe-eval');
const util = require('util');
//const fs = require('fs');
@ -25,7 +26,7 @@ const wrapper = function() {
clients.push({
name: "tg",
type: "tg",
client: new tglib(cfg.client[srv].token, { polling: cfg.client[srv].polling })
client: new tglib(cfg.client[srv])
});
break;
}
@ -33,35 +34,10 @@ const wrapper = function() {
clients.forEach(client => {
client.client.on("message", e => {
if(client.type === "tg")
e = replytg(client.client, e);
this.emit('message', e);
});
});
};
const replytg = (bot, tmp) => { // muss noch hier raus
return {
type: "tg",
channel: tmp.chat.title,
channelid: tmp.chat.id,
user: {
nick: tmp.from.first_name,
username: tmp.from.username
},
message: tmp.text,
time: tmp.date,
reply: function(msg) {
bot.sendMessage(tmp.chat.id, msg);
},
replyAction: function(msg) {
bot.sendMessage(tmp.chat.id, msg);
},
replyNotice: function(msg) {
bot.sendMessage(tmp.chat.id, msg);
}
};
};
util.inherits(wrapper, EventEmitter);
export default { wrapper, clients };