whatsapp-web.js icon indicating copy to clipboard operation
whatsapp-web.js copied to clipboard

[Feature] Sending a PDF with a custom caption

Open KJoaquin opened this issue 2 years ago • 18 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

When sending a PDF in Base64 or by URL, a caption is sent with the name of the file

Captura de pantalla 2022-11-29 a la(s) 3 18 26 p  m

Expected behavior

Send the pdf with the name and the optional caption separately

with name only: Captura de pantalla 2022-11-29 a la(s) 3 16 39 p  m

With only caption: Captura de pantalla 2022-11-29 a la(s) 3 19 26 p  m

Steps to Reproduce the Bug or Issue

The pdf is sent by code through Base64 and the caption separately:

Captura de pantalla 2022-11-29 a la(s) 3 21 09 p  m

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: Mac Phone: Android - IOS whatsapp-web.js version 1.18.3-alpha.0

Additional context

No response

KJoaquin avatar Nov 29 '22 21:11 KJoaquin

The caption feature is still new and has not been rolled out to everyone fully, when it has I am sure it will be fixed. I am also experiencing the bug

amosayomide05 avatar Nov 29 '22 22:11 amosayomide05

use options { sendMediaAsDocument: true } thats enough!

RC047 avatar Nov 29 '22 22:11 RC047

@RC047 Yes, but what I want is for the same file name not to be sent as caption

image

KJoaquin avatar Nov 29 '22 23:11 KJoaquin

If want same file name in the caption, you can try this: const media = MessageMedia.fromFilePath(fattach); //fattach is full file path await client.sendMessage(number, media, {caption: captionMedia});

tcp5441 avatar Nov 30 '22 07:11 tcp5441

The bug is not in the MessageMedia.... but in the sendMessage, since the name is being copied with the caption

KJoaquin avatar Nov 30 '22 14:11 KJoaquin

Hi Everyone,

I found one location where I can change the filename who send .pdf file:

Maybe anybody can found the issue and fixes this.

On MessageMedia.js

/**
     * Creates a MessageMedia instance from a local file path
     * @param {string} filePath 
     * @returns {MessageMedia}
     */
    static fromFilePath(filePath) {
        const b64data = fs.readFileSync(filePath, {encoding: 'base64'});
        const mimetype = mime.getType(filePath); 
        var filename = path.basename(filePath);

        filename = filename.replace('.pdf', ''); //here

        return new MessageMedia(mimetype, b64data, filename);
    }

isn't my solution, but partialy have helped me at the moment.

See you

pondwar avatar Dec 12 '22 12:12 pondwar

Hello @pondwar I already modified that part and it is not from there

KJoaquin avatar Dec 13 '22 15:12 KJoaquin

Hey guys, I am also facing the same problem.

I haven't found the exact origin of bug in the code. However, after some debugging, I found out that overriding caption in src/util/Injected.js/sendMessage function like this is fixing the issue:

        let attOptions = {};
        if (options.attachment) {
            attOptions = options.sendMediaAsSticker
                ? await window.WWebJS.processStickerData(options.attachment)
                : await window.WWebJS.processMediaData(options.attachment, {
                    forceVoice: options.sendAudioAsVoice,
                    forceDocument: options.sendMediaAsDocument,
                    forceGif: options.sendVideoAsGif
                });

            attOptions.caption = options.caption; // Here

            content = options.sendMediaAsSticker ? undefined : attOptions.preview;

            delete options.attachment;
            delete options.sendMediaAsSticker;
        }

HeyItsJs avatar Dec 27 '22 06:12 HeyItsJs

Hello @pondwar I already modified that part and it is not from there

Hi, unfortunatly its not the solution, but I just raname the .pdf file to message I wanna send and works partialy.

But its not the solution just workaround.

pondwar avatar Dec 27 '22 14:12 pondwar

Hey guys, I am also facing the same problem.

I haven't found the exact origin of bug in the code. However, after some debugging, I found out that overriding caption in src/util/Injected.js/sendMessage function like this is fixing the issue:

        let attOptions = {};
        if (options.attachment) {
            attOptions = options.sendMediaAsSticker
                ? await window.WWebJS.processStickerData(options.attachment)
                : await window.WWebJS.processMediaData(options.attachment, {
                    forceVoice: options.sendAudioAsVoice,
                    forceDocument: options.sendMediaAsDocument,
                    forceGif: options.sendVideoAsGif
                });

            attOptions.caption = options.caption; // Here

            content = options.sendMediaAsSticker ? undefined : attOptions.preview;

            delete options.attachment;
            delete options.sendMediaAsSticker;
        }

We'll provide a PR when it is stable.

purpshell avatar Dec 29 '22 11:12 purpshell

Thanks @PurpShell , yes it worked

KJoaquin avatar Dec 29 '22 14:12 KJoaquin

Thanks @PurpShell , yes it worked

PDFs with captions are not stable yet in whatsapp

purpshell avatar Dec 29 '22 15:12 purpshell

Thanks @PurpShell , yes it worked

PDFs with captions are not stable yet in whatsapp

It could be added to a new version to update to my project

KJoaquin avatar Dec 29 '22 15:12 KJoaquin

I am having the same issue, waiting for the fix Thanks @PurpShell for your suggestion, will try it

mahmouds12 avatar Dec 31 '22 09:12 mahmouds12

Thanks @PurpShell , yes it worked

filamuu avatar Dec 31 '22 18:12 filamuu

How to send like that? give me example code pls

Aromakelapa avatar Jan 10 '23 04:01 Aromakelapa

@pedroslopez Could you add this modification to the repository?

KJoaquin avatar Jan 17 '23 23:01 KJoaquin

I'm also interested in uploading this

kikemarto avatar Jan 24 '23 12:01 kikemarto

Hi, is there any update on this issue? It looks like document caption is officially released a week ago

jonylim avatar Feb 23 '23 15:02 jonylim

Can we put this update out now? It gives a lot of problems and it's a simple solution @pedroslopez

kikemarto avatar Mar 13 '23 14:03 kikemarto

@pedroslopez amazing, good! thanks

kanglerian avatar Mar 27 '23 03:03 kanglerian

I am trying to send a PDF file with some text as caption and custom filename as demonstrated in the example provided by @KJoaquin I created the MessageMedia object as follows:

const media = new MessageMedia('application/pdf', string_base64, caption_text);

where the string_base64 is the content of the PDF encoded in base64 format and caption_text is the text I want to put along the file as caption..

I did console.log(media) and found out that filename for the media is undefined.

The following workaround did the job for me:

media.filename = 'attachment.pdf';  // this is the name I want my PDF to be 

And then sending the message as follows:

    await client.sendMessage(chatId, media, { caption: caption_text, attachment: media, sendMediaAsDocument: true });    

I hope it helps.

Kunalvrm555 avatar Jun 18 '23 16:06 Kunalvrm555

I am trying to send a PDF file with some text as caption and custom filename as demonstrated in the example provided by @KJoaquin I created the MessageMedia object as follows:

const media = new MessageMedia('application/pdf', string_base64, caption_text);

where the string_base64 is the content of the PDF encoded in base64 format and caption_text is the text I want to put along the file as caption..

I did console.log(media) and found out that filename for the media is undefined.

The following workaround did the job for me:

media.filename = 'attachment.pdf';  // this is the name I want my PDF to be 

And then sending the message as follows:

    await client.sendMessage(chatId, media, { caption: caption_text, attachment: media, sendMediaAsDocument: true });    

I hope it helps.

Work for me

jarsoftSolutions avatar Sep 07 '23 23:09 jarsoftSolutions