go-whatsapp
go-whatsapp copied to clipboard
Error receive media with receiveMessages/main.go
Hi,
Im trying to run the example for receive media message on example/receiveMessages/main.go
by uncomment this function:
func (*waHandler) HandleTextMessage(message whatsapp.TextMessage) {
fmt.Printf("%v %v %v %v\n\t%v\n", message.Info.Timestamp, message.Info.Id, message.Info.RemoteJid, message.ContextInfo.QuotedMessageID, message.Text)
}
//Example for media handling. Video, Audio, Document are also possible in the same way
func (h *waHandler) HandleImageMessage(message whatsapp.ImageMessage) {
data, err := message.Download()
if err != nil {
if err != whatsapp.ErrMediaDownloadFailedWith410 && err != whatsapp.ErrMediaDownloadFailedWith404 {
return
}
if _, err = h.c.LoadMediaInfo(message.Info.RemoteJid, message.Info.Id, strconv.FormatBool(message.Info.FromMe)); err == nil {
data, err = message.Download()
if err != nil {
return
}
}
}
filename := fmt.Sprintf("%v/%v.%v", os.TempDir(), message.Info.Id, strings.Split(message.Type, "/")[1])
file, err := os.Create(filename)
defer file.Close()
if err != nil {
return
}
_, err = file.Write(data)
if err != nil {
return
}
log.Printf("%v %v\n\timage received, saved at:%v\n", message.Info.Timestamp, message.Info.RemoteJid, filename)
}
But I got the following error :
$ go run receiveMessages/main.go
# command-line-arguments
./main.go:46:13: undefined: whatsapp.ErrMediaDownloadFailedWith410
./main.go:46:62: undefined: whatsapp.ErrMediaDownloadFailedWith404
./main.go:49:74: undefined: strconv
./main.go:57:69: undefined: strings
Please any advice?
@bryanjack I just comment in every handler with this constant like this:
// if err != whatsapp.ErrMediaDownloadFailedWith410 && err != whatsapp.ErrMediaDownloadFailedWith404 {
return "", err
// }
I tested after this and everything work fine even with Downloads! I'll wait for a better idea...
@bryanjack I just comment in every handler with this constant like this:
// if err != whatsapp.ErrMediaDownloadFailedWith410 && err != whatsapp.ErrMediaDownloadFailedWith404 { return "", err // }
I tested after this and everything work fine even with Downloads! I'll wait for a better idea...
Thanks for your response. If I comment that part, I still got the following error:
go run receiveMessages/main.go
# command-line-arguments
receiveMessages/main.go:49:74: undefined: strconv
receiveMessages/main.go:57:69: undefined: strings
These are Go related errors. strconv
and strings
are standard library packages and should be imported before being referenced. I would advise setting up your code editor for Go so these imports are automatically handled.
These are Go related errors.
strconv
andstrings
are standard library packages and should be imported before being referenced. I would advise setting up your code editor for Go so these imports are automatically handled.
Thanks for your reply,
Do you have any idea about this error:
undefined: whatsapp.ErrMediaDownloadFailedWith410
whatsapp
refers to this library, go-whatsapp
. You should import it by adding github.com/Rhymen/go-whatsapp
to your imports. This reference should also be automatically resolved by your editor tools when properly configured :+1:
go-whatsapp
. You should import it by addinggithub.com/Rhymen/go-whatsapp
to your imports. This reference should also be automatically resolved by your editor tools when properly configured 👍
I've added the import github.com/Rhymen/go-whatsapp
and I'm using vcode with go-lang extension but it still give me error :
.\main.go:48:13: undefined: whatsapp.ErrMediaDownloadFailedWith410
.\main.go:48:62: undefined: whatsapp.ErrMediaDownloadFailedWith404
Hmmmm, after some testing I noticed some interesting points:
- The
go.mod
file inreceiveMessages
resolves thewhatsapp
dependency locally (and relatively to the example work directory). It means that one should clone the whole repository and keep the directory structure while running the examples. - If you are already running the examples from inside the repository structure, your repository might be outdated. Try pulling the recent changes.
Also, if none of the above works, I would recommend copying the example code in a new project and getting the go-whatsapp dependency with go get
. Let me know if any of the above works! :wink: