Rhymen / go-whatsapp

WhatsApp Web API
MIT License
2.07k stars 493 forks source link

Sending Media File (audio, video, document. image) #69

Closed gerlim29 closed 5 years ago

gerlim29 commented 5 years ago

Hello i have compiled some of the Type that a certain media file to be sent but I can't seem to figure out what are the other ones for example in this repo you can send image by using this code

This is the code that was currently in the project

image, err := os.Open("image.jpg") // the path of the image
fmt.Println(image)
if err != nil {
    fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
    os.Exit(1)
}

msg := whatsapp.ImageMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "###########@s.whatsapp.net",
    },
    Type:    "image/jpeg", // jpeg needs to be change if you used different extension
    Caption: "Hello Gopher!",
    Content: image,
}

err = wac.Send(msg)
if err != nil {
    fmt.Fprintf(os.Stderr, "error sending file: %v\n", err)
    os.Exit(1)
}

and these are the msg that I compiled in order to send different types of media files

For mp4

msg := whatsapp.VideoMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "###########@s.whatsapp.net",
    },
    Type:    "video/mp4",
    Caption: "Hello Gopher!",
    Content: video, // the code os.Open("video.mp4")
}

For ogg

msg := whatsapp.AudioMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "###########@s.whatsapp.net",
    },
    Type:    "audio/ogg; codecs=opus",
    Content: audio, // the code os.Open("audio.ogg")
}

Hope somehow this will help other go-whatsapp devs here

My concern is that i can't seem to figure out what is the type when sending these kinds of file 1) audio (mp3) 2) documents (docx/doc/pdf) any possible document

My code for mp3

msg := whatsapp.AudioMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "###########@s.whatsapp.net",
    },
    Type:    "audio/mp3; codecs=opus", // tried removing `; codecs=opus` but nothing happens
    Content: audio, // os.Open("audio.mp3")
}

This is the error message that I'm receiving when sending mp3 error sending file: message sending responded with %!d(float64=400)

My code for document

msg := whatsapp.DocumentMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "###########@s.whatsapp.net",
    },
    Type:      "document/docx",
    Thumbnail: thumbnail,
    Content:   document, // os.Open("document.docx")
}

I can send the document. The problem is that when viewing it on the receiver phone i can't seem to open it. I believe also that i have a docx/pdf opener on the receiver phone since I can open pdf and docx file from that phone that was downloaded from the internet.

gerlim29 commented 5 years ago

I traced and checked the response from the HandleDocumentMessage and I was able to find out that the Type for these media files are

Documents .DOCX/.DOC = application/vnd.openxmlformats-officedocument.wordprocessingml.document .XLSX = application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .PDF = application/pdf

Zip Files .DEB = application/vnd.debian.binary-package .GZ = application/gzip .ZIP = application/zip .7z = application/x-7z-compressed

PS: I can't still figure out how mp3 attachments are sent

codenoid commented 5 years ago

any update on this ? should i create new issue ? hope this feature (at least send video, document) are available in go-whatsapp

Valdenirmezadri commented 5 years ago

this work for me:

audio, err := os.Open("audio.wav") //<--- msg2 := whatsapp.AudioMessage{ Info: whatsapp.MessageInfo{ RemoteJid: "5547***@s.whatsapp.net", }, Type: "audio/ogg; codecs=opus", //<--- Content: audio, }

i need send on ogg format but can't make this on chrome + angular + opus-media-recorder =/

gerlim29 commented 5 years ago

@codenoid , @Valdenirmezadri

You need to format the media files first before sending it. apparently web.whatsapp.com formats their media files first in the front-end before sending it through their REST API.

Valdenirmezadri commented 5 years ago

Yes, That's exactly what I can't do in the angular

gerlim29 commented 5 years ago

@Valdenirmezadri

https://www.npmjs.com/package/node-file-converter

codenoid commented 5 years ago

You need to format the media files first before sending it

what kind of format ? can you give me example like sending mp4 file ?

Valdenirmezadri commented 5 years ago

You need to format the media files first before sending it

what kind of format ? can you give me example like sending mp4 file ?

to send for whatsapp, the audio need to be a .ogg file

Good news!!!

After a whole day, I was able to stream audio to .ogg on angular

gerlim29 commented 5 years ago

@codenoid

https://www.videoconverterfactory.com/tips/whatsapp-video-format.html

gerlim29 commented 5 years ago

@Valdenirmezadri

as long as whatsapp suppports the codec even if it isn't .ogg file it will work. even .webm is supported. the only problem I have is for .mp3 which is not also allowed in whatsapp web and application

https://www.videoconverterfactory.com/tips/whatsapp-video-format.html

gerlim29 commented 5 years ago

Sometimes .ogg also won't work if the codec is not correct. whatsapp in my knowledge only accepts a codec of opus for .ogg audio. try download different .ogg files from google and test

Valdenirmezadri commented 5 years ago

i make a test sending the ogg that my angular saved and worked

albertord84 commented 4 years ago

Hi @Valdenirmezadri! I'm facing this problem rigth now :( could you said us how do you do it with "chrome + angular + opus-media-recorder" please? Perhaps some part of your code help more thanks! :)

Valdenirmezadri commented 4 years ago

Hi @Valdenirmezadri! I'm facing this problem rigth now :( could you said us how do you do it with "chrome + angular + opus-media-recorder" please? Perhaps some part of your code help more thanks! :)

Good morning!

My code:

import OpusMediaRecorder from 'opus-media-recorder';

const options = { mimeType: 'audio/ogg; codecs=opus' }; const workerOptions = { encoderWorkerFactory() { return new Worker('assets/opus/encoderWorker.umd.js'); }, OggOpusEncoderWasmPath: 'OggOpusEncoder.wasm', }; @Component({ ...

private _initiateRecording(): void { navigator.mediaDevices.getUserMedia(this.mediaConstraints).then((stream) => { if (this.recorder && this.recorder.state !== 'inactive') { throw new Error('Stop the recorder first'); } this._createMediaRecorder(stream); }).catch(err => this._errorCallback(err)); }

private _createMediaRecorder(stream: any): void { this.recorder = new OpusMediaRecorder(stream, options, workerOptions);

this.recorder.onstart = () => { this.dataChunks = []; };

this.recorder.ondataavailable = (e: any) => {
  this.dataChunks.push(e.data);
};

this.recorder.onstop = () => {
  this.blob = new Blob(this.dataChunks, {type: this.recorder.mimeType});
  this._processRecording();
};
this._startRecord();

}

private _startRecord(): void { this.recorder.start(); }

private _stopRecording() { this.recorder.stop(); }

private _processRecording() { if (this.sendAudio) { this.recording$.emit(this.blob); } else { this.recording$.emit(new Blob()); } }

private _errorCallback(error: string) { this.error = 'Não foi possível rodar o audio neste navegador'; }

tigsdev commented 4 years ago

with version 0, 4, 2080 video messages are not sent

AlmogBaku commented 4 years ago

notice: for recordings- you MUST use OOG with OPUS codec, mono, sample-rate of 16kHz, bitrate of 21kbs.