Send a photo to Telegram

I need some help in order to send a photo taken with the pi camera to Telegram.

I am using "node-red-contrib-telegrambot-home" and these are the flow and nodes:

flow

camerapi-takephoto

telegram_payload

--

telegram_notify

With this flow the picture is taken and stored in the Pi, but I get nothing in Telegram.

I also tried with a "funtion" node instead of a "telegram payload" node:

funtion

In this case what I get is a text in the TelegramBot: "/home/pi/Pictures/foto". But not picture at all.

Could somebody help me?

Thank you so much.

Have you checked to see that the photo actually exists and checked the name?

It's often easier to break a problem down.
i.e a flow to capture a photo and store it
a flow that gets a stored photo and sends it to telegram

That way you can see where the problem occurs

1 Like

Thanks for your response.
Yes, I have checked that the photo is in the right folder and with the right name.

Is the send too quick... has the photo been fully written to disk and the file closed before the payload node tries to read it ?

dceejay, thank you so much for your response.

That does not seem to be the problem. I have put a delay (first of 1 and then of 5 seconds) after the "camerapi-takephoto" node and it does not work.

You have to send the data as a multi-part form similar to any other file upload to a server. Details are here:

Thank you so much for your response.

I have tried this node:

Still not working. I am afraid this multi-part form thing is far out of my current knowledge.

I copy the flow just in case somebody can detect any mistake.

Thanks again.

[{"id":"85be023a.0552e","type":"inject","z":"91952aac.1bd658","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":100,"wires":[["6544f6e3.7a6388"]]},{"id":"6544f6e3.7a6388","type":"camerapi-takephoto","z":"91952aac.1bd658","filemode":"1","filename":"foto","filedefpath":"0","filepath":"/home/pi/Pictures/","fileformat":"jpeg","resolution":"1","rotation":"0","fliph":"0","flipv":"0","brightness":"50","contrast":"0","sharpness":"0","quality":"80","imageeffect":"none","exposuremode":"auto","iso":"0","agcwait":"1.0","led":"0","awb":"auto","name":"","x":360,"y":180,"wires":[["21262984.983446"]]},{"id":"21262984.983446","type":"delay","z":"91952aac.1bd658","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":400,"y":240,"wires":[["c637d704.e41cd8"]]},{"id":"c637d704.e41cd8","type":"http-send-multipart","z":"91952aac.1bd658","name":"","ret":"bin","filepath":"/home/pi/Pictures/foto.JPEG","url":"my_public_IP:my_port","tls":"","x":450,"y":280,"wires":[["73793f29.5deae"]]},{"id":"73793f29.5deae","type":"telegrambot-payload","z":"91952aac.1bd658","name":"","bot":"ab134760.32e278","chatId":"My_Chat_ID","sendMethod":"sendPhoto","payload":"{\n    \"photo\": \"/home/pi/Pictures/foto.JPEG\",\n    \"caption\": \"Test\"\n}","x":510,"y":340,"wires":[["d68268c7.c33238"]]},{"id":"d68268c7.c33238","type":"telegrambot-notify","z":"91952aac.1bd658","name":"","bot":"ab134760.32e278","chatId":"My_chat_ID","message":"","parseMode":"HTML","x":520,"y":400,"wires":[]},{"id":"ab134760.32e278","type":"telegrambot-config","z":"","botname":"NodeRedBot","usernames":"Talud08","chatIds":"209723044","pollInterval":"300"}]

Hi everyone,

Just for the records, I've made a different approach to this problem and now it is solved.

I've used an Exec Node to run a system command and execute a python script, to take a picture with the raspberry pi camera and send it to a telegram bot.

I have to give the credit to Carlo Mascellani
http://rpihome.blogspot.com/2016/04/using-telegram-with-raspberry-pi.html
Thanks Carlo

This is the code

import telegram
import picamera

from telegram_keys import (
    my_token,
    my_chat_id
)
    

# Connect to our bot
bot = telegram.Bot(my_token)

# Sets the id for the active chat
chat_id=my_chat_id

#Get the photo
camera=picamera.PiCamera()
camera.capture('/home/pi/Pictures/capture.jpg')
camera.close()

