How can I save attachments #174
-
How can I save incoming message if it is image/document/audio/video? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Here is the code to save image/audio/video/documents received at WhatsApp: case evt.Message.ImageMessage != nil:
img := evt.Message.GetImageMessage()
if img != nil {
data, err := global.Cli.Download(img)
if err != nil {
log.Printf("Failed to download image: %v", err)
return
}
exts, _ := mime.ExtensionsByType(img.GetMimetype())
path := fmt.Sprintf("./Downloads/Image/%s-%s%s", sender, evt.Info.ID, exts[0])
err = os.WriteFile(path, data, 0600)
if err != nil {
log.Printf("Failed to save image: %v", err)
return
}
log.Printf("Saved image in message to %s", path)
}
case evt.Message.AudioMessage != nil:
audio := evt.Message.GetAudioMessage()
if audio != nil {
data, err := global.Cli.Download(audio)
if err != nil {
log.Printf("Failed to download audio: %v", err)
return
}
exts, _ := mime.ExtensionsByType(audio.GetMimetype())
path := fmt.Sprintf("./Downloads/Audio/%s-%s%s", sender, evt.Info.ID, exts[0])
err = os.WriteFile(path, data, 0600)
if err != nil {
log.Printf("Failed to save audio: %v", err)
return
}
log.Printf("Saved audio in message to %s", path)
}
case evt.Message.VideoMessage != nil:
video := evt.Message.GetVideoMessage()
if video != nil {
data, err := global.Cli.Download(video)
if err != nil {
log.Printf("Failed to download video: %v", err)
return
}
exts, _ := mime.ExtensionsByType(video.GetMimetype())
path := fmt.Sprintf("./Downloads/Video/%s-%s%s", sender, evt.Info.ID, exts[0])
err = os.WriteFile(path, data, 0600)
if err != nil {
log.Printf("Failed to save video: %v", err)
return
}
log.Printf("Saved video in message to %s", path)
}
case evt.Message.DocumentMessage != nil:
document := evt.Message.GetDocumentMessage()
if document != nil {
data, err := global.Cli.Download(document)
if err != nil {
log.Printf("Failed to download audio: %v", err)
return
}
exts, _ := mime.ExtensionsByType(document.GetMimetype())
path := fmt.Sprintf("./Downloads/Documents/%s-%s%s", sender, evt.Info.ID, exts[0])
err = os.WriteFile(path, data, 0600)
if err != nil {
log.Printf("Failed to save document: %v", err)
return
}
log.Printf("Saved document in message to %s", path)
} |
Beta Was this translation helpful? Give feedback.
-
Sorry for the newbie question, but I cannot figure out what global.Cli in global.Cli.Download(audio) referees to. Can you please tell me, what global and or global.Cli is connected to? Currently I try to figure out, how to download and save WhatsApp-Messages to disc. And I think the key to it is hidden in the above snippet. Thx, |
Beta Was this translation helpful? Give feedback.
-
Is it possible to get the caption (text that is displayed under the image in WhatsApp) that the user typed before upload? Maybe somewhere in here? I looked in |
Beta Was this translation helpful? Give feedback.
Here is the code to save image/audio/video/documents received at WhatsApp: