Skip to content

Commit

Permalink
src/telegram: Add API v3.2 methods
Browse files Browse the repository at this point in the history
Notes:

    Add methods:
      * TelegramBot#createNewStickerSet
      * TelegramBot#addStickerToSet
      * TelegramBot#setStickerPositionInSet
      * TelegramBot#deleteStickerFromSet

    Tests are missing!

References:

    * FR: #407
  • Loading branch information
GochoMugo committed Oct 16, 2017
1 parent 8fd243e commit 4f50f73
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


* * *
Expand Down
83 changes: 83 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ TelegramBot
* [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ <code>Promise</code>
* [.getStickerSet(name, [options])](#TelegramBot+getStickerSet) ⇒ <code>Promise</code>
* [.uploadStickerFile(userId, pngSticker, [options])](#TelegramBot+uploadStickerFile) ⇒ <code>Promise</code>
* [.createNewStickerSet(userId, name, title, pngSticker, emojis, [options])](#TelegramBot+createNewStickerSet) ⇒ <code>Promise</code>
* [.addStickerToSet(userId, name, pngSticker, emojis, [options])](#TelegramBot+addStickerToSet) ⇒ <code>Promise</code>
* [.setStickerPositionInSet(sticker, position)](#TelegramBot+setStickerPositionInSet) ⇒ <code>Promise</code>
* [.deleteStickerFromSet(sticker)](#TelegramBot+deleteStickerFromSet) ⇒ <code>Promise</code>
* _static_
* [.Promise](#TelegramBot.Promise)

Expand Down Expand Up @@ -1007,6 +1011,85 @@ times). Returns the uploaded [File](https://core.telegram.org/bots/api#file) on
| pngSticker | <code>String</code> &#124; <code>stream.Stream</code> &#124; <code>Buffer</code> | 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] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+createNewStickerSet"></a>

### telegramBot.createNewStickerSet(userId, name, title, pngSticker, emojis, [options]) ⇒ <code>Promise</code>
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 <code>[TelegramBot](#TelegramBot)</code>
**See**: https://core.telegram.org/bots/api#createnewstickerset
**Todo**

- [ ] Add tests for this method!


| Param | Type | Description |
| --- | --- | --- |
| userId | <code>Number</code> | User identifier of created sticker set owner |
| name | <code>String</code> | Short name of sticker set, to be used in `t.me/addstickers/` URLs (e.g., *animals*) |
| title | <code>String</code> | Sticker set title, 1-64 characters |
| pngSticker | <code>String</code> &#124; <code>stream.Stream</code> &#124; <code>Buffer</code> | 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 | <code>String</code> | One or more emoji corresponding to the sticker |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+addStickerToSet"></a>

### telegramBot.addStickerToSet(userId, name, pngSticker, emojis, [options]) ⇒ <code>Promise</code>
Use this method to add a new sticker to a set created by the bot.
Returns True on success.

**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**See**: https://core.telegram.org/bots/api#addstickertoset
**Todo**

- [ ] Add tests for this method!


| Param | Type | Description |
| --- | --- | --- |
| userId | <code>Number</code> | User identifier of sticker set owner |
| name | <code>String</code> | Sticker set name |
| pngSticker | <code>String</code> &#124; <code>stream.Stream</code> &#124; <code>Buffer</code> | 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 | <code>String</code> | One or more emoji corresponding to the sticker |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+setStickerPositionInSet"></a>

### telegramBot.setStickerPositionInSet(sticker, position) ⇒ <code>Promise</code>
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 <code>[TelegramBot](#TelegramBot)</code>
**See**: https://core.telegram.org/bots/api#setstickerpositioninset
**Todo**

- [ ] Add tests for this method!


| Param | Type | Description |
| --- | --- | --- |
| sticker | <code>String</code> | File identifier of the sticker |
| position | <code>Number</code> | New sticker position in the set, zero-based |

<a name="TelegramBot+deleteStickerFromSet"></a>

### telegramBot.deleteStickerFromSet(sticker) ⇒ <code>Promise</code>
Use this method to delete a sticker from a set created by the bot.
Returns True on success.

**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**See**: https://core.telegram.org/bots/api#deletestickerfromset
**Todo**

- [ ] Add tests for this method!


| Param | Type | Description |
| --- | --- | --- |
| sticker | <code>String</code> | File identifier of the sticker |

<a name="TelegramBot.Promise"></a>

### TelegramBot.Promise
Expand Down
99 changes: 99 additions & 0 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 4f50f73

Please sign in to comment.