# Sends a message to the chat
bot.sendPhoto(chat_id=chat_id, photo=open('/home/pi/Pictures/capture.jpg', 'rb'))

Thank you everyone for your support.

2 Likes
import telegram import picamera from telegram_keys import ( my_token, my_chat_id ) # Connect to our bot bot = telegram.Bot(my_token) # Sets the id for the active chat chat_id=my_chat_id #Get the photo camera=picamera.PiCamera() camera.capture('/home/pi/Pictures/capture.jpg') camera.close() # Sends a message to the chat bot.sendPhoto(chat_id=chat_id, photo=open('/home/pi/Pictures/capture.jpg', 'rb'))

@talud08

Would this script mean the flow requires a telegram payload node or no?
Would you mind posting your flow?

1 Like

how can we send a picture from http://192.168.1.xx:5000/clips/frontdoorcctv-1618757066.85689-5afutf.jpg location

Use a http request node to get the binary image, then use that in the msg property.

This post demonstrates requesting a picture from a http resource then sending it via telegram

2 Likes

Thanks for the help i managed to send picture to telegram

Now i am in different situation

I would like to fetch the picture from my RTSP camera which stores file (Everytime a motion detected) on my secondary hard drive (Clips folder)
I am able to get the image to my telegram using this link http://192.168.1.66:4000/clips/cctv-XXXXXXXXXX.jpg using http get request node.

But the problem here is in the above link http://192.168.1.66:4000/clips/cctv-XXXXXXXXXX.jpg whenever motion detected event id XXXXXXXXXX changes.

I am able to extract the event id XXXXXXXXXX using MQTT+Jason+Change payload nodes.

How do i combine the eveint id XXXXXXXXXX & create a url every time & send it thru http get requet node?

Please help

1 Like

There are a few ways.

One way would be to store the XXXXXXXX value in flow context then use it to build a dynamic URL.

Use a change node to store the value from MQTT e.g. store the XXXXXXX value in flow.lastEventId using a change node.

Then add a function node before the HTTP Request node...

var eventID = flow.get("lastEventId");
if(eventID) {
    msg.url = `http://192.168.1.66:4000/clips/cctv-${eventID}.jpg`;
    return msg;
}

Alternatively, if you want to initiate this flow when the MQTT value arrives, simply change your change node around to set msg.url to "http://192.168.1.66:4000/clips/cctv-" & eventID & ".jpg"
(where eventID is the XXXXXXX value you managed to extract)


NOTE: to use a dynamic url in the request node, leave the HTTP Requests URL field empty so that it uses msg.url

1 Like

I managed to get the image till the HTTP node. but not able to send to telegram
below is my flow


Function node (send picture) after HTTP is as follows

  content: msg.payload,
  caption : 'Someone At Your Front Door',
  type : 'photo',
  chatId: 'XXXXXX',
}

msg.payload = pl;
return msg;```

The obvious questions...

  • Is the http request set to return a buffer?
  • Is the chatId number you used confirmed working for regular messages?

Also, as I cannot see your full code, here is a working demo...

var pl = {
  content: msg.payload, // <-- check msg.payload is a buffer
  caption: `Someone At Your Front Door`,
  type : 'photo',
  chatId: 123456789 // <--  set chat id number
}
msg.payload = pl;
return msg;

my http node details is as follows

Yes my chat ID is right correct (though I have not shown it here)

I dont get it working

Not sure what's going wrong

It is quite difficult to help further without the following...

  1. a copy of your flow (export using ctrl+e)
  2. an image to test with (you can temporarily connect the http node to a file node to save the buffer to file)

@ceaswaran I think this is related to using a different set of telegram nodes.

For anyone following along. @ceaswaran sent a copy of his flow privately. When i imported it, I got an warning "unknown: telegrambot-notify"

That node is not part of node-red-contrib-telegrambot but instead appears to be a node from node-red-contrib-telegrambot-home.

FYI: node-red-contrib-telegrambot is by far the more popular choice and the one I use - I can't help with why the node-red-contrib-telegrambot-home nodes dont work for you. All i can say is the telegram nodes I use do work.

1 Like

Ah, as always, a lesson in making sure that you share all relevant information up-front when looking for help.

2 Likes

Using node-red-contrib-telegrambot Worked Thanks