Send a camera snapshot via Telegram

Hi all,

I’ve been busy experimenting with Node Red and I’m really like it so far. However, there’s a bit of a learning curve. I’ve spent some time today going through forums (including this one) and have a simple flow that enables me to obtain (either by writing a file to disk, or as a binary property of a msg object) a snapshot from a camera, but then I’d like to send that out via Telegram.

The following post is pretty good at getting started, but it sends the result via email, and not via Telegram:

I’ve tried both via the Telegram-Bot node, as well as via HomeAssistant’s Call Service node (using telegrambot.sendphoto), but where I’m stumbling is that both seem to want a public URL for a file (presumably, so that Telegram’s servers can download it), whereas I want to upload the camera snapshot to them.

Looking at the Telegram API, they’re happy to receive one as a multipart-form message, but nothing out of the box here seems to be doing the trick.

What am I missing?

Thanks!

The telegram_bot.send_photo service can either take a url argument for a remote path to an image, or the file argument for a local file. Documentation Link.

I have an automation that saves a camera snapshot to a folder called ‘pics’ in my config directory. To send that photo out, I use this action in an automation:

  - service: telegram_bot.send_photo
    data_template: 
      file: "/config/pics/camera_alert.jpg"

Thanks, so that confirms I can send from a local file. However, from my API node (which is calling the camera_proxy service, I receive a binary buffer response, then I pass this to a write file node, which is only writing 13 bytes…

I seem to be getting nowhere fast :slight_smile:

Ok, I got there in the end. I’ve pasted the working demo flow below to help anyone who’s attempting something similar. @Silicon_Avatar got me halfway there, but the other issue was my misuse of setting variables for msg payloads. Here’s the working flow:

[{"id":"c7fa6c57.aa19e","type":"tab","label":"Dev","disabled":false,"info":""},{"id":"da5bca72.a8ae88","type":"inject","z":"c7fa6c57.aa19e","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":480,"wires":[["83cb3705.7adc08"]]},{"id":"736b0172.0c043","type":"function","z":"c7fa6c57.aa19e","name":"prep msg","func":"msg.filename = '/config/www/snapshots/snap.jpg';\nmsg.payload = { \n data: {\n type: 'photo',\n file: '/config/www/snap.jpg',\n chatId: YOUR_CHAT_ID,\n caption: 'Snapshot from camera.' }\n};\nreturn msg;","outputs":1,"noerr":0,"x":680,"y":480,"wires":[["bfcaabb3.6fca68","1b7063f9.8a2adc"]]},{"id":"83cb3705.7adc08","type":"api-call-service","z":"c7fa6c57.aa19e","name":"","server":"eb4f80de.bd93a","service_domain":"camera","service":"snapshot","data":"{\"entity_id\":\"camera.driveway_cctv\",\"filename\":\"/config/www/snap.jpg\"}","mergecontext":"","output_location":"","output_location_type":"none","x":420,"y":480,"wires":[["736b0172.0c043"]]},{"id":"bfcaabb3.6fca68","type":"api-call-service","z":"c7fa6c57.aa19e","name":"","server":"eb4f80de.bd93a","service_domain":"telegram_bot","service":"send_photo","data":"","mergecontext":"","output_location":"","output_location_type":"none","x":970,"y":480,"wires":[[]]},{"id":"1b7063f9.8a2adc","type":"debug","z":"c7fa6c57.aa19e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":900,"y":420,"wires":[]},{"id":"eb4f80de.bd93a","type":"server","z":"","name":"Home Assistant"}]

Note, the file name and write location are hardcoded at the moment. I’d like to change that, but each time I do, it’s like the call service method ignores the typical {{ msg.variable }} parsing and inserts it as a string literal, which isn’t ideal!

3 Likes