2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-09-05 08:45:44 +00:00

refactor: rewrite to babel, es6 & eslint

This commit is contained in:
AVVS
2016-03-17 02:44:34 +03:00
parent b9649c00bb
commit 83e42201ee
14 changed files with 1062 additions and 1019 deletions

View File

@@ -1,67 +1,85 @@
'use strict';
const debug = require('debug')('node-telegram-bot-api');
const https = require('https');
const http = require('http');
const fs = require('fs');
const bl = require('bl');
var debug = require('debug')('node-telegram-bot-api');
var https = require('https');
var http = require('http');
var fs = require('fs');
class TelegramBotWebHook {
var TelegramBotWebHook = function (token, options, callback) {
this.token = token;
this.callback = callback;
if (typeof options === 'boolean') {
options = {};
}
options.port = options.port || 8443;
var binded = this._requestListener.bind(this);
constructor(token, options, callback) {
this.token = token;
this.callback = callback;
this.regex = new RegExp(this.token);
if (options.key && options.cert) { // HTTPS Server
debug('HTTPS WebHook enabled');
var opts = {
key: fs.readFileSync(options.key),
cert: fs.readFileSync(options.cert)
};
this._webServer = https.createServer(opts, binded);
} else {
debug('HTTP WebHook enabled');
this._webServer = http.createServer(binded);
}
// define opts
if (typeof options === 'boolean') {
options = {}; // eslint-disable-line no-param-reassign
}
options.port = options.port || 8443;
this._webServer.listen(options.port, options.host, function () {
debug('WebHook listening on port %s', options.port);
});
};
if (options.key && options.cert) { // HTTPS Server
debug('HTTPS WebHook enabled');
const opts = {
key: fs.readFileSync(options.key),
cert: fs.readFileSync(options.cert)
};
this._webServer = https.createServer(opts, this._requestListener);
} else {
debug('HTTP WebHook enabled');
this._webServer = http.createServer(this._requestListener);
}
TelegramBotWebHook.prototype._requestListener = function (req, res) {
var self = this;
var regex = new RegExp(this.token);
debug('WebHook request URL:', req.url);
debug('WebHook request headers: %j', req.headers);
// If there isn't token on URL
if (!regex.test(req.url)) {
debug('WebHook request unauthorized');
res.statusCode = 401;
res.end();
} else if (req.method === 'POST') {
var fullBody = '';
req.on('data', function (chunk) {
fullBody += chunk.toString();
this._webServer.listen(options.port, options.host, () => {
debug('WebHook listening on port %s', options.port);
});
req.on('end', function () {
try {
debug('WebHook request fullBody', fullBody);
var data = JSON.parse(fullBody);
self.callback(data);
} catch (error) {
debug(error);
}
res.end('OK');
});
} else { // Authorized but not a POST
debug('WebHook request isn\'t a POST');
res.statusCode = 418; // I'm a teabot!
res.end();
}
};
// used so that other funcs are not non-optimizable
_safeParse(json) {
try {
return JSON.parse(json);
} catch (err) {
debug(err);
return null;
}
}
// pipe+parse body
_parseBody = (err, body) => {
if (err) {
return debug(err);
}
const data = this._safeParse(body);
if (data) {
return this.callback(data);
}
return null;
}
// bound req listener
_requestListener = (req, res) => {
debug('WebHook request URL:', req.url);
debug('WebHook request headers: %j', req.headers);
// If there isn't token on URL
if (!this.regex.test(req.url)) {
debug('WebHook request unauthorized');
res.statusCode = 401;
res.end();
} else if (req.method === 'POST') {
req
.pipe(bl(this._parseBody))
.on('end', () => res.end('OK'));
} else {
// Authorized but not a POST
debug('WebHook request isn\'t a POST');
res.statusCode = 418; // I'm a teabot!
res.end();
}
}
}
module.exports = TelegramBotWebHook;