Combine all items into one string for Telegram

Describe the problem/error/question

I have the following output from an Item List and want to create a string with each item per paragraph to telegram send it.

[
  {
    "id": "399366",
    "ratings_imdb_value": "6.7",
    "ratings_rottenTomatoes_value": "49",
    "appended_title": [
      "Movie Title 1"
    ]
  },
  {
    "id": "747188",
    "ratings_imdb_value": "6.6",
    "ratings_rottenTomatoes_value": "75",
    "appended_title": [
      "Movie Title 2"
    ]
  }
]

And get as output something like:

Movies List:

  • <Movie Title 1> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>
  • <Movie Title 2> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>

I have been trying with code but without any success. Is there an easier way to make this on n8n?

Something like this?

3 Likes

Hey @Auto.Matron

this is the code you need:

for (const item of $input.all()) {
  item.json.message = `${item.json.appended_title[0]}  - IMDB: ${item.json.ratings_imdb_value} | RP: ${item.json.ratings_rottenTomatoes_value}`;
}

return $input.all();

After the code node you find the message in the message column.

Here you see the whole workflow:

Cheers.

Thanks @nico-kow,

I was hoping to have one string as output with:

Movies List:

  • <Movie Title 1> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>
  • <Movie Title 2> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>

Or, converting to machine language :smiley: :

Movie List:\n\n<Movie Title 1> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>\n<Movie Title 2> - IMDB: <“ratings_imdb_value”> | RP: <“ratings_rottenTomatoes_value”>

I was trying yo do that with your code, but I am still struggling with that.

Hey @Auto.Matron,

Sorry my bad…

// Generate message per movie
for (const item of $input.all()) {
  item.json.message = `${item.json.appended_title[0]}  - IMDB: ${item.json.ratings_imdb_value} | RP: ${item.json.ratings_rottenTomatoes_value}`;
}

// Put everything together
const wholeMessage = `Movie List:

${$input.all().map(item => `- ${item.json.message}`).join('\n')}`;

return [
  {
    'message': wholeMessage
  }
]

This code schould actually work. But it seems that n8n may not render the string correctly in the output area. Try to send it to telegram. maybe it is working as expected.

I tried with slack and it works as expected.

Cheers.

btw: I think the solution of @donniedarkowindsalt should also be working and is probably better in your situation of not beeing used to code :slight_smile:

1 Like

Yeah, the solution of @donniedarkowindsalt is very good too. I will use it because it’s easier and very simple to use. Then I will save your code too for future use.

Thank you both for the fast and amazing support :slight_smile:

1 Like

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