Node red sending picture to telegram

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?

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)..

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);
}

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

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

Here is a working example...

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

The demo flow...

[{"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}]
3 Likes

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

[{"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}]

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

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.

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??

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