2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-22 09:57:10 +00:00

src/telegram: Deprecate auto-enabling Promise cancellation

Side-effects:

  src/telegram: Allow providing custom Promise constructor

References:

  * BR: https://github.com/yagop/node-telegram-bot-api/issues/319
This commit is contained in:
GochoMugo 2017-08-25 18:39:13 +03:00
parent 28160c70cd
commit ef48af49f5
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
3 changed files with 126 additions and 67 deletions

View File

@ -12,68 +12,71 @@ TelegramBot
* [TelegramBot](#TelegramBot)
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
* [.startPolling([options])](#TelegramBot+startPolling) ⇒ <code>Promise</code>
* ~~[.initPolling([options])](#TelegramBot+initPolling) ⇒ <code>Promise</code>~~
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
* [.isPolling()](#TelegramBot+isPolling) ⇒ <code>Boolean</code>
* [.openWebHook()](#TelegramBot+openWebHook) ⇒ <code>Promise</code>
* [.closeWebHook()](#TelegramBot+closeWebHook) ⇒ <code>Promise</code>
* [.hasOpenWebHook()](#TelegramBot+hasOpenWebHook) ⇒ <code>Boolean</code>
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
* [.setWebHook(url, [options])](#TelegramBot+setWebHook) ⇒ <code>Promise</code>
* [.deleteWebHook()](#TelegramBot+deleteWebHook) ⇒ <code>Promise</code>
* [.getWebHookInfo()](#TelegramBot+getWebHookInfo) ⇒ <code>Promise</code>
* [.getUpdates([options])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
* [.processUpdate(update)](#TelegramBot+processUpdate)
* [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code>
* [.answerInlineQuery(inlineQueryId, results, [options])](#TelegramBot+answerInlineQuery) ⇒ <code>Promise</code>
* [.forwardMessage(chatId, fromChatId, messageId, [options])](#TelegramBot+forwardMessage) ⇒ <code>Promise</code>
* [.sendPhoto(chatId, photo, [options])](#TelegramBot+sendPhoto) ⇒ <code>Promise</code>
* [.sendAudio(chatId, audio, [options])](#TelegramBot+sendAudio) ⇒ <code>Promise</code>
* [.sendDocument(chatId, doc, [options], [fileOpts])](#TelegramBot+sendDocument) ⇒ <code>Promise</code>
* [.sendSticker(chatId, sticker, [options])](#TelegramBot+sendSticker) ⇒ <code>Promise</code>
* [.sendVideo(chatId, video, [options])](#TelegramBot+sendVideo) ⇒ <code>Promise</code>
* [.sendVideoNote(chatId, videoNote, [options])](#TelegramBot+sendVideoNote) ⇒ <code>Promise</code>
* [.sendVoice(chatId, voice, [options])](#TelegramBot+sendVoice) ⇒ <code>Promise</code>
* [.sendChatAction(chatId, action)](#TelegramBot+sendChatAction) ⇒ <code>Promise</code>
* [.kickChatMember(chatId, userId)](#TelegramBot+kickChatMember) ⇒ <code>Promise</code>
* [.unbanChatMember(chatId, userId)](#TelegramBot+unbanChatMember) ⇒ <code>Promise</code>
* [.restrictChatMember(chatId, userId, [options])](#TelegramBot+restrictChatMember) ⇒ <code>Promise</code>
* [.promoteChatMember(chatId, userId, [options])](#TelegramBot+promoteChatMember) ⇒ <code>Promise</code>
* [.exportChatInviteLink(chatId)](#TelegramBot+exportChatInviteLink) ⇒ <code>Promise</code>
* [.setChatPhoto(chatId, photo)](#TelegramBot+setChatPhoto) ⇒ <code>Promise</code>
* [.deleteChatPhoto(chatId)](#TelegramBot+deleteChatPhoto) ⇒ <code>Promise</code>
* [.setChatTitle(chatId, title)](#TelegramBot+setChatTitle) ⇒ <code>Promise</code>
* [.setChatDescription(chatId, description)](#TelegramBot+setChatDescription) ⇒ <code>Promise</code>
* [.pinChatMessage(chatId, messageId)](#TelegramBot+pinChatMessage) ⇒ <code>Promise</code>
* [.unpinChatMessage(chatId)](#TelegramBot+unpinChatMessage) ⇒ <code>Promise</code>
* [.answerCallbackQuery([options])](#TelegramBot+answerCallbackQuery) ⇒ <code>Promise</code>
* [.editMessageText(text, [options])](#TelegramBot+editMessageText) ⇒ <code>Promise</code>
* [.editMessageCaption(caption, [options])](#TelegramBot+editMessageCaption) ⇒ <code>Promise</code>
* [.editMessageReplyMarkup(replyMarkup, [options])](#TelegramBot+editMessageReplyMarkup) ⇒ <code>Promise</code>
* [.getUserProfilePhotos(userId, [options])](#TelegramBot+getUserProfilePhotos) ⇒ <code>Promise</code>
* [.sendLocation(chatId, latitude, longitude, [options])](#TelegramBot+sendLocation) ⇒ <code>Promise</code>
* [.sendVenue(chatId, latitude, longitude, title, address, [options])](#TelegramBot+sendVenue) ⇒ <code>Promise</code>
* [.sendContact(chatId, phoneNumber, firstName, [options])](#TelegramBot+sendContact) ⇒ <code>Promise</code>
* [.getFile(fileId)](#TelegramBot+getFile) ⇒ <code>Promise</code>
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
* [.onText(regexp, callback)](#TelegramBot+onText)
* [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ <code>Object</code>
* [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ <code>Number</code>
* [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ <code>Object</code>
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ <code>Promise</code>
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ <code>Promise</code>
* [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ <code>Promise</code>
* [.getChatMember(chatId, userId)](#TelegramBot+getChatMember) ⇒ <code>Promise</code>
* [.leaveChat(chatId)](#TelegramBot+leaveChat) ⇒ <code>Promise</code>
* [.sendGame(chatId, gameShortName, [options])](#TelegramBot+sendGame) ⇒ <code>Promise</code>
* [.setGameScore(userId, score, [options])](#TelegramBot+setGameScore) ⇒ <code>Promise</code>
* [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ <code>Promise</code>
* [.deleteMessage(chatId, messageId, [options])](#TelegramBot+deleteMessage) ⇒ <code>Promise</code>
* [.sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, [options])](#TelegramBot+sendInvoice) ⇒ <code>Promise</code>
* [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ <code>Promise</code>
* [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ <code>Promise</code>
* _instance_
* [.startPolling([options])](#TelegramBot+startPolling) ⇒ <code>Promise</code>
* ~~[.initPolling([options])](#TelegramBot+initPolling) ⇒ <code>Promise</code>~~
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
* [.isPolling()](#TelegramBot+isPolling) ⇒ <code>Boolean</code>
* [.openWebHook()](#TelegramBot+openWebHook) ⇒ <code>Promise</code>
* [.closeWebHook()](#TelegramBot+closeWebHook) ⇒ <code>Promise</code>
* [.hasOpenWebHook()](#TelegramBot+hasOpenWebHook) ⇒ <code>Boolean</code>
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
* [.setWebHook(url, [options])](#TelegramBot+setWebHook) ⇒ <code>Promise</code>
* [.deleteWebHook()](#TelegramBot+deleteWebHook) ⇒ <code>Promise</code>
* [.getWebHookInfo()](#TelegramBot+getWebHookInfo) ⇒ <code>Promise</code>
* [.getUpdates([options])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
* [.processUpdate(update)](#TelegramBot+processUpdate)
* [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code>
* [.answerInlineQuery(inlineQueryId, results, [options])](#TelegramBot+answerInlineQuery) ⇒ <code>Promise</code>
* [.forwardMessage(chatId, fromChatId, messageId, [options])](#TelegramBot+forwardMessage) ⇒ <code>Promise</code>
* [.sendPhoto(chatId, photo, [options])](#TelegramBot+sendPhoto) ⇒ <code>Promise</code>
* [.sendAudio(chatId, audio, [options])](#TelegramBot+sendAudio) ⇒ <code>Promise</code>
* [.sendDocument(chatId, doc, [options], [fileOpts])](#TelegramBot+sendDocument) ⇒ <code>Promise</code>
* [.sendSticker(chatId, sticker, [options])](#TelegramBot+sendSticker) ⇒ <code>Promise</code>
* [.sendVideo(chatId, video, [options])](#TelegramBot+sendVideo) ⇒ <code>Promise</code>
* [.sendVideoNote(chatId, videoNote, [options])](#TelegramBot+sendVideoNote) ⇒ <code>Promise</code>
* [.sendVoice(chatId, voice, [options])](#TelegramBot+sendVoice) ⇒ <code>Promise</code>
* [.sendChatAction(chatId, action)](#TelegramBot+sendChatAction) ⇒ <code>Promise</code>
* [.kickChatMember(chatId, userId)](#TelegramBot+kickChatMember) ⇒ <code>Promise</code>
* [.unbanChatMember(chatId, userId)](#TelegramBot+unbanChatMember) ⇒ <code>Promise</code>
* [.restrictChatMember(chatId, userId, [options])](#TelegramBot+restrictChatMember) ⇒ <code>Promise</code>
* [.promoteChatMember(chatId, userId, [options])](#TelegramBot+promoteChatMember) ⇒ <code>Promise</code>
* [.exportChatInviteLink(chatId)](#TelegramBot+exportChatInviteLink) ⇒ <code>Promise</code>
* [.setChatPhoto(chatId, photo)](#TelegramBot+setChatPhoto) ⇒ <code>Promise</code>
* [.deleteChatPhoto(chatId)](#TelegramBot+deleteChatPhoto) ⇒ <code>Promise</code>
* [.setChatTitle(chatId, title)](#TelegramBot+setChatTitle) ⇒ <code>Promise</code>
* [.setChatDescription(chatId, description)](#TelegramBot+setChatDescription) ⇒ <code>Promise</code>
* [.pinChatMessage(chatId, messageId)](#TelegramBot+pinChatMessage) ⇒ <code>Promise</code>
* [.unpinChatMessage(chatId)](#TelegramBot+unpinChatMessage) ⇒ <code>Promise</code>
* [.answerCallbackQuery([options])](#TelegramBot+answerCallbackQuery) ⇒ <code>Promise</code>
* [.editMessageText(text, [options])](#TelegramBot+editMessageText) ⇒ <code>Promise</code>
* [.editMessageCaption(caption, [options])](#TelegramBot+editMessageCaption) ⇒ <code>Promise</code>
* [.editMessageReplyMarkup(replyMarkup, [options])](#TelegramBot+editMessageReplyMarkup) ⇒ <code>Promise</code>
* [.getUserProfilePhotos(userId, [options])](#TelegramBot+getUserProfilePhotos) ⇒ <code>Promise</code>
* [.sendLocation(chatId, latitude, longitude, [options])](#TelegramBot+sendLocation) ⇒ <code>Promise</code>
* [.sendVenue(chatId, latitude, longitude, title, address, [options])](#TelegramBot+sendVenue) ⇒ <code>Promise</code>
* [.sendContact(chatId, phoneNumber, firstName, [options])](#TelegramBot+sendContact) ⇒ <code>Promise</code>
* [.getFile(fileId)](#TelegramBot+getFile) ⇒ <code>Promise</code>
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code>
* [.onText(regexp, callback)](#TelegramBot+onText)
* [.removeTextListener(regexp)](#TelegramBot+removeTextListener) ⇒ <code>Object</code>
* [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) ⇒ <code>Number</code>
* [.removeReplyListener(replyListenerId)](#TelegramBot+removeReplyListener) ⇒ <code>Object</code>
* [.getChat(chatId)](#TelegramBot+getChat) ⇒ <code>Promise</code>
* [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ <code>Promise</code>
* [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ <code>Promise</code>
* [.getChatMember(chatId, userId)](#TelegramBot+getChatMember) ⇒ <code>Promise</code>
* [.leaveChat(chatId)](#TelegramBot+leaveChat) ⇒ <code>Promise</code>
* [.sendGame(chatId, gameShortName, [options])](#TelegramBot+sendGame) ⇒ <code>Promise</code>
* [.setGameScore(userId, score, [options])](#TelegramBot+setGameScore) ⇒ <code>Promise</code>
* [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ <code>Promise</code>
* [.deleteMessage(chatId, messageId, [options])](#TelegramBot+deleteMessage) ⇒ <code>Promise</code>
* [.sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, [options])](#TelegramBot+sendInvoice) ⇒ <code>Promise</code>
* [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ <code>Promise</code>
* [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ <code>Promise</code>
* _static_
* [.Promise](#TelegramBot.Promise)
<a name="new_TelegramBot_new"></a>
@ -974,6 +977,23 @@ Use this method to confirm shipping of a product.
| ok | <code>Boolean</code> | Specify if every order details are ok |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot.Promise"></a>
### TelegramBot.Promise
Change Promise library used internally, for all existing and new
instances.
**Kind**: static property of <code>[TelegramBot](#TelegramBot)</code>
| Param | Type |
| --- | --- |
| customPromise | <code>function</code> |
**Example**
```js
const TelegramBot = require('node-telegram-bot-api');
TelegramBot.Promise = myPromise;
```
* * *

View File

@ -7,7 +7,6 @@ const TelegramBotPolling = require('./telegramPolling');
const debug = require('debug')('node-telegram-bot-api');
const EventEmitter = require('eventemitter3');
const fileType = require('file-type');
const Promise = require('bluebird');
const request = require('request-promise');
const streamedRequest = require('request');
const qs = require('querystring');
@ -18,6 +17,7 @@ const URL = require('url');
const fs = require('fs');
const pump = require('pump');
const deprecate = require('depd')('node-telegram-bot-api');
let Promise = require('bluebird');
const _messageTypes = [
'audio',
@ -49,10 +49,27 @@ const _deprecatedMessageTypes = [
'new_chat_participant', 'left_chat_participant'
];
// enable cancellation
Promise.config({
cancellation: true,
});
// Enable Promise cancellation.
try {
const msg =
'Automatic enabling of cancellation of promises is deprecated.\n' +
'In the future, you will have to enable it yourself.\n' +
'See https://github.com/yagop/node-telegram-bot-api/issues/319.';
deprecate(msg);
Promise.config({
cancellation: true,
});
} catch (ex) {
/* eslint-disable no-console */
const msg =
'error: Enabling Promise cancellation failed.\n' +
' Temporary fix is to load/require this library as early as possible before using any Promises.';
console.error(msg);
throw ex;
/* eslint-enable no-console */
}
class TelegramBot extends EventEmitter {
@ -64,6 +81,19 @@ class TelegramBot extends EventEmitter {
return _messageTypes;
}
/**
* Change Promise library used internally, for all existing and new
* instances.
* @param {Function} customPromise
*
* @example
* const TelegramBot = require('node-telegram-bot-api');
* TelegramBot.Promise = myPromise;
*/
static set Promise(customPromise) {
Promise = customPromise;
}
on(event, listener) {
if (_deprecatedMessageTypes.indexOf(event) !== -1) {
const url = 'https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#events';

View File

@ -109,6 +109,15 @@ describe('TelegramBot', function telegramSuite() {
});
});
it('allows providing custom Promise library', function test() {
TelegramBot.Promise = global.Promise;
const promise = bot.stopPolling();
assert.ok(promise instanceof global.Promise);
assert.ok(!(promise instanceof Promise));
// revert
TelegramBot.Promise = Promise;
});
it('automatically starts polling', function test() {
assert.equal(botPolling.isPolling(), true);
return utils.isPollingMockServer(pollingPort2);