How to add thumbnail image when sending audio files via Telegram Bot API

I’m working on a Telegram bot that sends audio files with thumbnails. I extract cover art from MP3 files using a music metadata library and convert it to a BufferedImage. Then I resize the image to meet Telegram’s requirements (320x320 pixels, JPEG format, under 200KB).

Here’s my image resizing function:

fun BufferedImage.scaleToSize(newWidth: Int, newHeight: Int): BufferedImage {
    val scaledImg = BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB)
    val g2d: Graphics2D = scaledImg.createGraphics()
    g2d.drawImage(this, 0, 0, newWidth, newHeight, null)
    g2d.dispose()
    return scaledImg
}

I save the resized image as JPEG: ImageIO.write(coverArt.scaleToSize(320, 320), "jpeg", thumbFile)

Then I send the audio with thumbnail:

bot.sendAudio(
    chatId = ChatId(RawChatId(channelId)),
    audio = musicFile.asMultipartFile(),
    title = songTitle,
    performer = artistName,
    duration = trackDuration.toLong(),
    disableNotification = true,
    thumb = thumbFile.asMultipartFile()
)

The audio message sends successfully but the thumbnail doesn’t show up. The image file size is definitely under 200KB. What could be causing this issue?

check if your thumbFile actually exists after writing - I’ve seen file writes fail silently. Also, telegram thumbnails must be square (1:1 ratio). Even if your dimensions are 320x320, it’ll get rejected if the scaling messed up the aspect ratio.

I had the same thumbnail issues when building an audio sharing bot. The problem’s probably how you’re handling the image data stream. Try calling thumbFile.createNewFile() and make sure you’re properly closing the file after writing the JPEG. Another issue could be multipart boundary handling. Skip asMultipartFile() and use TelegramBotAPI.RequestBody.Companion.create() with explicit MediaType instead. The API sometimes fails silently when content-type headers aren’t set right. Here’s something that tripped me up - some MP3 metadata has progressive JPEG thumbnails that Telegram won’t accept. Force baseline JPEG encoding by setting ImageWriteParam compression type to baseline when creating your JPEG output.

Had this exact problem building my music bot a few months ago. Your resizing code looks fine - the issue is Telegram’s API being picky about thumbnail formatting.

Here’s what fixed it for me: Make sure your BufferedImage converts to RGB before writing to JPEG. More importantly, ditch the basic ImageIO.write method and use ImageWriter with compression quality around 0.8f instead.

Also check that asMultipartFile() sets the MIME type as ‘image/jpeg’. Another gotcha - some album art comes in CMYK color space and Telegram silently rejects it. Converting to sRGB before resizing solved most of my thumbnail headaches.