Hi there,
I’m creating a workflow to upload images from Telegram to Google Drive. I’ve configured it so that each image should be named on Drive using the caption from the Telegram message.
When I send multiple images simultaneously with one caption, only the first image gets named correctly with the caption. The subsequent images default to a generic name (like “foto”) instead of using the shared caption.
How can I ensure that all images in a Telegram media group share the same caption for naming purposes? Any suggestions on how to properly handle Telegram media groups in n8n would be greatly appreciated!
In the Telegram Trigger node, enable Return All Data. This will ensure that metadata such as media_group_id and caption are present.
Insert a Code node (the new Function), placing it after the Trigger and before the Google Drive node.
Use the following code:
// Temporary in-memory cache to store captions by media_group_id
// Important: This is reset with each individual execution of the flow
const itemsOut = [];
for (const item of items) {
const groupId = item.json.message?.media_group_id;
const caption = item.json.message?.caption;
// We save the caption by media_group_id in a static variable
if (groupId) {
if (!global.store) global.store = {};
if (!global.store[groupId] && caption) {
global.store[groupId] = caption;
}
item.json.savedCaption = global.store[groupId] || "Untitled";
} else {
item.json.savedCaption = caption || "Untitled";
}
itemsOut.push(item);
}
return itemsOut;
This ensures that even if subsequent images don’t have the caption, they will inherit the first one thanks to the use of media_group_id.
In the Google Drive node
In the File Name field, use: ={{ $json.savedCaption }}
Technical limitation
This technique will not persist the caption between flow executions because global.store only lives for a single execution. For flows that process messages one by one (rather than as a batch), this technique could fail. In that case, additional logic with external storage (such as Redis or a temporary collection in n8n) is required.
Hi there!
I’m using the Telegram Trigger node.
I see just one caption and when the media are upload on drive, just the first one has the caption as name