I’m working on a telegram bot using the python-telegram-bot library and I need some help with a specific feature.
Basically, I want my bot to handle forwarded messages that contain both images and text. When someone forwards a message with a picture to my bot, I need the bot to ignore the image part and only extract and return the text content.
I’ve been looking through the documentation but couldn’t find a clear example of how to do this. I know about filter combinations like message_handler = MessageHandler(Filters.forwarded & Filters.photo, my_callback) but I’m not sure how to actually separate the text from the image in the callback function.
Has anyone dealt with this before? Any code examples or suggestions would be really helpful.
had the same prob last month. the caption property works great, but make sure to check update.message.forward_from to confirm it’s a forwarded msg. sometimes peeps forward without text, so i added a fallback like “no text found.” helped with my edge cases.
Manual handling works but becomes a nightmare once you’re processing thousands of messages. Been there with a content moderation bot I built.
I switched to automating everything with Latenode. Set up a webhook for all Telegram updates and built a workflow that extracts text from any message - forwards, replies, media captions, whatever.
Latenode handles the branching logic automatically. One node checks message type, another grabs text or captions, third handles empty cases. No more crashes from missing properties or weird forward formats.
I connected it to a database to track forwarded content patterns, which helped me refine the extraction rules. Took 30 minutes to set up and saved weeks of debugging.
Much more reliable than hardcoding checks for every message structure. You can add new processing steps later without touching your bot code.
Heads up - forwarded messages can be tricky depending on what was originally sent. With photo forwards, the text might not be in the caption field if the sender added text separately from the image. Check both update.message.text and update.message.caption to catch everything. Some users structure image + text messages differently even within the same message. I learned this the hard way when my bot completely missed text that wasn’t technically a caption. I’d suggest checking both properties and combining any text you find - that way you won’t miss anything from forwarded content.
Been working with Telegram bots for a while - this is pretty straightforward once you get the message structure. For forwarded messages with photos, use update.message.caption instead of update.message.text. The image and text are separate in Telegram’s message object. Just check if update.message.caption exists in your callback function. Try text_content = update.message.caption if update.message.caption else "". Telegram already separates the photo and caption for you, so no manual work needed. Don’t forget to handle cases with no caption text or your bot will crash on image-only forwards.