From 4f50f7350c708c61444529402849581d9d386748 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Mon, 16 Oct 2017 13:07:58 +0300 Subject: [PATCH] src/telegram: Add API v3.2 methods Notes: Add methods: * TelegramBot#createNewStickerSet * TelegramBot#addStickerToSet * TelegramBot#setStickerPositionInSet * TelegramBot#deleteStickerFromSet Tests are missing! References: * FR: https://github.com/yagop/node-telegram-bot-api/issues/407 --- CHANGELOG.md | 5 ++- doc/api.md | 83 +++++++++++++++++++++++++++++++++++++++++ src/telegram.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b60ccb7..6140b84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). Added: 1. Add methods: - * (#429) Add *TelegramBot#getStickerSet* (by @CapacitorSet, @LibertyLocked) - * (#430) Add *TelegramBot#uploadStickerFile* (by @CapacitorSet) + * (#429) *TelegramBot#getStickerSet* (by @CapacitorSet, @LibertyLocked) + * (#430) *TelegramBot#uploadStickerFile* (by @CapacitorSet) + * *TelegramBot#createNewStickerSet*, *TelegramBot#addStickerToSet*, *TelegramBot#setStickerPositionInSet*, *TelegramBot#deleteStickerFromSet* (by @GochoMugo) * * * diff --git a/doc/api.md b/doc/api.md index 8863b19..66b7df2 100644 --- a/doc/api.md +++ b/doc/api.md @@ -77,6 +77,10 @@ TelegramBot * [.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 + * [.setStickerPositionInSet(sticker, position)](#TelegramBot+setStickerPositionInSet) ⇒ Promise + * [.deleteStickerFromSet(sticker)](#TelegramBot+deleteStickerFromSet) ⇒ Promise * _static_ * [.Promise](#TelegramBot.Promise) @@ -1007,6 +1011,85 @@ times). Returns the uploaded [File](https://core.telegram.org/bots/api#file) on | 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 | + + +### telegramBot.createNewStickerSet(userId, name, title, pngSticker, emojis, [options]) ⇒ 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. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#createnewstickerset +**Todo** + +- [ ] Add tests for this method! + + +| Param | Type | Description | +| --- | --- | --- | +| userId | Number | User identifier of created sticker set owner | +| name | String | Short name of sticker set, to be used in `t.me/addstickers/` URLs (e.g., *animals*) | +| title | String | Sticker set title, 1-64 characters | +| 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 | + + + +### telegramBot.addStickerToSet(userId, name, pngSticker, emojis, [options]) ⇒ Promise +Use this method to add a new sticker to a set created by the bot. +Returns True on success. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#addstickertoset +**Todo** + +- [ ] Add tests for this method! + + +| Param | Type | Description | +| --- | --- | --- | +| userId | Number | User identifier of sticker set owner | +| name | String | Sticker set name | +| 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 | + + + +### telegramBot.setStickerPositionInSet(sticker, position) ⇒ Promise +Use this method to move a sticker in a set created by the bot to a specific position. +Returns True on success. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#setstickerpositioninset +**Todo** + +- [ ] Add tests for this method! + + +| Param | Type | Description | +| --- | --- | --- | +| sticker | String | File identifier of the sticker | +| position | Number | New sticker position in the set, zero-based | + + + +### telegramBot.deleteStickerFromSet(sticker) ⇒ Promise +Use this method to delete a sticker from a set created by the bot. +Returns True on success. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#deletestickerfromset +**Todo** + +- [ ] Add tests for this method! + + +| Param | Type | Description | +| --- | --- | --- | +| sticker | String | File identifier of the sticker | + ### TelegramBot.Promise diff --git a/src/telegram.js b/src/telegram.js index cf16922..e44eb86 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -1538,6 +1538,105 @@ class TelegramBot extends EventEmitter { } return this._request('uploadStickerFile', opts); } + + /** + * 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. + * + * @param {Number} userId User identifier of created sticker set owner + * @param {String} name Short name of sticker set, to be used in `t.me/addstickers/` URLs (e.g., *animals*) + * @param {String} title Sticker set title, 1-64 characters + * @param {String|stream.Stream|Buffer} pngSticker 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 {String} emojis One or more emoji corresponding to the sticker + * @param {Object} [options] Additional Telegram query options + * @return {Promise} + * @see https://core.telegram.org/bots/api#createnewstickerset + * @todo Add tests for this method! + */ + createNewStickerSet(userId, name, title, pngSticker, emojis, options = {}) { + const opts = { + qs: options, + }; + opts.qs.user_id = userId; + opts.qs.name = name; + opts.qs.title = title; + opts.qs.emojis = emojis; + try { + const sendData = this._formatSendData('png_sticker', pngSticker); + opts.formData = sendData[0]; + opts.qs.png_sticker = sendData[1]; + } catch (ex) { + return Promise.reject(ex); + } + return this._request('createNewStickerSet', opts); + } + + /** + * Use this method to add a new sticker to a set created by the bot. + * Returns True on success. + * + * @param {Number} userId User identifier of sticker set owner + * @param {String} name Sticker set name + * @param {String|stream.Stream|Buffer} pngSticker 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 {String} emojis One or more emoji corresponding to the sticker + * @param {Object} [options] Additional Telegram query options + * @return {Promise} + * @see https://core.telegram.org/bots/api#addstickertoset + * @todo Add tests for this method! + */ + addStickerToSet(userId, name, pngSticker, emojis, options = {}) { + const opts = { + qs: options, + }; + opts.qs.user_id = userId; + opts.qs.name = name; + opts.qs.emojis = emojis; + try { + const sendData = this._formatSendData('png_sticker', pngSticker); + opts.formData = sendData[0]; + opts.qs.png_sticker = sendData[1]; + } catch (ex) { + return Promise.reject(ex); + } + return this._request('addStickerToSet', opts); + } + + /** + * Use this method to move a sticker in a set created by the bot to a specific position. + * Returns True on success. + * + * @param {String} sticker File identifier of the sticker + * @param {Number} position New sticker position in the set, zero-based + * @return {Promise} + * @see https://core.telegram.org/bots/api#setstickerpositioninset + * @todo Add tests for this method! + */ + setStickerPositionInSet(sticker, position) { + const form = { + sticker, + position, + }; + return this._request('setStickerPositionInSet', { form }); + } + + /** + * Use this method to delete a sticker from a set created by the bot. + * Returns True on success. + * + * @param {String} sticker File identifier of the sticker + * @return {Promise} + * @see https://core.telegram.org/bots/api#deletestickerfromset + * @todo Add tests for this method! + */ + deleteStickerFromSet(sticker) { + const form = { + sticker, + }; + return this._request('deleteStickerFromSet', { form }); + } } module.exports = TelegramBot;