Skip to content

MessagesSendMultiMedia returns rpc error code 400: MEDIA_INVALID when sending multiple media in a single post #1527

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

Open
asadbekGo opened this issue Feb 22, 2025 · 8 comments
Labels
enhancement New feature or request

Comments

@asadbekGo
Copy link

I am trying to send multiple media files (e.g., images) in a single post using MessagesSendMultiMedia, but I keep getting the error:
rpc error code 400: MEDIA_INVALID

_, err = v1.tgBot.Client.API().MessagesSendMultiMedia(context.Background(), &tg.MessagesSendMultiMediaRequest{
Peer: &tg.InputPeerChat{ChatID: groupId},
MultiMedia: []tg.InputSingleMedia{
{
Media: &tg.InputMediaUploadedPhoto{File: inputFile},
RandomID: rand.Int63(),
Message: post.Text,
},
{
Media: &tg.InputMediaUploadedPhoto{File: inputFile},
RandomID: rand.Int63(),
},
},
})

What I Tried:

  1. Verified that inputFile is correctly uploaded and contains valid ID, Parts, and MD5Checksum.
  2. Ensured &inputFile is correctly assigned (also tried inputFile without pointer).
  3. Tried InputMediaUploadedDocument instead of InputMediaUploadedPhoto (same error).
  4. Attempted adding Flags: 1, but GroupedID is not available in InputSingleMedia.
    Questions:
  5. How can I send multiple media files in a single post instead of separate messages?
  6. Is there a way to properly group media items using MessagesSendMultiMedia?
  7. Does InputSingleMedia support GroupedID, or is there another approach?
    Would appreciate any insights or suggestions. Thanks!
@asadbekGo asadbekGo added the enhancement New feature or request label Feb 22, 2025
@asadbekGo
Copy link
Author

@ernado

@borzovplus
Copy link

borzovplus commented Mar 11, 2025

before using MessagesSendMultiMedia you should upload all files to telegram, please check docs before creating a issue https://core.telegram.org/methods

@asadbekGo
Copy link
Author

@borzovplus I checked this and studied the documentation, but I still face the same problem. Have you tried using multiMedia yourself, if you know, can you please show me?

@borzovplus
Copy link

I can not to show, because I have a lot of abstractions (to much codes).
Try use "github.com/gotd/td/telegram/uploader" to upload Media before call MessagesSendMultiMedia, than create []tg.InputSingleMedia with uploaded medias

@asadbekGo
Copy link
Author

func UploadFile(ctx context.Context, client *telegram.Client, filePath string) (tg.InputFileClass, []byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, nil, fmt.Errorf("error opening file: %w", err)
}
defer file.Close()

fileInfo, err := file.Stat()
if err != nil {
	return nil, nil, fmt.Errorf("error getting file info: %w", err)
}

if fileInfo.Size() > maxFileSize {
	return nil, nil, fmt.Errorf("file size exceeds the limit of %d bytes", maxFileSize)
}

fileBytes, err := io.ReadAll(file)
if err != nil {
	return nil, nil, fmt.Errorf("error reading file: %w", err)
}

upl := uploader.NewUploader(client.API()).WithPartSize(uploader.MaximumPartSize)

f, err := upl.FromPath(context.Background(), filePath)
if err != nil {
	return nil, nil, fmt.Errorf("error creating uploader from path: %w", err)
}

return f, fileBytes, nil

}

@borzovplus Yes, I am using the same uploader package, but still no result.

@shampigeon
Copy link

Have you found a solution? @asadbekGo

@asadbekGo
Copy link
Author

no, I still haven't found a solution. If you know a solution, please help @shampigeon

@asadbekGo
Copy link
Author

please help @ernado

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

No branches or pull requests

3 participants