Multiple images in telegram

Describe the issue/error/question

Hi,

I’m creating a workflow that sends input files to telegram. In case of images I can have multiple input. I know that I can set multiple files to send in UI. But how to deal when I don’t know how many files will appear in input and I need to send them as album in single message?
Kindly advise if there is a way to do this without creating multiple telegram nodes that will only have different number of preset Media files?

Thank you in advance.

Hi @kygechuk, welcome to the community :tada:

I assume you are using the Send Photo operation of the Telegram node here? A possible approach would be first take all binary items you have and split them into individual items using a snippet like this one in a Function node before your Telegram node:

The Telegram node should then run for each individual item (and thus sending all photos one by one, regardless of how many there are in total).

1 Like

Dear MutedJam,

Thank you very much for your reply!

OMG! This link to code snippets is AWESOME! I was as a blindman seeking at google for pieces of information like this :slight_smile:

Yes, you’re right I’m asking about Telegram node. But how node will understand how many individual items I pass to it if I need to preset all of them manually in the node? And if I just split and get 2 or 5 or 7 and node will send them as separate images, not as an album of items.

Thank you!

1 Like

Almost all n8n nodes will run once for each input item (the exceptions would be listed here). The example snippet (in the Split binary data into individual items section on the linked page) will take all binary elements on the first item and will convert them into individual items, each with a single binary property named data.

Meaning if you pass a single item with 5 binary files to the Telegram node, it would only once (and only send the one binary data element you have specified). But if you pass 5 items with 1 binary data element each to the Telegram node, it would run 5 times (sending one binary data element on each run, so all 5 in total).

This is assuming you are using binary data in the node (which is my understanding from reading your description):
image

I hope this makes sense! If this doesn’t quite apply to your case, could you share the example data the Telegram node receives in your workflow as well as the workflow itself?

1 Like

Hi,

Let me show simple example.

Here is the code where I have 2 different inputs. In one case I have 1 image to send in another - 2. And Telegram node fails because in has no input for 2nd image in 2nd item.

My question was - how to hande situation when I don’t know how many files to send I have from input?

And other problem - their type - they can be images or videos. So even I create 10 telegram nodes for sending 1-10 images, and another 10 nodes to send 1-10 videos, I’ll face a problem with items that have both images and videos in a random order,

{
  "name": "Telegram multiple images",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      text: \"message1\",\n      attachments: [\"https://source.unsplash.com/random/300x300/?dog\",\"https://source.unsplash.com/random/300x300/?cat\"],\n      attachment1: \"https://source.unsplash.com/random/300x300/?dog\",\n      attachment2: \"https://source.unsplash.com/random/300x300/?cat\"\n    }\n  },\n  {\n    json: {\n      text: \"message2\",\n      attachments: [\"https://source.unsplash.com/random/300x300/?bird\"],\n      attachment1: \"https://source.unsplash.com/random/300x300/?bird\"\n    }\n  }\n];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "operation": "sendMediaGroup",
        "chatId": "12345678",
        "media": {
          "media": [
            {
              "media": "={{$json[\"attachment1\"]}}",
              "additionalFields": {
                "caption": "={{$json[\"text\"]}}"
              }
            },
            {
              "media": "={{$json[\"attachment2\"]}}",
              "additionalFields": {}
            }
          ]
        },
        "additionalFields": {}
      },
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "credentials": {
        "telegramApi": {
          "id": "1",
          "name": "AppDeals bot"
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 9
}