From d844bc08112edeb801071ad9a0245b00f940a248 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 30 Dec 2022 20:02:34 +0100 Subject: [PATCH] fix: Add Thumb --- CHANGELOG.md | 2 +- src/telegram.js | 43 +++++++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b6b621..4c1d8fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). 2. Minor changes: * The parameters `name` and `icon_custom_emoji_id` of the method `editForumTopic` are now optional. - + * Fix add thumb in sendAudio, sendVideo and sendVideoNote ## [0.60.0][0.60.0] - 2022-10-06 1. Support Telegram Bot API v6.3 (@danielperez9430) diff --git a/src/telegram.js b/src/telegram.js index 0316dd1..f1607a2 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -237,6 +237,26 @@ class TelegramBot extends EventEmitter { } } + _fixAddFileThumb(options, opts) { + if (options.thumb) { + if (opts.formData === null) { + opts.formData = {}; + } + + try { + const attachName = 'photo'; + const [formData] = this._formatSendData(attachName, options.thumb.replace('attach://', '')); + + if (formData) { + opts.formData[attachName] = formData[attachName]; + opts.qs.thumb = `attach://${attachName}`; + } + } catch (ex) { + throw Promise.reject(ex); + } + } + } + /** * Make request against the API * @param {String} _path API endpoint @@ -987,28 +1007,11 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('audio', audio, fileOptions); opts.formData = sendData[0]; opts.qs.audio = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } - if (options.thumb) { - if (opts.formData === null) { - opts.formData = {}; - } - - try { - const attachName = 'photo'; - const [formData] = this._formatSendData(attachName, options.thumb.replace('attach://', '')); - - if (formData) { - opts.formData[attachName] = formData[attachName]; - opts.qs.thumb = `attach://${attachName}`; - } - } catch (ex) { - return Promise.reject(ex); - } - } - return this._request('sendAudio', opts); } @@ -1032,9 +1035,11 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('document', doc, fileOptions); opts.formData = sendData[0]; opts.qs.document = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } + return this._request('sendDocument', opts); } @@ -1059,6 +1064,7 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('video', video, fileOptions); opts.formData = sendData[0]; opts.qs.video = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); } @@ -1141,6 +1147,7 @@ class TelegramBot extends EventEmitter { const sendData = this._formatSendData('video_note', videoNote, fileOptions); opts.formData = sendData[0]; opts.qs.video_note = sendData[1]; + this._fixAddFileThumb(options, opts); } catch (ex) { return Promise.reject(ex); }