# Node red sending picture to telegram

**URL:** <https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656>\
**Category:** General\
**Tags:** telegram\
**Created:** [24 January 2021 16:27 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656 "2021-01-24T16:27:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndriesS82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andriess82/32/35842_2.png) [@AndriesS82](https://discourse.nodered.org/u/AndriesS82)\
**Post date:** [24 January 2021 16:27 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/1 "2021-01-24T16:27:24Z")

</div>

Helo,

I'am trying to send a picture from my Dalhua cam to telegram.  
I have look over the forum and found the solution to do that but for one kind of a reason the picture of the cam doens't go to telegram. I try to change the resulution, the size, ... nothing works.  
If i use the same picture that is stored after the snapshot, I open it with the program 'pictures' from microsoft and just save and overwrite the original file then i can send the image with the timestamp... The size of the image is alot bigger after overwriting and is still a PNG file with the same sizes . Can anyone explain me this and having a solution for that?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98c039f9d61cf99c9becc9ccf0d3695fc44f6f63.png)

---

<div class="post-metadata">

**Author:** ![hoolahoous](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hoolahoous/32/16612_2.png) [@hoolahoous](https://discourse.nodered.org/u/hoolahoous)\
**Post date:** [25 January 2021 01:04 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/2 "2021-01-25T01:04:35Z")

</div>

i use function node and node "request" module . this code will automatically send a text message (if msg.photo is NOT set) or a photo message (if msg.photo is set)..

```auto
var request = global.get('request');
const maxRetry = 2; // try 2 times
const delayBeforeRetry = 500; // 30 seconds between each retry

const token = 'XXXX:yyyyyy';

let chatId = '-000000000';
if (msg.chatId) {
    chatId = msg.chatId;
}

let payload = {};

try {
    if (msg.photo) {
        payload = 
            { url : 'https://api.telegram.org/bot' + token + '/sendPhoto',
            maxAttempts: maxRetry,   
            retryDelay: delayBeforeRetry, 
            formData: {
              chat_id : chatId, 
              photo : {
                    value: msg.photo,
                    options: {
                      filename: 'image.jpg',
                      contentType: 'image/jpeg'
                    }
                  },
              caption : msg.payload,
            },
        };
    } else {
        payload = 
            { url : 'https://api.telegram.org/bot' + token + '/sendMessage',
            maxAttempts: maxRetry,   
            retryDelay: delayBeforeRetry, 
            form: {
              chat_id : chatId, 
              text : msg.payload,
              parse_mode : 'HTML', 
              disable_web_page_preview:true,
            },
        };
    }
    
    let responseHandler = (err, httpResponse, body) => {
      if (err) {
        node.error('telegram message failed:' + err, msg);
      }
    }

    request.post(payload, responseHandler);
} catch (err) {
        node.error('telegram message failed:' + err, msg);
}

```

---

<div class="post-metadata">

**Author:** ![AndriesS82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andriess82/32/35842_2.png) [@AndriesS82](https://discourse.nodered.org/u/AndriesS82)\
**Post date:** [25 January 2021 19:06 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/3 "2021-01-25T19:06:22Z")

</div>

Is there a way that you translate it to nodes? This is a bit chinees for me...

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 January 2021 19:25 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/4 "2021-01-25T19:25:47Z")

</div>

In your original example, the issue is likely because you are **NOT** sending a buffer.

Here is a working example...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df4f182fabb83f588322004ad1fd4507fdfff533.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f2bae38da6a4f36e34c87add3d799e389f19a04.jpeg)

To make it work

- import the demo flow
- setup the BOT connection
- send `/picture` to your bot

