Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image message unable to preview #204

Closed
lancezhaozai opened this issue Jul 16, 2022 · 7 comments
Closed

Image message unable to preview #204

lancezhaozai opened this issue Jul 16, 2022 · 7 comments

Comments

@lancezhaozai
Copy link

lancezhaozai commented Jul 16, 2022

I followed the documentation and perform a simple image upload but the image (bottom message) sent was unable to preview like we sending the image manually (top message)

NOTE: the receiver has no saved contact of the sender
my code to reproduce the issue:

        jpegImageFile, jpegErr := os.Open("/home/user/Desktop/beaches.jpeg")
	if jpegErr != nil{
		// Handle error
	}
	defer jpegImageFile.Close()

	jpegFileinfo, _ := jpegImageFile.Stat()
	var jpegSize int64 = jpegFileinfo.Size()
	jpegBytes := make([]byte, jpegSize)

	jpegBuffer := bufio.NewReader(jpegImageFile)
	_, jpegErr = jpegBuffer.Read(jpegBytes)

        resp, err := client.Upload(context.Background(), jpegBytes, whatsmeow.MediaImage)
	// handle error

        my_num := ""

	if err == nil {

		imageMsg := &waProto.ImageMessage{
				Caption:  proto.String("Hello, world!"),
				Mimetype: proto.String("image/jpeg"), // replace this with the actual mime type
				// you can also optionally add other fields like ContextInfo and JpegThumbnail here
				ThumbnailDirectPath: &resp.DirectPath,
				ThumbnailSha256: resp.FileSHA256,
				ThumbnailEncSha256: resp.FileEncSHA256,
				JpegThumbnail: jpegBytes,
				

				Url:           &resp.URL,
				DirectPath:    &resp.DirectPath,
				MediaKey:      resp.MediaKey,
				FileEncSha256: resp.FileEncSHA256,
				FileSha256:    resp.FileSHA256,
				FileLength:    &resp.FileLength,
				
		}
		_, err = client.SendMessage(context.Background(), types.NewJID(my_num, "s.whatsapp.net"), "", &waProto.Message{
				ImageMessage: imageMsg,
				// ExtendedTextMessage: &waProto.ExtendedTextMessage{
					
				// 	ThumbnailDirectPath: &resp.DirectPath,
				// 	ThumbnailSha256: resp.FileSHA256,
				// 	ThumbnailEncSha256: resp.FileEncSHA256,
				// 	JpegThumbnail: jpegBytes,
				// },
		})
	}

@mamur-rezeki
Copy link

Maybe you need to upload Thumbnail like you upload the image (but with Media thumbnail option) and attaching the response properties to the thumbnail fields.

@lancezhaozai
Copy link
Author

Maybe you need to upload Thumbnail like you upload the image (but with Media thumbnail option) and attaching the response properties to the thumbnail fields.

tried with whatsmeow.MediaLinkThumbnail, no luck, I found out that it maybe the reason of unsaved contact, but it sometime works on manually sending

@mamur-rezeki
Copy link

So i have testing a case, maybe on mobile App they limiting the thumbnail size to 300px or another size.

I'm sorry for my wrong for "MediaLinkThumbnail" that actually didn't work. 😆

Size : for image bytes
W, H : Max image target size in pixel

I've been testing it so far :
Screenshot_20220719-094917_WhatsApp

@lancezhaozai
Copy link
Author

lancezhaozai commented Jul 19, 2022

could you try out my image file? because I tried resize my image and my code is not working as yours, I use the same image bytes for image and its thumbnail, what am I doing it wrong?

size: 12KB, W: 300, H: 245

Update 1: I also tried different bytes for actual image 1MB with 12KB thumbnail upload, and also do both 245 x 245 pixels, maybe my code is incorrect?

Update 2: Could it be the URL? seems like encrypted data I check the DirectPath and URL of the image, how can we be sure that image is upload successfully? but once clicked download button on there the image is viewable, so the upload should be completed?

