node-telegram-bot-api
Advanced tools
Comparing version 0.22.1 to 0.23.0
@@ -29,23 +29,33 @@ 'use strict'; | ||
var _messageTypes = ['text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created']; | ||
var TelegramBot = function (_EventEmitter) { | ||
_inherits(TelegramBot, _EventEmitter); | ||
/** | ||
* Both request method to obtain messages are implemented. To use standard polling, set `polling: true` | ||
* on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate. | ||
* Emits `message` when a message arrives. | ||
* | ||
* @class TelegramBot | ||
* @constructor | ||
* @param {String} token Bot Token | ||
* @param {Object} [options] | ||
* @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options | ||
* @param {String|Number} [options.polling.timeout=10] Polling time in seconds | ||
* @param {String|Number} [options.polling.interval=2000] Interval between requests in miliseconds | ||
* @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options | ||
* @param {String} [options.webHook.key] PEM private key to webHook server. | ||
* @param {String} [options.webHook.cert] PEM certificate (public) to webHook server. | ||
* @see https://core.telegram.org/bots/api | ||
*/ | ||
_createClass(TelegramBot, null, [{ | ||
key: 'messageTypes', | ||
get: function get() { | ||
return _messageTypes; | ||
} | ||
/** | ||
* Both request method to obtain messages are implemented. To use standard polling, set `polling: true` | ||
* on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate. | ||
* Emits `message` when a message arrives. | ||
* | ||
* @class TelegramBot | ||
* @constructor | ||
* @param {String} token Bot Token | ||
* @param {Object} [options] | ||
* @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options | ||
* @param {String|Number} [options.polling.timeout=10] Polling time in seconds | ||
* @param {String|Number} [options.polling.interval=2000] Interval between requests in miliseconds | ||
* @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options | ||
* @param {String} [options.webHook.key] PEM private key to webHook server. | ||
* @param {String} [options.webHook.cert] PEM certificate (public) to webHook server. | ||
* @see https://core.telegram.org/bots/api | ||
*/ | ||
}]); | ||
function TelegramBot(token) { | ||
@@ -58,3 +68,31 @@ var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
_this.processUpdate = function (update) { | ||
_this.options = options; | ||
_this.token = token; | ||
_this.textRegexpCallbacks = []; | ||
_this.onReplyToMessages = []; | ||
if (options.polling) { | ||
_this.initPolling(); | ||
} | ||
if (options.webHook) { | ||
_this._WebHook = new TelegramBotWebHook(token, options.webHook, _this.processUpdate); | ||
} | ||
return _this; | ||
} | ||
_createClass(TelegramBot, [{ | ||
key: 'initPolling', | ||
value: function initPolling() { | ||
if (this._polling) { | ||
this._polling.abort = true; | ||
this._polling.lastRequest.cancel('Polling restart'); | ||
} | ||
this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate); | ||
} | ||
}, { | ||
key: 'processUpdate', | ||
value: function processUpdate(update) { | ||
var _this2 = this; | ||
debug('Process Update %j', update); | ||
@@ -68,7 +106,7 @@ var message = update.message; | ||
debug('Process Update message %j', message); | ||
_this.emit('message', message); | ||
this.emit('message', message); | ||
var processMessageType = function processMessageType(messageType) { | ||
if (message[messageType]) { | ||
debug('Emtting %s: %j', messageType, message); | ||
_this.emit(messageType, message); | ||
_this2.emit(messageType, message); | ||
} | ||
@@ -79,3 +117,3 @@ }; | ||
debug('Text message'); | ||
_this.textRegexpCallbacks.forEach(function (reg) { | ||
this.textRegexpCallbacks.forEach(function (reg) { | ||
debug('Matching %s whith', message.text, reg.regexp); | ||
@@ -91,3 +129,3 @@ var result = reg.regexp.exec(message.text); | ||
// Only callbacks waiting for this message | ||
_this.onReplyToMessages.forEach(function (reply) { | ||
this.onReplyToMessages.forEach(function (reply) { | ||
// Message from the same chat | ||
@@ -105,44 +143,16 @@ if (reply.chatId === message.chat.id) { | ||
debug('Process Update inline_query %j', inlineQuery); | ||
_this.emit('inline_query', inlineQuery); | ||
this.emit('inline_query', inlineQuery); | ||
} else if (chosenInlineResult) { | ||
debug('Process Update chosen_inline_result %j', chosenInlineResult); | ||
_this.emit('chosen_inline_result', chosenInlineResult); | ||
this.emit('chosen_inline_result', chosenInlineResult); | ||
} else if (callbackQuery) { | ||
debug('Process Update callback_query %j', callbackQuery); | ||
_this.emit('callback_query', callbackQuery); | ||
this.emit('callback_query', callbackQuery); | ||
} | ||
}; | ||
_this.options = options; | ||
_this.token = token; | ||
_this.textRegexpCallbacks = []; | ||
_this.onReplyToMessages = []; | ||
if (options.polling) { | ||
_this.initPolling(); | ||
} | ||
if (options.webHook) { | ||
_this._WebHook = new TelegramBotWebHook(token, options.webHook, _this.processUpdate); | ||
} | ||
return _this; | ||
} | ||
// used so that other funcs are not non-optimizable | ||
// Telegram message events | ||
_createClass(TelegramBot, [{ | ||
key: 'initPolling', | ||
value: function initPolling() { | ||
if (this._polling) { | ||
this._polling.abort = true; | ||
this._polling.lastRequest.cancel('Polling restart'); | ||
} | ||
this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate); | ||
} | ||
}, { | ||
key: '_safeParse', | ||
// used so that other funcs are not non-optimizable | ||
value: function _safeParse(json) { | ||
@@ -161,3 +171,3 @@ try { | ||
value: function _request(_path) { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -186,3 +196,3 @@ var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var data = _this2._safeParse(resp.body); | ||
var data = _this3._safeParse(resp.body); | ||
if (data.ok) { | ||
@@ -442,2 +452,3 @@ return data.result; | ||
* @param {Object} [options] Additional Telegram query options | ||
* @param {Object} [fileOpts] Optional file related meta-data | ||
* @return {Promise} | ||
@@ -451,2 +462,3 @@ * @see https://core.telegram.org/bots/api#sendDocument | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var fileOpts = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
@@ -460,2 +472,5 @@ var opts = { | ||
opts.qs.document = content[1]; | ||
if (opts.formData && Object.keys(fileOpts).length) { | ||
opts.formData.document.options = fileOpts; | ||
} | ||
return this._request('sendDocument', opts); | ||
@@ -779,3 +794,3 @@ } | ||
value: function getFileLink(fileId) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -786,3 +801,3 @@ return this.getFile(fileId).then(function (resp) { | ||
host: 'api.telegram.org', | ||
pathname: '/file/bot' + _this3.token + '/' + resp.file_path | ||
pathname: '/file/bot' + _this4.token + '/' + resp.file_path | ||
}); | ||
@@ -851,5 +866,2 @@ }); | ||
TelegramBot.messageTypes = ['text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created']; | ||
module.exports = TelegramBot; |
{ | ||
"name": "node-telegram-bot-api", | ||
"version": "0.22.1", | ||
"version": "0.23.0", | ||
"description": "Telegram Bot API", | ||
@@ -20,3 +20,3 @@ "main": "./lib/telegram.js", | ||
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"gen-doc": "./node_modules/.bin/babel --no-babelrc --plugins transform-es2015-spread,transform-es2015-destructuring,transform-strict-mode,transform-es2015-parameters,transform-es2015-shorthand-properties,transform-object-rest-spread,transform-class-properties -d lib-doc src && ./node_modules/.bin/jsdoc2md --src lib-doc/telegram.js -t README.hbs > README.md", | ||
"gen-doc": "./node_modules/.bin/jsdoc2md --src src/telegram.js -t README.hbs > README.md", | ||
"eslint": "./node_modules/.bin/eslint ./src" | ||
@@ -23,0 +23,0 @@ }, |
@@ -69,3 +69,3 @@ [![Build Status](https://travis-ci.org/yagop/node-telegram-bot-api.svg?branch=master)](https://travis-ci.org/yagop/node-telegram-bot-api) [![Build status](https://ci.appveyor.com/api/projects/status/ujko6bsum3g5msjh/branch/master?svg=true)](https://ci.appveyor.com/project/yagop/node-telegram-bot-api/branch/master) [![Coverage Status](https://coveralls.io/repos/yagop/node-telegram-bot-api/badge.svg?branch=master)](https://coveralls.io/r/yagop/node-telegram-bot-api?branch=master) [![bitHound Score](https://www.bithound.io/github/yagop/node-telegram-bot-api/badges/score.svg)](https://www.bithound.io/github/yagop/node-telegram-bot-api) [![https://telegram.me/node_telegram_bot_api](https://img.shields.io/badge/π¬ Telegram-node__telegram__bot__api-blue.svg)](https://telegram.me/node_telegram_bot_api) [![https://telegram.me/Yago_Perez](https://img.shields.io/badge/π¬ Telegram-Yago__Perez-blue.svg)](https://telegram.me/Yago_Perez) | ||
* [.sendAudio(chatId, audio, [options])](#TelegramBot+sendAudio) β <code>Promise</code> | ||
* [.sendDocument(chatId, doc, [options])](#TelegramBot+sendDocument) β <code>Promise</code> | ||
* [.sendDocument(chatId, doc, [options], [fileOpts])](#TelegramBot+sendDocument) β <code>Promise</code> | ||
* [.sendSticker(chatId, sticker, [options])](#TelegramBot+sendSticker) β <code>Promise</code> | ||
@@ -214,3 +214,3 @@ * [.sendVideo(chatId, video, [options])](#TelegramBot+sendVideo) β <code>Promise</code> | ||
### telegramBot.sendDocument(chatId, doc, [options]) β <code>Promise</code> | ||
### telegramBot.sendDocument(chatId, doc, [options], [fileOpts]) β <code>Promise</code> | ||
Send Document | ||
@@ -226,2 +226,3 @@ | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
| [fileOpts] | <code>Object</code> | Optional file related meta-data | | ||
@@ -228,0 +229,0 @@ <a name="TelegramBot+sendSticker"></a> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73029
932
493
1