From 8ff5bceca011629ff84635d0c32951eb5119e837 Mon Sep 17 00:00:00 2001 From: alechkos <93551621+alechkos@users.noreply.github.com> Date: Mon, 31 Jul 2023 12:53:35 +0300 Subject: [PATCH] feat: send photo/video as a view once message (#2317) * added isViewOnce property * docs updated * added usage example * fix: ESLint --- example.js | 6 ++++++ index.d.ts | 2 ++ src/Client.js | 7 +++++-- src/util/Injected.js | 5 +++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/example.js b/example.js index 4885398..7fe57d7 100644 --- a/example.js +++ b/example.js @@ -140,6 +140,12 @@ client.on('message', async msg => { const attachmentData = await quotedMsg.downloadMedia(); client.sendMessage(msg.from, attachmentData, { caption: 'Here\'s your requested media.' }); } + } else if (msg.body === '!isviewonce' && msg.hasQuotedMsg) { + const quotedMsg = await msg.getQuotedMessage(); + if (quotedMsg.hasMedia) { + const media = await quotedMsg.downloadMedia(); + await client.sendMessage(msg.from, media, { isViewOnce: true }); + } } else if (msg.body === '!location') { msg.reply(new Location(37.422, -122.084, 'Googleplex\nGoogle Headquarters')); } else if (msg.location) { diff --git a/index.d.ts b/index.d.ts index 7e2b86c..7b54fdd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -909,6 +909,8 @@ declare namespace WAWebJS { sendMediaAsSticker?: boolean /** Send media as document */ sendMediaAsDocument?: boolean + /** Send photo/video as a view once message */ + isViewOnce?: boolean /** Automatically parse vCards and send them as contacts */ parseVCards?: boolean /** Image or videos caption */ diff --git a/src/Client.js b/src/Client.js index e5d45c3..5b3887a 100644 --- a/src/Client.js +++ b/src/Client.js @@ -238,9 +238,9 @@ class Client extends EventEmitter { // Listens to qr token change if (mut.type === 'attributes' && mut.attributeName === 'data-ref') { window.qrChanged(mut.target.dataset.ref); - } else + } // Listens to retry button, when found, click it - if (mut.type === 'childList') { + else if (mut.type === 'childList') { const retry_button = document.querySelector(selectors.QR_RETRY_BUTTON); if (retry_button) retry_button.click(); } @@ -738,6 +738,7 @@ class Client extends EventEmitter { * @property {boolean} [sendVideoAsGif=false] - Send video as gif * @property {boolean} [sendMediaAsSticker=false] - Send media as a sticker * @property {boolean} [sendMediaAsDocument=false] - Send media as a document + * @property {boolean} [isViewOnce=false] - Send photo/video as a view once message * @property {boolean} [parseVCards=true] - Automatically parse vCards and send them as contacts * @property {string} [caption] - Image or video caption * @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to) @@ -779,10 +780,12 @@ class Client extends EventEmitter { if (content instanceof MessageMedia) { internalOptions.attachment = content; + internalOptions.isViewOnce = options.isViewOnce, content = ''; } else if (options.media instanceof MessageMedia) { internalOptions.attachment = options.media; internalOptions.caption = content; + internalOptions.isViewOnce = options.isViewOnce, content = ''; } else if (content instanceof Location) { internalOptions.location = content; diff --git a/src/util/Injected.js b/src/util/Injected.js index a787190..9e2bd90 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -127,6 +127,7 @@ exports.LoadUtils = () => { attOptions.caption = options.caption; } content = options.sendMediaAsSticker ? undefined : attOptions.preview; + attOptions.isViewOnce = options.isViewOnce; delete options.attachment; delete options.sendMediaAsSticker; @@ -232,8 +233,8 @@ exports.LoadUtils = () => { } let listOptions = {}; - if(options.list){ - if(window.Store.Conn.platform === 'smba' || window.Store.Conn.platform === 'smbi'){ + if (options.list) { + if (window.Store.Conn.platform === 'smba' || window.Store.Conn.platform === 'smbi') { throw '[LT01] Whatsapp business can\'t send this yet'; } listOptions = {