Getting 'wrong file identifier/HTTP URL specified' error when sending images via URL in Telegram bot

I’m working on a Telegram bot that sends images using URLs, but I keep running into a weird issue. Some images work perfectly fine, while others throw this error:

{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}

Here’s what’s confusing me. I tested with two different image URLs from the same domain. The first one works without any problems:

https://api.telegram.org/bot<TOKEN>/sendPhoto?chat_id=<ID>&photo=https%3A%2F%2Fimages.example.com%2Fgallery%2F789123456%2Fsize%253Dlarge%2Fv1%3Fformat%3Djpeg%26hash%3Da1b2c3d4e5f6789012345678901234567890abcdef1234567890123456789012

But this second URL fails every time:

https://api.telegram.org/bot<TOKEN>/sendPhoto?chat_id=<ID>&photo=https%3A%2F%2Fimages.example.com%2Fgallery%2F987654321%2Fsize%253Dmedium%2Fv1%3Fformat%3Djpeg%26hash%3Dz9y8x7w6v5u4321098765432109876543210fedcba0987654321098765432109

I checked everything I could think of:

  • Both URLs load the images fine in browser
  • Both are JPEG files with correct MIME types
  • File sizes are under the 10MB limit
  • URL encoding looks proper

What could be causing this inconsistent behavior? Is there something about certain URL patterns that Telegram doesn’t like?

Had the same issue last month. It’s usually not the URL structure - it’s how Telegram’s servers cache and validate endpoints. Their image fetcher gets confused by certain hash values or parameter combos that worked fine before. Happens way more with dynamic image URLs that have messy query strings. Two things fixed it for me: add a timestamp parameter so Telegram treats it as a fresh request, or use InputFile with a downloaded buffer instead of direct URLs for the problematic images. The inconsistency you’re seeing? That’s just typical Telegram API behavior with complex image URLs.

I’ve hit this exact problem before. It’s usually special characters in the URL that mess with Telegram’s validation, even when they’re properly encoded. That hash parameter in your failing URL has character sequences Telegram’s backend randomly rejects. URLs with repeated characters or certain hex patterns in query parameters trigger this error way more often. Quick fix: download the problematic image first, then upload it as a file instead of the direct URL. Or proxy it through your own server to strip the messy parameters while serving the same image.

telegram’s finicky with URL lengths. that second url looks way longer and might be hitting some undocumented limit. try shortening the hash parameter or throw it through a url shortener to see if that fixes it.