Telegram Bot Send Image From Share

Hi All,

i am new to node red barely using it for few days and still learning

Trying to send the image using telegram sender node ( The saved image file is located in Home Assistant server 1 and Node Red is running on separate standalone server 2 )

Current Setup

Server 1 Node red running telegram bot
Server 2 Running Home assistant and image is getting stored at location named /share in home assistant server

Code used in function node

msg.payload = {}
msg.payload.chatId = -12345678
msg.payload.type = "photo"
msg.payload.content = "/share/snapshot_Dahua - MediaProfile_Channel1_SubStream1.jpg"
return msg;

Requirment

Node red need to send the snapshot from the /share location on server 1 as telegram notification .

Current flow used by me and its is not working and errors as per debug

3/21/2021, node: Motion Detected
msg : string[28]
"msg.payload.content is empty"

if i add the content payload to the function i am getting this error

Caught exception in sender node:

'Error: ETELEGRAM: 400 Bad Request: URL host is empty'

when processing message: 

{"_msgid":"8edb6561.8dc748","payload":{"chatId":-1234568,"type":"photo","content":"/share/snapshot_Dahua - MediaProfile_Channel1_SubStream1.jpg","options":{"chat_id":-123456789,"photo":"/share/snapshot_Dahua - MediaProfile_Channel1_SubStream1.jpg"}},"topic":""}
[{"id":"c2cf9273.a27e08","type":"tab","label":"Camera Motion Detection","disabled":false,"info":""},{"id":"6c99316a.409cb","type":"server-state-changed","z":"c2cf9273.a27e08","name":"Camera Cell Motion Detection","server":"f96cd7e5.e97f3","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"camera.dahua_mediaprofile_channel1_substream1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":150,"y":220,"wires":[["e4e4a7f3.283d3"]]},{"id":"217e8e52.a70a12","type":"telegram sender","z":"c2cf9273.a27e08","name":"Motion Detected","bot":"b1c8e34b.bb945","haserroroutput":false,"outputs":1,"x":900,"y":80,"wires":[["9fdb2672.2d8be8"]]},{"id":"a9b344a3.324088","type":"function","z":"c2cf9273.a27e08","name":"","func":"msg.payload = {\n\tchatId : ,\n\ttype :  \"photo\",\n\tfile : \"/share/snapshot_Dahua - MediaProfile_Channel1_SubStream1.jpg\"\n\t};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":140,"wires":[["217e8e52.a70a12","2a2f3f72.94c028"]]},{"id":"add56cbe.ffaf6","type":"inject","z":"c2cf9273.a27e08","name":"Inject","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":150,"y":320,"wires":[["e4e4a7f3.283d3"]]},{"id":"e4e4a7f3.283d3","type":"api-call-service","z":"c2cf9273.a27e08","name":"Take Snap Shot","server":"f96cd7e5.e97f3","version":1,"debugenabled":false,"service_domain":"camera","service":"snapshot","entityId":"camera.dahua_mediaprofile_channel1_substream1","data":"{\"filename\":\"/share/snapshot_{{ entity_id.name }}.jpg\"}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":400,"y":220,"wires":[["c08b8e58.b5b838","a9b344a3.324088"]]},{"id":"2a2f3f72.94c028","type":"debug","z":"c2cf9273.a27e08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":220,"wires":[]},{"id":"c08b8e58.b5b838","type":"debug","z":"c2cf9273.a27e08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":300,"wires":[]},{"id":"9fdb2672.2d8be8","type":"debug","z":"c2cf9273.a27e08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":120,"wires":[]},{"id":"f96cd7e5.e97f3","type":"server","name":"","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"b1c8e34b.bb925","type":"telegram bot","botname":"","usernames":"","chatids":"-","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Hi & welcome to the forum.

There are quite a few similar threads in this forum that should help.

Have you tried sending the image as a buffer?

Here is a demo that helped a user & is quite similar to your requirement.

In your case, read the file into node-red. Or grab the image from the camera directly without any files at all. Both options are possible & covered in the demo

Hi Steve ,

Thanks for tagging the thread , will try and let you know .

i did search the forum and found few thread similar to my request but they didn't help so thought of asking the experts help directly .

@Steve-Mcl

Thanks i imported that flow and made a small change to it by adding the camera snapshot before the file in node

How could i make the service node to directly feed the image as buffer like you said instead of saving it local on the /share once snapshot is triggered .

msg : error
"Error: ENOENT: no such file or directory, open 'snapshot_Dahua - MediaProfile_Channel1_SubStream1.jpg'"


File In

What is a service node? I assume this is a HA thing? You might have better luck asking on HA forum.

However, a potentially better solution, just send a trigger from HA and use node-red to grab the image directly from the camera.

Yes Service node is part of HA :slight_smile: .

Is there any specific node can you recommend to grab image directly from my camera from inside node red.

Does the http Request node with RTSP URL fit the requirement ??

If your camera has a snapshot url(most do) point a http request node at it, set to return a buffer. Just like the random image request in the demo.

As for RTSP search the forum.

Other options include ONVIF (again search the forum)

Ps, what is the make and model of camera?

@Steve-Mcl

Thanks for suggestion i tried the snapshot URL with HTTP node and got the below error in debug

but camera snapshot url works perfectly fine when tried to open with browser .

Caught exception in sender node:'Error: EFATAL: Unsupported Buffer file-type'when processing message: {"payload":{"content":{"type":"Buffer","data":[]},"message":"Hey ., here is /pic random you requested","type":"photo","chatId":,"chat":{"id":2,"title":"Private Group","type":"group","all_members_are_administrators":true},"from":{"id":,"is_bot":false,"first_name":".","username":"","language_code":"en"},"message_id":275,"options":{"chat_id":-12345678}},"originalMessage":{"message_id":275,"from":{"id":1,"is_bot":false,"first_name":".","username":"","language_code":"en"},"chat":{"id":-588540112,"title":"Private Group","type":"group","all_members_are_administrators":true},"date":1616326003,"text":"/pic random","entities":[{"offset":0,"length":4,"type":"bot_command"}]},"_msgid":"70fe1cc5.1b0a14","statusCode":401,"headers":{"www-authenticate":"Digest realm=\"Login to 45a442a8f33042a41785231e04c6b15e\", qop=\"auth\", nonce=\"...

My camera is Onvif Supported , Dahua Pro Series IPC-HDW5241H-AS-PV

https://www.dahuasecurity.com/products/All-Products/Network-Cameras/WizMind-Series/5-Series/Active-Deterrence/IPC-HDW5241H-AS-PV

put a debug node on the http request - what do you get from the camera?

Also share your flow so i can see what property to add to the telegram payload content

@Steve-Mcl

http request node returns a empty output as per debug

Please find the flow , i am using the same flow which you tagged .

Only change i did was replace the URL in http request node with the snapshot url of the camera .

FYI : when ever i open the snapshot url of the camera i am getting a popup in browser saying
"You are about to log in to the site “192.1.1.150” with the username “camera01” . after Clicking Ok only i can see the snapshot from camera .... does the popup affect the http request node aswell ?

Current Flow goes as below

[{"id":"359b15d1.c317ba","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"b2d22ec9.f90d6","type":"telegram sender","z":"359b15d1.c317ba","name":"","bot":"b1c8e34b.bb925","haserroroutput":false,"outputs":1,"x":970,"y":240,"wires":[["5c3ddee9.c91bd"]]},{"id":"ac172f0f.fa5c88","type":"telegram command","z":"359b15d1.c317ba","name":"/pic","command":"/pic","bot":"b1c8e34b.bb925","strict":false,"hasresponse":false,"useregex":false,"removeregexcommand":false,"outputs":1,"x":70,"y":220,"wires":[["11f59f32.0e7c79"]]},{"id":"5c3ddee9.c91bd","type":"debug","z":"359b15d1.c317ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1250,"y":140,"wires":[]},{"id":"37328fef.43b2","type":"function","z":"359b15d1.c317ba","name":"send picture","func":"var pl = {\n  content: msg.payload,\n  message: `Hey ${msg.originalMessage.from.first_name}, here is ${msg.originalMessage.text} 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":770,"y":240,"wires":[["b2d22ec9.f90d6"]]},{"id":"11f59f32.0e7c79","type":"switch","z":"359b15d1.c317ba","name":"local, random, other","property":"originalMessage.text","propertyType":"msg","rules":[{"t":"eq","v":"/pic local","vt":"str"},{"t":"eq","v":"/pic random","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":320,"y":320,"wires":[["41fb3a6e.d3678c"],["912386c0.d40d48"],["ba9f933.4ecc8f"]]},{"id":"41fb3a6e.d3678c","type":"file in","z":"359b15d1.c317ba","name":"load image","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":570,"y":220,"wires":[["37328fef.43b2"]]},{"id":"ba9f933.4ecc8f","type":"function","z":"359b15d1.c317ba","name":"Dont understand message","func":"var message = \n`\n## I dont understand 🤔\n\nPlease send \\`/pic local\\` or \\`/pic random\\`\n\n`;\n\nmsg.payload.type = 'message';\nmsg.payload.content = message;\n\n// activate markdown\nmsg.payload.options = {\n  disable_web_page_preview : true, \n  parse_mode : \"Markdown\"\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":400,"wires":[["b2d22ec9.f90d6"]],"info":"## New message\n\nHello *markdown* formatted messages!'\n\nwith `inline` code\n\n> and quote\n>> nested\n\n\\`\\`\\`\nvar code = \"cool\";\n\\`\\`\\`\n\n| table | data |\n|---|---|\n| 1 | fred |\n| 2 | bob |\n\n_Italic_ **bold** "},{"id":"912386c0.d40d48","type":"http request","z":"359b15d1.c317ba","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"http://username:password@ipaddress/cgi-bin/snapshot.cgi","tls":"","persist":false,"proxy":"","authType":"","x":550,"y":320,"wires":[["37328fef.43b2","246fdd1e.198872"]]},{"id":"246fdd1e.198872","type":"debug","z":"359b15d1.c317ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":340,"wires":[]},{"id":"b1c8e34b.bb925","type":"telegram bot","botname":"","usernames":"","chatids":"-123456789","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Probably.

Try some of the URLs listed here

@Steve-Mcl

Tried multiple url listed all works but i cannot avoid the browser popup saying

"You are about to log in to the site “192.1.1.150” with the username “camera01”

@Steve-Mcl

To over come the popup problem i tried to change few options in the http node as shown below but still empty value as out put .

I have managed to do this with a dahau camera (I remember have some difficulty) I think they use digest authentication.

Lastly, check in devtools network tab, refresh the page. See if you can spot the URL where the image comes from.

@Steve-Mcl

Digest authentication did the trick , got it working:) /pic random worked like charm & received the snap in telegram .

Thanks thanks thanks a lot Steve its been 2 continuous days i spent my time on this thing finally the output made my day .

I learned a quite new things from you about node red , Such a power packed application .

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

My final flow as per my plan and as per your best suggestions i am getting images without saving it anywhere on server .

My Flow Export

[{"id":"f3d4e1ca.79f14","type":"tab","label":"Camera Motion Detection","disabled":false,"info":""},{"id":"35639059.d7bfc8","type":"server-state-changed","z":"f3d4e1ca.79f14","name":"Camera Cell Motion Detection","server":"f96cd7e5.e97f3","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"camera.dahua_mediaprofile_channel1_substream1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":200,"y":200,"wires":[["4253b48c.b0d3f4"]]},{"id":"4253b48c.b0d3f4","type":"http request","z":"f3d4e1ca.79f14","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"http://192.168.1.115/cgi-bin/snapshot.cgi","tls":"","persist":false,"proxy":"","authType":"digest","x":490,"y":200,"wires":[["3a306c79.81491c"]]},{"id":"3a306c79.81491c","type":"function","z":"f3d4e1ca.79f14","name":"","func":"msg.payload.chatId=-123456789\nmsg.payload.type= 'photo';\nmsg.payload.caption= \"Caption\"\nmsg.payload.content= msg.payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":200,"wires":[["30ae57c4.736098"]]},{"id":"30ae57c4.736098","type":"telegram sender","z":"f3d4e1ca.79f14","name":"","bot":"b1c8e34b.bb925","haserroroutput":false,"outputs":1,"x":960,"y":200,"wires":[[]]},{"id":"f96cd7e5.e97f3","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"b1c8e34b.bb925","type":"telegram bot","botname":"","usernames":"","chatids":"-123456789","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Code in function node

msg.payload.chatId=-123456789
msg.payload.type= 'photo';
msg.payload.caption= "Caption"
msg.payload.content= msg.payload
return msg;
3 Likes

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