NOTE: this demo uses [node-red-contrib-telegrambot](https://flows.nodered.org/node/node-red-contrib-telegrambot)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af806c87574aabaa927adc754588d12f05e0faf7.png)

The demo flow...

```auto
[{"id":"4cdb400.5a8f3c","type":"telegram command","z":"5d3f964c.fef898","name":"/picture","command":"/picture","bot":"fa7a9376.321ce","strict":false,"hasresponse":false,"useregex":false,"removeregexcommand":false,"outputs":1,"x":1390,"y":1760,"wires":[["fdbea5e0.730e08"]]},{"id":"fdbea5e0.730e08","type":"http request","z":"5d3f964c.fef898","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"https://picsum.photos/200/300","tls":"","persist":false,"proxy":"","authType":"","x":1410,"y":1840,"wires":[["352469a3.7bb1d6","d33bab75.38ecb8"]]},{"id":"352469a3.7bb1d6","type":"function","z":"5d3f964c.fef898","name":"send picture","func":"var pl = {\n content: msg.payload,\n message: `Hey ${msg.originalMessage.from.first_name}, here is the random piture you requested`,\n type : 'photo',\n chatId: msg.originalMessage.chat.id,\n chat: msg.originalMessage.chat,\n from: msg.originalMessage.from,\n message_id : msg.originalMessage.message_id\n}\n\nmsg.payload = pl;\n\n\n/* type can be one of the following\nphoto\naudio\nvideo\nsticker\nvoice\ndocument\n*/\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1610,"y":1840,"wires":[["b5d7e8fa.e4aa08","b2b89b56.bd20f8"]]},{"id":"b5d7e8fa.e4aa08","type":"telegram sender","z":"5d3f964c.fef898","name":"","bot":"","outputs":1,"x":1850,"y":1840,"wires":[[]]},{"id":"d33bab75.38ecb8","type":"debug","z":"5d3f964c.fef898","name":"The image","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":1880,"wires":[]},{"id":"b2b89b56.bd20f8","type":"debug","z":"5d3f964c.fef898","name":"the telegram msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1630,"y":1880,"wires":[]},{"id":"fa7a9376.321ce","type":"telegram bot","z":"5d3f964c.fef898","botname":"mybot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"","usesocks":false,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

```

---

<div class="post-metadata">

**Author:** ![AndriesS82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andriess82/32/35842_2.png) [@AndriesS82](https://discourse.nodered.org/u/AndriesS82)\
**Post date:** [25 January 2021 20:18 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/5 "2021-01-25T20:18:21Z")

</div>

ok i got this demo working.  
How do I send now my picture that is stored on my NAS to telegram? The request is only working with http://

below my flow

```auto
[{"id":"744085d0.20ac7c","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"cab9916f.919f","type":"api-call-service","z":"744085d0.20ac7c","name":"","server":"29566c30.83fbd4","version":1,"debugenabled":false,"service_domain":"camera","service":"snapshot","entityId":"camera.voordeur_3","data":"{\"filename\": \"/config/www/snap.png\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":740,"y":220,"wires":[["f3de430e.8b75c","49ac734f.e9c70c"]]},{"id":"e1a058be.d08648","type":"inject","z":"744085d0.20ac7c","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":300,"wires":[["cab9916f.919f"]]},{"id":"b0847137.8ad63","type":"looptimer","z":"744085d0.20ac7c","duration":"5","units":"Second","maxloops":"3","maxtimeout":"30","maxtimeoutunits":"Second","name":"","x":420,"y":220,"wires":[["cab9916f.919f"],[]]},{"id":"f3de430e.8b75c","type":"debug","z":"744085d0.20ac7c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":300,"wires":[]},{"id":"49ac734f.e9c70c","type":"function","z":"744085d0.20ac7c","name":"prep msg","func":"msg.filename = '/config/www/snap.png';\nmsg.payload = { \n data: {\n type: 'photo',\n file: '/config/www/snap.png',\n chatId: 12345678,\n caption: 'Snapshot voordeur.' }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":960,"y":220,"wires":[["beb298d9.3a9e88","fc6acbb3.786d08"]]},{"id":"9b622a42.852c28","type":"inject","z":"744085d0.20ac7c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":220,"wires":[["b0847137.8ad63"]]},{"id":"beb298d9.3a9e88","type":"debug","z":"744085d0.20ac7c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1170,"y":300,"wires":[]},{"id":"fc6acbb3.786d08","type":"api-call-service","z":"744085d0.20ac7c","name":"","server":"29566c30.83fbd4","version":1,"debugenabled":true,"service_domain":"telegram_bot","service":"send_photo","entityId":"","data":"{\"file\":\"/config/www/snap.png\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1250,"y":220,"wires":[[]]},{"id":"4d40e61f.e3e758","type":"inject","z":"744085d0.20ac7c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":300,"wires":[["49ac734f.e9c70c"]]},{"id":"29566c30.83fbd4","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 January 2021 20:21 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/6 "2021-01-25T20:21:27Z")

</div>

Firstly, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

As for sending image from Nas, you essentially replace the http request with whatever means you have of grabbing the image from the Nas. E.g. a `file in` node will read a file and return a buffer. Give it a go. Use a debug node to check the file in node reads a buffer of bytes.

---

<div class="post-metadata">

**Author:** ![AndriesS82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andriess82/32/35842_2.png) [@AndriesS82](https://discourse.nodered.org/u/AndriesS82)\
**Post date:** [17 February 2021 18:44 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/7 "2021-02-17T18:44:14Z")

</div>

helo, i have try everything but i can't find it...  
Yesterday I reboot home assistant and suddenly everything works fine with my original code???  
How is this possible??  
Could it be with the cache memory??

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 April 2021 18:44 UTC](https://discourse.nodered.org/t/node-red-sending-picture-to-telegram/39656/8 "2021-04-18T18:44:49Z")

</div>

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