Sending group of photos with telegram bot

I want to send group of photos via using telegram sender. I am using "node-red-contrib-telegrambot"

My nodes looks like this

the first function returns the path of the photos like this; (the photos are located on my server locally)

//return path of photos
let numberpage = 609;

msg.payload = [
    `/data/photos/${numberpage}.jpg`,
    `/data/photos/${numberpage + 1}.jpg`,
];
return [msg];

Then i split them and i read these files and join together and i create a buffer like this which contains two images.

//debug 7 looks like this
msg.payload : array[2]
array[2]
0: buffer[1892013]
1: buffer[1651348]

And then i take this two array as content for sending as a message. It looks like this;

//content for message function node
var msg = {
    chatId: 9999999, //example
    content: msg.payload, //here should come the 2 images.
    type: "photo"
}
return msg;

But i get this error.

msg : string[20]

"msg.payload is empty"

I think there is a problem with the last function. Because it gives me msg.payload is empty.

Can someone help me? Thanks :slightly_smiling_face:

should be

msg.payload = {
    chatId: 9999999, //example
    content: msg.payload, //here should come the 2 images.
    type: "photo"
}

But if you add all photos to a httpstatic folder you can just us the file uri's Configuration : Node-RED

First try sending one photo

[edit] I have no issues sending single buffer images, using this, you could split your payload and send multiple messages. Not sure if the telegram bot can send group media messages.

msg.payload = {
  caption: msg.topic,
  type : 'photo',
  chatId: chatid,
  content: msg.buffer,
  fileOptions:{
    filename: msg.topic + '.jpg',
    contentType: 'image/jpeg',
  }
}

Oooh, naughty Telegram.

Not a good idea to show everyone what web server/proxy they are using and its version. They are asking to be hacked.

1 Like

I can send one photo its not the problem i can even send more photos but it can not send group of photos at the same time.

My Process:

  1. Generating the File Paths:
  • The first function determines the file paths of the three images dynamically.
  • It constructs an array of file paths based on a variable number_of_pages:
var number_of_pages = 1;

msg.payload = [  
  `/data/photos/${number_of_pages}.jpg`,
  `/data/photos/${number_of_pages+1}.jpg`,
  `/data/photos/${number_of_pages+2}.jpg`
];

return msg;

Processing the Images:

The paths are split, and each file is read individually.
After reading, the images are grouped together again.
Once joined, the msg.payload appears as an array of buffers:

2.2025, 22:10:08 node: debug 2
msg.payload : array[3]
[ buffer[3097142], buffer[1620527], buffer[1667224] ]

Formatting the Telegram Message:

  • A function formats the message to be sent via the Telegram bot.
    It encapsulates the buffer array within an object containing the chat_id, content, and type:
msg.payload = {
    chat_id: 1099927519, //example
    content: msg.payload,
    type: "photo"
};

return msg;

Final node before sending;

msg.payload : Object
object
chat_id: 1099927519 //example
content: array[3]
type: "photo"

The error that i got;

Caught exception in sender node:

Error: EPARSE: Error parsing response: <html>

<head><title>414 Request-URI Too Large</title></head>

<body>

<center><h1>414 Request-URI Too Large</h1></center>

<hr><center>nginx/1.18.0</center>

</body>

</html>
when processing message: 
{"payload":{"content":[{"type":"Buffer","data":[255,216,255,225,31,136,69,120,105,102,0,0,77,77,0,42,0,0,0,8,0,12,1,0,0,3,0,0,0,1,7,181,0,0,1,1,0,3,0,0,0,1,11,4

I also tried "sendMediaGroup" method but it didnt work.

Any suggestions?

So after investigating the examples it is possible, but the example shows a uri not a buffer

msg.payload.type = "mediaGroup";
msg.payload.content = [
    {
        type : "photo",
        media : "path1.jpg",
        caption : "Photo 1"
    },
    {
        type : "photo",
        media : "path2.jpg",
        caption : "Photo 2"
    }
];

You could attempt replacing the uri with a buffer in similar format, it may work.

the solution you referenced is about sending multiple separate messages, not a grouped album of photos in a single message. Like photo1, photo2 etc.. not [photo1,photo2] together. The Telegram Sender node in Node-RED as i read only supports sending individual messages . Because of that I need to use sendMediaGroupfrom Telegram API and i'll use HTTP Request node for that. I'll post my solution here later.

see above.

[edit] Just tested and is possible using a buffer in the format i posted above

msg.payload = {
    type: "mediaGroup",
    chatId: chatid,
    content: [
        {
            type: "photo",
            media: buffer1,
            caption: "Photo 1"
        },
        {
            type: "photo",
            media: buffer2
            caption: "Photo 2"
        }
    ]
}