Skip to content

Commit

Permalink
src/telegram: Add TelegramBot#uploadStickerFile
Browse files Browse the repository at this point in the history
Notes:

    * Closes PR #430

References:

    * FR: #407
    * PR: #430
    * PR-by: @CapacitorSet
  • Loading branch information
CapacitorSet authored and GochoMugo committed Oct 7, 2017
1 parent a2d85b8 commit 8fd243e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

Added:

1. (#429) Add *TelegramBot#getStickerSet* (by @CapacitorSet, @LibertyLocked)
1. Add methods:
* (#429) Add *TelegramBot#getStickerSet* (by @CapacitorSet, @LibertyLocked)
* (#430) Add *TelegramBot#uploadStickerFile* (by @CapacitorSet)


* * *
Expand Down
16 changes: 16 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ TelegramBot
* [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ <code>Promise</code>
* [.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>
* _static_
* [.Promise](#TelegramBot.Promise)

Expand Down Expand Up @@ -991,6 +992,21 @@ Use this method to get a sticker set. On success, a [StickerSet](https://core.te
| name | <code>String</code> | Name of the sticker set |
| [options] | <code>Object</code> | Additional Telegram query options |

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

### telegramBot.uploadStickerFile(userId, pngSticker, [options]) ⇒ <code>Promise</code>
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.

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

| Param | Type | Description |
| --- | --- | --- |
| userId | <code>Number</code> | User identifier of sticker file owner |
| 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.Promise"></a>

### TelegramBot.Promise
Expand Down
26 changes: 26 additions & 0 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,32 @@ class TelegramBot extends EventEmitter {
form.name = name;
return this._request('getStickerSet', { form });
}

/**
* 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.
*
* @param {Number} userId User identifier of sticker file owner
* @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
* @return {Promise}
* @see https://core.telegram.org/bots/api#uploadstickerfile
*/
uploadStickerFile(userId, pngSticker, options = {}) {
const opts = {
qs: options,
};
opts.qs.user_id = userId;
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('uploadStickerFile', opts);
}
}

module.exports = TelegramBot;
Binary file added test/data/sticker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,4 +1371,18 @@ describe('TelegramBot', function telegramSuite() {
});
});
});

describe('#uploadStickerFile', function sendPhotoSuite() {
before(function before() {
utils.handleRatelimit(bot, 'uploadStickerFile', this);
});
it('should upload a sticker from file', function test() {
const sticker = `${__dirname}/data/sticker.png`;
return bot.uploadStickerFile(USERID, sticker).then(resp => {
assert.ok(is.object(resp));
assert.ok(is.string(resp.file_id));
});
});
// Other tests (eg. Buffer, URL) are skipped, because they rely on the same features as sendPhoto.
});
}); // End Telegram

0 comments on commit 8fd243e

Please sign in to comment.