Go-whatsapp: Sending Media File (audio, video, document. image)

Created on 24 Oct 2018  路  16Comments  路  Source: Rhymen/go-whatsapp

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.

Most helpful comment

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

All 16 comments

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

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

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 =/

@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.

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

You need to format the media files first before sending it

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

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

@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

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

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

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! :)

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';
}

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0xhex picture 0xhex  路  9Comments

lutpiero picture lutpiero  路  8Comments

Valdenirmezadri picture Valdenirmezadri  路  5Comments

carloslfu picture carloslfu  路  5Comments

jhow2892 picture jhow2892  路  3Comments