Need help with "sendmediagroup" telegram

hey, I looked at this topic.

I integrated his solution into my workflow, everything worked, photos are sent by album, but the data is duplicated and two messages with the same data arrive.


  • n8n version:1.102.3
  • Database ():Airtable
  • n8n EXECUTIONS_PROCESS setting (default: own, main):default
  • Running n8n via (Docker, npm, n8n cloud, desktop app):n8n cloud
  • Operating system:win11

The most reliable solution, used by several users successfully:

Don’t use the “Send Media Group” node.
Use an HTTP Request to the Telegram API: Endpoint: sendMediaGroup, JSON with a variable media array.
You can iterate over the media_ids retrieved from the database and dynamically build the array.

This approach allows you to send any combination of photos/videos, without duplicates and with proper programmatic logic.

You should also build a dynamic array, with a code node such as:

return [
{
json: {
chat_id: ,
media: items.map(item => ({
type: item.json.media_type, // “photo” or “video”
media: item.json.media_id
}))
}
}
];

Then pass this. This JSON will be passed to the HTTP Request node.

Send with HTTP Request
Configure:
URL: https://api.telegram.org/bot<YOUR_TOKEN>/sendMediaGroup
Method: POST
Body: Use the generated JSON as JSON/RAW Body
This allows variable albums to be sent correctly.

thanks for the reply, I’m already using an http request from senmediagroup. It turns out that each attached copper to the message initializes a new startup of the workflow, and each node is executed several times, maybe I made a mistake somewhere during the setup?

// items[] —
const media = items.map(i => ({
  type:    i.json.fields.type,
  media:   i.json.fields.file_id,
  caption: i.json.fields.caption || ''
}));

const uniq = [];
const seen = new Set();
for (const m of media) {
  if (!seen.has(m.media)) {
    uniq.push(m);
    seen.add(m.media);
  }
}
return [{
  json: {
    media: JSON.stringify(uniq), 
    ids:   items.map(i => i.json.id)
  }
}];

this is my code for build array

items already deprecated since in the new version, you must use $input.all() instead.
so replace the 2nd line from items.map to $input.all().map() vice versa for the ids at line 19, you need to change it to $input.all().map(i=>i.json.id)

thanks for the reply, I did everything as you wrote, but the media was duplicated anyway.


You must dynamically build a JSON array with the media to send:

return [
{
json: {
chat_id: ...,
media: items.map(i => ({
type: i.json.media_type, // “photo” or “video”
media: i.json.media_id,
caption: i.json.caption || ''
}))
}
}
];

Then send this payload using a configured HTTP Request node:

This allows you to send albums with any combination or number of files without duplication or fixed limits

1 Like


this code doesn’t seem to work for me

here is the one i made:

const TARGET_CHAT_ID = '-1002865103306';   


const media = $input.all().map(i => {
  const j = i.json;

 
  const type =
    j.type ? j.type :
    j.mimeType?.startsWith('video') ? 'video' :
    'photo';

  return {
    type,              
    media:   j.file_id, 
    caption: j.caption || ''
  };
});


const uniq = [];
const seen = new Set();
for (const m of media) {
  if (!seen.has(m.media)) {
    uniq.push(m);
    seen.add(m.media);
  }
}

const recIds = $input.all().map(i => i.json.id);

return [
  {
    json: {
      chat_id: TARGET_CHAT_ID,
      media:   JSON.stringify(uniq),
      ids:     recIds 
    }
  }
];

Still sends duplicate files to target chat
Maybe I’m doing something wrong?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.