Rhymen / go-whatsapp

WhatsApp Web API
MIT License
2.05k stars 490 forks source link

Error send message audio #537

Open adarosci opened 3 years ago

adarosci commented 3 years ago

Good night guys, I have a problem sending audio messages, I have tried in many ways but the audio only appears to whoever is sending the receiver the audio does not appear, if i send image or files it is going normally, i even tried to send the audio as a file and it was normally. Does anyone have this problem?

In my example i'm taking an mp3 audio from the web, in case if i use an ogg the same problem, follow code and prints with the results

`package main

import ( "encoding/gob" "fmt" "github.com/Rhymen/go-whatsapp" qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go" "net/http" "os" "time" )

func main() { //create new WhatsApp connection wac, err := whatsapp.NewConn(5 * time.Second) if err != nil { fmt.Fprintf(os.Stderr, "error creating connection: %v\n", err) return }

err = login(wac)
if err != nil {
    fmt.Fprintf(os.Stderr, "error logging in: %v\n", err)
    return
}

<-time.After(3 * time.Second)

img, err := http.Get("https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3")
if err != nil {
    fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
    os.Exit(1)
}

msg := whatsapp.AudioMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "RemoteJid@s.whatsapp.net",
    },
    Length:      27,
    Type:        "audio/mp3",
    Content:     img.Body,
    Ptt:         true,
}

msgId, err := wac.Send(msg)
if err != nil {
    fmt.Fprintf(os.Stderr, "error sending message: %v", err)
    os.Exit(1)
} else {
    fmt.Println("Message Sent -> ID : " + msgId)
}

}

func login(wac *whatsapp.Conn) error { //load saved session session, err := readSession() if err == nil { //restore session session, err = wac.RestoreWithSession(session) if err != nil { return fmt.Errorf("restoring failed: %v\n", err) } } else { //no saved session -> regular login qr := make(chan string) go func() { terminal := qrcodeTerminal.New() terminal.Get(<-qr).Print() }() session, err = wac.Login(qr) if err != nil { return fmt.Errorf("error during login: %v\n", err) } }

//save session
err = writeSession(session)
if err != nil {
    return fmt.Errorf("error saving session: %v\n", err)
}
return nil

}

func readSession() (whatsapp.Session, error) { session := whatsapp.Session{} file, err := os.Open(os.TempDir() + "/whatsappSession.gob") if err != nil { return session, err } defer file.Close() decoder := gob.NewDecoder(file) err = decoder.Decode(&session) if err != nil { return session, err } return session, nil }

func writeSession(session whatsapp.Session) error { file, err := os.Create(os.TempDir() + "/whatsappSession.gob") if err != nil { return err } defer file.Close() encoder := gob.NewEncoder(file) err = encoder.Encode(session) if err != nil { return err } return nil } ` image image

rhsobr commented 3 years ago

Only audio/ogg; codecs=opus can be sent as ptt.

https://github.com/Rhymen/go-whatsapp/issues/415#issuecomment-661291130 https://github.com/Rhymen/go-whatsapp/issues/69#issuecomment-612374435