go-whatsapp icon indicating copy to clipboard operation
go-whatsapp copied to clipboard

Error receive media with receiveMessages/main.go

Open bryanjack opened this issue 4 years ago • 7 comments

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 avatar Dec 08 '20 02:12 bryanjack

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

albertord84 avatar Dec 09 '20 21:12 albertord84

@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

bryanjack avatar Dec 10 '20 12:12 bryanjack

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.

abnt713 avatar Dec 16 '20 15:12 abnt713

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.

Thanks for your reply, Do you have any idea about this error: undefined: whatsapp.ErrMediaDownloadFailedWith410

bryanjack avatar Dec 17 '20 01:12 bryanjack

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:

abnt713 avatar Dec 18 '20 11:12 abnt713

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 👍

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

bryanjack avatar Dec 22 '20 13:12 bryanjack

Hmmmm, after some testing I noticed some interesting points:

  • The go.mod file in receiveMessages resolves the whatsapp 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:

abnt713 avatar Jan 06 '21 13:01 abnt713