From 8edb687283853202cfd1bd924bc6802e483e6658 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Tue, 5 Dec 2017 14:30:45 +0300 Subject: [PATCH] src: Add fileOptions to remaining file-sending methods --- doc/api.md | 30 ++++++++++++++++++------------ src/telegram.js | 30 ++++++++++++++++++------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/doc/api.md b/doc/api.md index 7834fbe..46993ef 100644 --- a/doc/api.md +++ b/doc/api.md @@ -22,7 +22,7 @@ TelegramBot * [.closeWebHook()](#TelegramBot+closeWebHook) ⇒ Promise * [.hasOpenWebHook()](#TelegramBot+hasOpenWebHook) ⇒ Boolean * [.getMe([options])](#TelegramBot+getMe) ⇒ Promise - * [.setWebHook(url, [options])](#TelegramBot+setWebHook) ⇒ Promise + * [.setWebHook(url, [options], [fileOptions])](#TelegramBot+setWebHook) ⇒ Promise * [.deleteWebHook([options])](#TelegramBot+deleteWebHook) ⇒ Promise * [.getWebHookInfo([options])](#TelegramBot+getWebHookInfo) ⇒ Promise * [.getUpdates([options])](#TelegramBot+getUpdates) ⇒ Promise @@ -33,7 +33,7 @@ TelegramBot * [.sendPhoto(chatId, photo, [options], [fileOptions])](#TelegramBot+sendPhoto) ⇒ Promise * [.sendAudio(chatId, audio, [options], [fileOptions])](#TelegramBot+sendAudio) ⇒ Promise * [.sendDocument(chatId, doc, [options], [fileOptions])](#TelegramBot+sendDocument) ⇒ Promise - * [.sendSticker(chatId, sticker, [options])](#TelegramBot+sendSticker) ⇒ Promise + * [.sendSticker(chatId, sticker, [options], [fileOptions])](#TelegramBot+sendSticker) ⇒ Promise * [.sendVideo(chatId, video, [options], [fileOptions])](#TelegramBot+sendVideo) ⇒ Promise * [.sendVideoNote(chatId, videoNote, [options], [fileOptions])](#TelegramBot+sendVideoNote) ⇒ Promise * [.sendVoice(chatId, voice, [options], [fileOptions])](#TelegramBot+sendVoice) ⇒ Promise @@ -43,7 +43,7 @@ TelegramBot * [.restrictChatMember(chatId, userId, [options])](#TelegramBot+restrictChatMember) ⇒ Promise * [.promoteChatMember(chatId, userId, [options])](#TelegramBot+promoteChatMember) ⇒ Promise * [.exportChatInviteLink(chatId, [options])](#TelegramBot+exportChatInviteLink) ⇒ Promise - * [.setChatPhoto(chatId, photo, [options])](#TelegramBot+setChatPhoto) ⇒ Promise + * [.setChatPhoto(chatId, photo, [options], [fileOptions])](#TelegramBot+setChatPhoto) ⇒ Promise * [.deleteChatPhoto(chatId, [options])](#TelegramBot+deleteChatPhoto) ⇒ Promise * [.setChatTitle(chatId, title, [options])](#TelegramBot+setChatTitle) ⇒ Promise * [.setChatDescription(chatId, description, [options])](#TelegramBot+setChatDescription) ⇒ Promise @@ -81,9 +81,9 @@ TelegramBot * [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ Promise * [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ Promise * [.getStickerSet(name, [options])](#TelegramBot+getStickerSet) ⇒ Promise - * [.uploadStickerFile(userId, pngSticker, [options])](#TelegramBot+uploadStickerFile) ⇒ Promise - * [.createNewStickerSet(userId, name, title, pngSticker, emojis, [options])](#TelegramBot+createNewStickerSet) ⇒ Promise - * [.addStickerToSet(userId, name, pngSticker, emojis, [options])](#TelegramBot+addStickerToSet) ⇒ Promise + * [.uploadStickerFile(userId, pngSticker, [options], [fileOptions])](#TelegramBot+uploadStickerFile) ⇒ Promise + * [.createNewStickerSet(userId, name, title, pngSticker, emojis, [options], [fileOptions])](#TelegramBot+createNewStickerSet) ⇒ Promise + * [.addStickerToSet(userId, name, pngSticker, emojis, [options], [fileOptions])](#TelegramBot+addStickerToSet) ⇒ Promise * [.setStickerPositionInSet(sticker, position, [options])](#TelegramBot+setStickerPositionInSet) ⇒ Promise * [.deleteStickerFromSet(sticker, [options])](#TelegramBot+deleteStickerFromSet) ⇒ Promise * _static_ @@ -225,7 +225,7 @@ Returns basic information about the bot in form of a `User` object. -### telegramBot.setWebHook(url, [options]) ⇒ Promise +### telegramBot.setWebHook(url, [options], [fileOptions]) ⇒ Promise Specify an url to receive incoming updates via an outgoing webHook. This method has an [older, compatible signature][setWebHook-v0.25.0] that is being deprecated. @@ -238,6 +238,7 @@ that is being deprecated. | url | String | URL where Telegram will make HTTP Post. Leave empty to delete webHook. | | [options] | Object | Additional Telegram query options | | [options.certificate] | String | stream.Stream | PEM certificate key (public). | +| [fileOptions] | Object | Optional file related meta-data | @@ -396,7 +397,7 @@ Send Document -### telegramBot.sendSticker(chatId, sticker, [options]) ⇒ Promise +### telegramBot.sendSticker(chatId, sticker, [options], [fileOptions]) ⇒ Promise Send .webp stickers. **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -407,6 +408,7 @@ Send .webp stickers. | chatId | Number | String | Unique identifier for the message recipient | | sticker | String | stream.Stream | Buffer | A file path, Stream or Buffer. Can also be a `file_id` previously uploaded. Stickers are WebP format files. | | [options] | Object | Additional Telegram query options | +| [fileOptions] | Object | Optional file related meta-data | @@ -570,7 +572,7 @@ Returns exported invite link as String on success. -### telegramBot.setChatPhoto(chatId, photo, [options]) ⇒ Promise +### telegramBot.setChatPhoto(chatId, photo, [options], [fileOptions]) ⇒ Promise Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success. @@ -583,6 +585,7 @@ Returns True on success. | chatId | Number | String | Unique identifier for the message recipient | | photo | stream.Stream | Buffer | A file path or a Stream. | | [options] | Object | Additional Telegram query options | +| [fileOptions] | Object | Optional file related meta-data | @@ -1154,7 +1157,7 @@ Use this method to get a sticker set. On success, a [StickerSet](https://core.te -### telegramBot.uploadStickerFile(userId, pngSticker, [options]) ⇒ Promise +### telegramBot.uploadStickerFile(userId, pngSticker, [options], [fileOptions]) ⇒ Promise Use this method to upload a .png file with a sticker for later use in *createNewStickerSet* and *addStickerToSet* methods (can be used multiple times). Returns the uploaded [File](https://core.telegram.org/bots/api#file) on success. @@ -1166,10 +1169,11 @@ times). Returns the uploaded [File](https://core.telegram.org/bots/api#file) on | userId | Number | User identifier of sticker file owner | | pngSticker | String | stream.Stream | Buffer | A file path or a Stream. Can also be a `file_id` previously uploaded. **Png** image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. | | [options] | Object | Additional Telegram query options | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.createNewStickerSet(userId, name, title, pngSticker, emojis, [options]) ⇒ Promise +### telegramBot.createNewStickerSet(userId, name, title, pngSticker, emojis, [options], [fileOptions]) ⇒ Promise Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set. Returns True on success. @@ -1189,10 +1193,11 @@ Returns True on success. | pngSticker | String | stream.Stream | Buffer | Png image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. | | emojis | String | One or more emoji corresponding to the sticker | | [options] | Object | Additional Telegram query options | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.addStickerToSet(userId, name, pngSticker, emojis, [options]) ⇒ Promise +### telegramBot.addStickerToSet(userId, name, pngSticker, emojis, [options], [fileOptions]) ⇒ Promise Use this method to add a new sticker to a set created by the bot. Returns True on success. @@ -1210,6 +1215,7 @@ Returns True on success. | pngSticker | String | stream.Stream | Buffer | Png image with the sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px | | emojis | String | One or more emoji corresponding to the sticker | | [options] | Object | Additional Telegram query options | +| [fileOptions] | Object | Optional file related meta-data | diff --git a/src/telegram.js b/src/telegram.js index 426737c..5df8468 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -469,10 +469,11 @@ class TelegramBot extends EventEmitter { * delete webHook. * @param {Object} [options] Additional Telegram query options * @param {String|stream.Stream} [options.certificate] PEM certificate key (public). + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#setwebhook */ - setWebHook(url, options = {}) { + setWebHook(url, options = {}, fileOptions = {}) { /* The older method signature was setWebHook(url, cert). * We need to ensure backwards-compatibility while maintaining * consistency of the method signatures throughout the library */ @@ -493,7 +494,7 @@ class TelegramBot extends EventEmitter { if (cert) { try { - const sendData = this._formatSendData('certificate', cert); + const sendData = this._formatSendData('certificate', cert, fileOptions); opts.formData = sendData[0]; opts.qs.certificate = sendData[1]; } catch (ex) { @@ -781,16 +782,17 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} sticker A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. Stickers are WebP format files. * @param {Object} [options] Additional Telegram query options + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendsticker */ - sendSticker(chatId, sticker, options = {}) { + sendSticker(chatId, sticker, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('sticker', sticker); + const sendData = this._formatSendData('sticker', sticker, fileOptions); opts.formData = sendData[0]; opts.qs.sticker = sendData[1]; } catch (ex) { @@ -994,16 +996,17 @@ class TelegramBot extends EventEmitter { * @param {Number|String} chatId Unique identifier for the message recipient * @param {stream.Stream|Buffer} photo A file path or a Stream. * @param {Object} [options] Additional Telegram query options + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#setchatphoto */ - setChatPhoto(chatId, photo, options = {}) { + setChatPhoto(chatId, photo, options = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('photo', photo); + const sendData = this._formatSendData('photo', photo, fileOptions); opts.formData = sendData[0]; opts.qs.photo = sendData[1]; } catch (ex) { @@ -1659,16 +1662,17 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} pngSticker A file path or a Stream. Can also be a `file_id` previously uploaded. **Png** image with the * sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. * @param {Object} [options] Additional Telegram query options + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#uploadstickerfile */ - uploadStickerFile(userId, pngSticker, options = {}) { + uploadStickerFile(userId, pngSticker, options = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.user_id = userId; try { - const sendData = this._formatSendData('png_sticker', pngSticker); + const sendData = this._formatSendData('png_sticker', pngSticker, fileOptions); opts.formData = sendData[0]; opts.qs.png_sticker = sendData[1]; } catch (ex) { @@ -1689,11 +1693,12 @@ class TelegramBot extends EventEmitter { * dimensions must not exceed 512px, and either width or height must be exactly 512px. * @param {String} emojis One or more emoji corresponding to the sticker * @param {Object} [options] Additional Telegram query options + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#createnewstickerset * @todo Add tests for this method! */ - createNewStickerSet(userId, name, title, pngSticker, emojis, options = {}) { + createNewStickerSet(userId, name, title, pngSticker, emojis, options = {}, fileOptions = {}) { const opts = { qs: options, }; @@ -1703,7 +1708,7 @@ class TelegramBot extends EventEmitter { opts.qs.emojis = emojis; opts.qs.mask_position = stringify(options.mask_position); try { - const sendData = this._formatSendData('png_sticker', pngSticker); + const sendData = this._formatSendData('png_sticker', pngSticker, fileOptions); opts.formData = sendData[0]; opts.qs.png_sticker = sendData[1]; } catch (ex) { @@ -1722,11 +1727,12 @@ class TelegramBot extends EventEmitter { * dimensions must not exceed 512px, and either width or height must be exactly 512px * @param {String} emojis One or more emoji corresponding to the sticker * @param {Object} [options] Additional Telegram query options + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#addstickertoset * @todo Add tests for this method! */ - addStickerToSet(userId, name, pngSticker, emojis, options = {}) { + addStickerToSet(userId, name, pngSticker, emojis, options = {}, fileOptions = {}) { const opts = { qs: options, }; @@ -1735,7 +1741,7 @@ class TelegramBot extends EventEmitter { opts.qs.emojis = emojis; opts.qs.mask_position = stringify(options.mask_position); try { - const sendData = this._formatSendData('png_sticker', pngSticker); + const sendData = this._formatSendData('png_sticker', pngSticker, fileOptions); opts.formData = sendData[0]; opts.qs.png_sticker = sendData[1]; } catch (ex) {