pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.03k stars 3.57k forks source link

CORRUPTED PDF #2953

Closed mateustrindadee closed 4 months ago

mateustrindadee commented 4 months ago

Is there an existing issue for this?

Describe the bug

I'm integrating the "https://www.chatpdf.com/" API into my BOT. This AI has 2 ways to send the PDF, via UPLOAD and via LINK.

I'm using it via UPLOAD, for this I download the PDF from WhatsApp and send it to the API endpoint, but in the process of downloading from WhatsApp, the PDF is corrupted and I can't resolve it.

Expected behavior

It was supposed to download the PDF from WhatsApp and upload it to the endpoint and the API would then return the PDF summary

Steps to Reproduce the Bug or Issue

1st Reply to a PDF with the command "!resume" 2nd It will download onto the machine. 3rd Once downloaded, it will be sent to the endpoint 4th You will respond to the summary of the PDF sent.

Relevant Code

if (message.body === '!resume' && message.hasQuotedMsg) { const quotedMsg = await message.getQuotedMessage(); let chat = await message.getChat(); if (chat.isGroup) { message.reply('[!] Erro, uso somente no privado.'); } else if (quotedMsg.hasMedia) { try {

            const attachmentData = await quotedMsg.downloadMedia();
            const attachmentPath = process.cwd() + `/pdfs/${attachmentData.filename}`;

            fs.writeFileSync(attachmentPath, attachmentData.data);

            // Fazer o anexo do pdf
            const formData = new FormData();
            formData.append('file', fs.createReadStream(attachmentPath));
            //
            console.log(`Arquivo salvo em: ${attachmentPath}`);

            // Header ChatPDF // Key
            const config = {
                headers: {
                    'x-api-key': 'API KEY',
                    ...formData.getHeaders(),
                }
            };
            //

            axios.post("https://api.chatpdf.com/v1/sources/add-file", formData, config)
            .then(response => {
                // Manipula os dados recebidos da API
                console.log("Source ID:", response.data.sourceId);

                client.sendMessage(message.from, 'Arquivo enviado com sucesso.');
            })
            .catch(error => {
                console.error('Erro:', error.message);
                console.log('Response:', error.response.data);
                client.sendMessage(message.from, 'Erro ao enviar o arquivo.');
            });
        } catch (error) {
            console.error('Erro ao fazer o download da mídia:', error);
            client.sendMessage(message.from, 'Erro ao fazer o download da mídia.');
        }
    } 
}

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Windows, Android e tudo atuailzado.

Additional context

image

No response

alechkos commented 4 months ago

Not related to the lib