From b9e867573681db83842c1b2e0a5712ff12892241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=C3=A9rez=20Fern=C3=A1ndez?= Date: Sun, 7 Nov 2021 18:55:14 +0100 Subject: [PATCH] fix: editMessageMedia - Now you can send a local file --- src/telegram.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/telegram.js b/src/telegram.js index b204873..7c84f20 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -1231,7 +1231,7 @@ class TelegramBot extends EventEmitter { } /** - * Use this method to approve a chat join request. + * Use this method to approve a chat join request. * The bot must be an administrator in the chat for this to work and must have the can_invite_users administrator right. * Returns True on success. * @@ -1516,7 +1516,43 @@ class TelegramBot extends EventEmitter { * @see https://core.telegram.org/bots/api#editmessagemedia */ editMessageMedia(media, form = {}) { + const regexAttach = /attach:\/\/.+/; + + if (typeof media.media === 'string' && regexAttach.test(media.media)) { + const opts = { + qs: form, + }; + + opts.formData = {}; + + const payload = Object.assign({}, media); + delete payload.media; + + try { + const attachName = String(0); + const [formData] = this._formatSendData( + attachName, + media.media.replace('attach://', ''), + media.fileOptions, + ); + + if (formData) { + opts.formData[attachName] = formData[attachName]; + payload.media = `attach://${attachName}`; + } else { + throw new errors.FatalError(`Failed to process the replacement action for your ${media.type}`); + } + } catch (ex) { + return Promise.reject(ex); + } + + opts.qs.media = JSON.stringify(payload); + + return this._request('editMessageMedia', opts); + } + form.media = stringify(media); + return this._request('editMessageMedia', { form }); }