go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API

Home Page:https://go-telegram-bot-api.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help with editMessageMedia

fak3user opened this issue · comments

          > Thanks!

Actually, as already said, my first solution doesn't allow changing the Image.

It seems that to change the image you must send an editMessageMedia command https://core.telegram.org/bots/api#editmessagemedia. This command seems to be composed through the EditMessageMediaConfig structure, here a totally UNTESTED code:

	msg := tgbotapi.EditMessageMediaConfig{
		BaseEdit: tgbotapi.BaseEdit{
			ChatID: 1,
			MessageID: 3,
		},
		Media: image,
	}
	if _, err = bot.Send(msg); err != nil {
		panic(err)
	}

Originally posted by @cmaglie in #512 (comment)

Hi!
Can anybody help with this method?

I've tried this:

// Create a FileBytes from the temporary file
	fileBytes := tgbotapi.FileBytes{
		Name:  fileName,
		Bytes: imageBytes,
	}

	baseInputMedia := tgbotapi.BaseInputMedia{
		Type:      "photo", // Set the desired media type
		Media:     fileBytes,
		ParseMode: "markdown", // Set the desired parse mode
	}

	// Create an EditMessageMediaConfig to update the message
	editMessageConfig := tgbotapi.EditMessageMediaConfig{
		BaseEdit: tgbotapi.BaseEdit{
			ChatID:    chatID,
			MessageID: messageID,
		},
		Media: tgbotapi.InputMediaPhoto{
			BaseInputMedia: baseInputMedia,
		},
	}

	// Edit the existing message with the updated photo and inline keyboard
	_, err = bot.Send(&editMessageConfig)
	if err != nil {
		return fmt.Errorf("error editing message: %v", err)
	}

It should work if the original message contained Media. Apparently editing a non-Media message to Media is not allowed so be advised.