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.43k stars 3.68k forks source link

Error occured when sending mp4 media #2838

Closed gensart-ai closed 7 months ago

gensart-ai commented 7 months ago

Is there an existing issue for this?

Describe the bug

image

Hi, this error came when I want to send an MP4 video (base64 encoded from axios) file,

I can't read what the error is trying to say.

Expected behavior

I expect it to be sent, but it returns an error.

Steps to Reproduce the Bug or Issue

On sending a video, and it returns an error

Relevant Code

This is the snapshot of the code :

import { MessageMedia } from 'whatsapp-web.js';
import { Executor } from '@/command-hive';
import axios, { AxiosError } from 'axios';
import * as wweb from '@utils/wweb';
import * as logger from '@utils/logger';
import * as cheerio from 'cheerio';
import config from '@/env';

const IG_DOWNLOADER_URL = 'you don't need this';
const SPOOFED_USER_AGENT = 'Mozilla (Firefox Inc.)';

type InstagramDom = {
    status: string,
    v?: string,
    data: string
}

type InstagramMedia = string | undefined;

const retrieveInstagramDom = async (instagramUrl: string) => {
    const response = await axios.post(IG_DOWNLOADER_URL, {
        q: instagramUrl,
    }, {
        withCredentials: true,
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
            'User-Agent': SPOOFED_USER_AGENT
        }
    });

    return response.data;
}

const processInstagramDom = (instagramDom: InstagramDom) => {
    if (instagramDom.v === undefined) return undefined;

    const $ = cheerio.load(instagramDom.data);
    const instagramMediaUrl: InstagramMedia = $('.download-items__btn').find('a.abutton').attr('href');

    return instagramMediaUrl;
}

const downloadMedia = async (instagramMediaUrl: string) => {
    const response = await axios.get(instagramMediaUrl, {
        responseType: 'arraybuffer',
        headers: {
            'User-Agent': SPOOFED_USER_AGENT
        }
    });

    return {
        media: Buffer.from(response.data, 'binary').toString('base64'),
        mime_type: response.headers['content-type']
    };
}

const instagramDownloader: Executor = async (client, message) => {
    const instagramUrl = message.body.split(' ')[1];

    if (instagramUrl == undefined) {
        wweb.replyMessage(message, `${config.botShortName} tidak melihat adanya URL video IG kamu :(.\n\nGunakan format: \`.ig [URL video IG]\` ya!`);
        return 0;
    }

    try {
        const instagramDom: InstagramDom = await retrieveInstagramDom(instagramUrl);
        const instagramMediaUrl: InstagramMedia = processInstagramDom(instagramDom);

        if (instagramMediaUrl === undefined) {
            wweb.replyMessage(message, `${config.botShortName} tidak dapat memproses link IG kamu, coba salin ulang lagi dari Instagram nya ya !`);
            return 0;
        }

        wweb.replyMessage(message, `Tunggu sebentar ya, ${config.botShortName} sedang memproses link IG kamu...`);

        downloadMedia(instagramMediaUrl).then(instagramMedia => {
            wweb.replyMessage(message, new MessageMedia(instagramMedia.mime_type, instagramMedia.media));
        });
    } catch (error) {

        const err = error as AxiosError;
        const contact = await message.getContact();
        logger.logError('instagramDownloader - ' + err.cause ?? err.message + ' by ' + contact?.pushname ?? 'unknown');
        wweb.replyMessage(message, `Maaf, ${config.botShortName} mengalami kegagalan saat memprosesnya. Silahkan coba kembali nanti ya! 🙏`);
    }
}

export {
    instagramDownloader
}

FYI. the wweb.replyMessage function only like this :

const replyMessage = (message: Message, content: MessageContent, options: SendingOptions = undefined) => {
    if(!system.isSendingMessageEnabled) { // a bool value that I made on my own
        return 0
    }

    message.reply(content, undefined, options);
}

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: Windows 10 Phone OS: Android Node.js Version 20

Additional context

No response

gensart-ai commented 7 months ago

sorry I literally forgot that the guide says here : https://wwebjs.dev/guide/creating-your-bot/handling-attachments.html#caveat-for-sending-videos-and-gifs

Is this related to my problem? because I didn't implement that thank you.

alechkos commented 7 months ago

use chrome instead of chromium

gensart-ai commented 7 months ago

use chrome instead of chromium

I think so, my problem came from there, thanks for pointing out.

I'm testing with Chrome instead of Chromium. I'll update the issue later if the solution works.