jpegImageFile, jpegErr := os.Open("/home/player/Desktop/testbeaches2.jpeg")
	if jpegErr != nil{
		// Handle error
	}
	defer jpegImageFile.Close()

	jpegFileinfo, _ := jpegImageFile.Stat()
	var jpegSize int64 = jpegFileinfo.Size()
	jpegBytes := make([]byte, jpegSize)

	jpegBuffer := bufio.NewReader(jpegImageFile)
	_, jpegErr = jpegBuffer.Read(jpegBytes)

	resp, err := client.Upload(context.Background(), jpegBytes, whatsmeow.MediaImage)


imageMsg := &waProto.ImageMessage{
		Caption:  proto.String(msg),
		Mimetype: proto.String("image/jpeg"), // replace this with the actual mime type
		// you can also optionally add other fields like ContextInfo and JpegThumbnail here
		ThumbnailDirectPath: &resp.DirectPath,
		ThumbnailSha256: resp.FileSHA256,
		ThumbnailEncSha256: resp.FileEncSHA256,
		JpegThumbnail: imgBytes,
		

		Url:           &resp.URL,
		DirectPath:    &resp.DirectPath,
		MediaKey:      resp.MediaKey,
		FileEncSha256: resp.FileEncSHA256,
		FileSha256:    resp.FileSHA256,
		FileLength:    &resp.FileLength,
		
	}

@tulir
Copy link
Owner

tulir commented Jul 19, 2022

JpegThumbnail is meant to be very small, 72x72 px at most.

@lancezhaozai
Copy link
Author

JpegThumbnail is meant to be very small, 72x72 px at most.

yes with 72x72 px, the preview is able to view now,

// Use thumbnail upload response
ThumbnailDirectPath: &thumbResp.DirectPath,
ThumbnailSha256: thumbResp.FileSHA256,
ThumbnailEncSha256: thumbResp.FileEncSHA256,

JpegThumbnail: imgBytes,   <<< use thumbnail image bytes

// Use image upload response
DirectPath:    &resp.DirectPath,
FileEncSha256: resp.FileEncSHA256,
FileSha256:    resp.FileSHA256,

@david-botelho-mariano
Copy link

david-botelho-mariano commented Sep 14, 2023

Worked for me, thumbnail configuration:

// open "test.jpg"
file, err := os.Open("test.jpg")
if err != nil {
	fmt.Println(err)
}

// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
	fmt.Println(err)
}
file.Close()

// resize to width 72 using Lanczos resampling
// and preserve aspect ratio
m := resize.Thumbnail(72, 72, img, resize.Lanczos3)

out, err := os.Create("test_resized.jpg")
if err != nil {
	fmt.Println(err)
}
defer out.Close()

// write new image to file
jpeg.Encode(out, m, nil)

thumbnailBytes, err := os.ReadFile("test_resized.jpg")
if err != nil {
	log.Errorf("Failed to read %s: %v", thumbnailPath, err)
	return
}	

Video configuration:

recipient, ok := parseJID("120363172773092731@g.us")
if !ok {
	return
}
data, err := os.ReadFile("test-video.mp4")
if err != nil {
	log.Errorf("Failed to read %s: %v", arquivoPath, err)
	return
}
uploaded, err := cli.Upload(context.Background(), data, whatsmeow.MediaVideo)
if err != nil {
	log.Errorf("Failed to upload file: %v", err)
	return
}
msg := &waProto.Message{VideoMessage: &waProto.VideoMessage{
	Url:           proto.String(uploaded.URL),
	DirectPath:    proto.String(uploaded.DirectPath),
	MediaKey:      uploaded.MediaKey,
	Mimetype:      proto.String(http.DetectContentType(data)),
	FileEncSha256: uploaded.FileEncSHA256,
	FileSha256:    uploaded.FileSHA256,
	FileLength:    proto.Uint64(uint64(len(data))),	
	Caption:       proto.String("test caption"),

	JpegThumbnail: thumbnailBytes,
}}
resp, err := cli.SendMessage(context.Background(), recipient, msg)
if err != nil {
	log.Errorf("Error sending video message: %v", err)
} else {
	log.Infof("Video message sent (server timestamp: %s)", resp.Timestamp)
}

Configure the resize library:

go get github.com/nfnt/resize

import (
	"github.com/nfnt/resize"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants