Taking a picture and sending it via telegram

hello everyone, good evening i have integrated telegram with my node red, i am able to send messages and get responds from node red. but now i am looking forward to requesting for a picture from node red and the picture send to me on the telegram bot. i have looked at lots of tutorials and tried them but non of them work for me. pls anyone help me out with a wayout

When I send this data from a function node (triggered by an inject node or something else)

var payload = { 
    chatId: mychatid,
    type: "photo",
    content: "/home/pi/.node-red/static/back.png" };
    
return {payload};

(replace mychatid with the chat ID of your Telegram user)

to my telegram out node, I instantly receive the picture, that does exist in the path above on my Raspberry Pi (the host, where Node-RED is running, NOT the path on a computer, where my browser is running!)
I'm using the telegram node node-red-contrib-telegrambot 8.1.0

BTW: Thanks for triggering me :smiley: I've never tried that before :rofl:

1 Like

Thanks for your response
I tried but it did not work
I am using a function node dat stores my chatid
And i have a USB webcam connected to my pi. I tried were the function node holding my chat id injects into the function node u sent above but did not get anything.

msg.payload.content can also be a buffer object

Are you able to get the image from the webcam into a buffer?

Here is a demo i did...

[{"id":"99ee972c.22d088","type":"telegram sender","z":"5d3f964c.fef898","name":"","bot":"fa7a9376.321ce","x":930,"y":280,"wires":[["72b71335.355a9c"]]},{"id":"58e78fc.dd0b27","type":"telegram command","z":"5d3f964c.fef898","name":"/pic","command":"/pic","bot":"fa7a9376.321ce","strict":false,"hasresponse":false,"x":130,"y":280,"wires":[["3eb663cf.b0d58c"]]},{"id":"72b71335.355a9c","type":"debug","z":"5d3f964c.fef898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":240,"wires":[]},{"id":"2058d729.1a3a48","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 ${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":730,"y":280,"wires":[["99ee972c.22d088"]]},{"id":"959c0404.742708","type":"image","z":"5d3f964c.fef898","name":"","width":160,"data":"payload","dataType":"msg","thumbnail":false,"active":true,"x":920,"y":360,"wires":[[]]},{"id":"3eb663cf.b0d58c","type":"switch","z":"5d3f964c.fef898","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":280,"y":360,"wires":[["7c8090ca.00c02"],["dec15032.19344"],["c41817dc.703418"]]},{"id":"7c8090ca.00c02","type":"file in","z":"5d3f964c.fef898","name":"load image","filename":"node-red-256.png","format":"","chunk":false,"sendError":false,"encoding":"none","x":510,"y":280,"wires":[["2058d729.1a3a48","959c0404.742708"]]},{"id":"c41817dc.703418","type":"function","z":"5d3f964c.fef898","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":560,"y":440,"wires":[["99ee972c.22d088"]],"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":"dec15032.19344","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":510,"y":360,"wires":[["959c0404.742708","2058d729.1a3a48"]]},{"id":"fa7a9376.321ce","type":"telegram bot","z":"5d3f964c.fef898","botname":"my_bot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"","usesocks":false,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
1 Like

I tried this about a year and a half ago but couldn't grok Telegram bot functionality. I gave up and simply sent the image using my phone provider's Email to MMS gateway as an attachment using the Email node.

Not all providers have Email to MMS gateways so I'd like to revisit Telegram again. Do you have a link on a good tutorial for setting things up?

I think my complication is I need the image to go to two or three "users" and setting up the group stuff seemed to be where my problem was.

TIA.

Don't know if this is what you are looking for, but that's how I'm doing it:

1 Like

My issues were setting up Telegram and using the "botfather" I got the node-red to send a message but I couldn't get it to send to multiple users as a single group.

I think you are right. You have to send it to each user to their own chat.
At least that's what I'm doing.

1 Like

thank you for your response
no am unable to get the pictures in to a buffer.

Show me your flow. I'll see if I can help.

here is the flow. it is part of a bigger flow i just selected this part and send to u

[{"id":"c20191e9.9a047","type":"function","z":"ec443684.0a5168","name":"Camera","func":"// Set the required access for this function here\nvar checklevel = 1;\n\n// Handle the help request\nif (msg.originalMessage.help) {\n    // only add the help command if the user has access to execute it\n    if (msg.originalMessage.from.accesslevel >= checklevel) {\n        msg.payload.content = msg.payload.content + \"\\r\\ncamera: request a captured image\";  \n    }\n    return [null,null,msg];\n}\n\n// Check if the current message contains this function\nif (msg.originalMessage.command===\"camera\") {\n    // check if the user has access to execute this function\n    if (msg.originalMessage.from.accesslevel >= checklevel) {\n        return [msg,null,null];  \n    } else {\n        return [null,msg,null];\n    }\n} else {\n    // pass the message to the next node\n    return [null,null,msg];\n}\n\n\n","outputs":"3","noerr":0,"x":360,"y":1460,"wires":[["1f3eb62a.d2f7ea"],["588be983.c15c18"],["766aa86c.deb3b8"]],"outputLabels":["User has access","Insufficient access","Handover to next node"]},{"id":"1f3eb62a.d2f7ea","type":"change","z":"ec443684.0a5168","name":"Set response text","rules":[{"t":"set","p":"payload.content","pt":"msg","to":"Capturing image...","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":805,"y":1440,"wires":[["2eb00b66.bc74c4","8271f4b5.029068"]]},{"id":"b106c5bb.a9f478","type":"function","z":"ec443684.0a5168","name":"create image message","func":"var payload = { \n    chatId: 587135869,\n    type: \"photo\",\n    content: \"/home/pi/.node-red/static/back.png\" };\n    \nreturn {payload};","outputs":1,"noerr":0,"x":1025,"y":1498,"wires":[["2eb00b66.bc74c4","eb267fb.faf568"]]},{"id":"6935dc3a.a39994","type":"link in","z":"ec443684.0a5168","name":"Telegram camera response","links":["c5354408.9490c8"],"x":795,"y":1500,"wires":[["b106c5bb.a9f478"]]},{"id":"eb267fb.faf568","type":"debug","z":"ec443684.0a5168","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1390,"y":1500,"wires":[]},{"id":"8271f4b5.029068","type":"function","z":"ec443684.0a5168","name":"Save Chatid","func":"msg.chatId = msg.payload.chatId;\nreturn msg;","outputs":1,"noerr":0,"x":1370,"y":1440,"wires":[["c5354408.9490c8"]]},{"id":"c5354408.9490c8","type":"link out","z":"ec443684.0a5168","name":"","links":["6935dc3a.a39994"],"x":1555,"y":1440,"wires":[]},{"id":"2eb00b66.bc74c4","type":"telegram sender","z":"ec443684.0a5168","name":"Send response","bot":"f050d812.38ad48","x":1420,"y":1260,"wires":[[]]},{"id":"c4ce3ebf.5ae48","type":"telegram receiver","z":"ec443684.0a5168","name":"","bot":"f050d812.38ad48","saveDataDir":"","filterCommands":false,"x":160,"y":1280,"wires":[["2211fd3b.796ad2"],[]]},{"id":"f050d812.38ad48","type":"telegram bot","z":"","botname":"ChenkoHome_bot","usernames":"","chatids":"","baseapiurl":"","pollinterval":"","usesocks":false,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

wen i request for the picture i get capturing image but the picture never comes

You have missed out the camera node

Which node is it that takes the picture & can you export the flow again with the camera node included

i have tried using this your flow, i can actually get random pictures. but not local.
how can i make this send me pics from my camera

i did not use a camera node, from all the tutorials i saw they just used a function node and did it the way i did mine and got pictures. so i did not used any camera node.

i have a USB webcam connected to my pi

wen i request for the picture i get capturing image but the picture never comes

thats probably why you dont get an image - unless I misunderstand your setup

So you are now trying to send local images?
In node create image message you try to send "/home/pi/.node-red/static/back.png" to telegram
Does this file exist?

There is no where at all in your flow that seems to actually get a jgp file or buffer from any kind of camera! - I can wee where you set the message "Capturing image..." - but not where you would get a live image.

More notes...

You will need asomething to get an image from camera.

I Suggest the USBCamera node

Youre gonna need something like this....

image

Use the flow I posted & replace the HTTP node" or "File In" node with a USB camera node set to "get buffer"

okay thank you let me try that and see what happens

i have done that and it did not still work
i get this message from from the image preview node

Input should be a buffer or a base64 string or a Jimp image (containing a jpg or png image)

add a debug after camera - what comes out of the camera node?

Did you set it to output a buffer?

share your flow