diff --git a/src/telegram.js b/src/telegram.js index c1e6b80..d9ce8b4 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -1,10 +1,9 @@ -var EventEmitter = require('events').EventEmitter; +var TelegramBotWebHook = require('./telegramWebHook'); var debug = require('debug')('node-telegram-bot-api'); +var EventEmitter = require('events').EventEmitter; var Promise = require("bluebird"); var request = require("request"); var stream = require('stream'); -var https = require('https'); -var http = require('http'); var util = require('util'); var mime = require('mime'); var path = require('path'); @@ -42,81 +41,13 @@ var TelegramBot = function (token, options) { } if (options.webHook) { - var port = options.webHook.port || 8443; - var key = options.webHook.key; - var cert = options.webHook.cert; - var host = options.webHook.host; - this._configureWebHook(port, host, key, cert); + var binded = this._processUpdate.bind(this); + this._WebHook = new TelegramBotWebHook(token, options.webHook, binded); } }; util.inherits(TelegramBot, EventEmitter); -TelegramBot.prototype._configureWebHook = function (port, host, key, cert) { - var binded = this._requestListener.bind(this); - - if (key && cert) { // HTTPS Server - debug('HTTPS WebHook enabled'); - var options = { - key: fs.readFileSync(key), - cert: fs.readFileSync(cert) - }; - this._webServer = https.createServer(options, binded); - } else { - debug('HTTP WebHook enabled'); - this._webServer = http.createServer(binded); - } - - this._webServer.listen(port, host, function () { - debug("WebHook listening on port %s", port); - }); -}; - -TelegramBot.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(); - }); - req.on('end', function () { - try { - debug('WebHook request fullBody', fullBody); - var data = JSON.parse(fullBody); - self._processUpdate(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(); - } -}; - -TelegramBot.prototype._processUpdate = function (update) { - if (update.message) { - this.emit('message', update.message); - } -}; - -TelegramBot.prototype._processUpdates = function (updates) { - for (var i = 0; i < updates.length; i++) { - this._processUpdate(updates[i]); - } -}; - TelegramBot.prototype._polling = function (timeout) { var self = this; this.getUpdates(timeout).then(function (data) { @@ -129,6 +60,20 @@ TelegramBot.prototype._polling = function (timeout) { }); }; +TelegramBot.prototype._processUpdate = function (update) { + debug('Process Update', update); + debug('Process Update message', update.message); + if (update.message) { + this.emit('message', update.message); + } +}; + +TelegramBot.prototype._processUpdates = function (updates) { + for (var i = 0; i < updates.length; i++) { + this._processUpdate(updates[i]); + } +}; + TelegramBot.prototype._request = function (path, options) { if (!this.token) { throw new Error('Telegram Bot Token not provided!'); diff --git a/src/telegramWebHook.js b/src/telegramWebHook.js new file mode 100644 index 0000000..97119b5 --- /dev/null +++ b/src/telegramWebHook.js @@ -0,0 +1,66 @@ +var debug = require('debug')('node-telegram-bot-api'); +var https = require('https'); +var http = require('http'); +var util = require('util'); +var fs = require('fs'); + +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); + + 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); + } + + this._webServer.listen(options.port, options.host, function () { + debug("WebHook listening on port %s", options.port); + }); +}; + +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(); + }); + 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(); + } +}; + +module.exports = TelegramBotWebHook; diff --git a/test/index.js b/test/index.js index be00311..bf2ecee 100644 --- a/test/index.js +++ b/test/index.js @@ -60,7 +60,7 @@ describe('Telegram', function () { method: 'POST' }, function (error, response, body) { response.statusCode.should.not.be.equal(200); - bot._webServer.close(); + bot._WebHook._webServer.close(); done(); }); }); @@ -72,7 +72,7 @@ describe('Telegram', function () { method: 'GET' }, function (error, response, body) { response.statusCode.should.not.be.equal(200); - bot._webServer.close(); + bot._WebHook._webServer.close(); done(); }); }); @@ -80,7 +80,7 @@ describe('Telegram', function () { it('should emit a `message` on HTTP WebHook', function (done) { var bot = new Telegram(TOKEN, {webHook: true}); bot.on('message', function (msg) { - bot._webServer.close(); + bot._WebHook._webServer.close(); done(); }); var url = 'http://localhost:8443/bot'+TOKEN; @@ -91,7 +91,7 @@ describe('Telegram', function () { headers: { "content-type": "application/json", }, - body: JSON.stringify({update_id: 0, message: {text: 'test'}}) + body: {update_id: 0, message: {text: 'test'}} }); }); @@ -105,7 +105,7 @@ describe('Telegram', function () { }; var bot = new Telegram(TOKEN, opts); bot.on('message', function (msg) { - bot._webServer.close(); + bot._WebHook._webServer.close(); done(); }); var url = 'https://localhost:8443/bot'+TOKEN;