Page 1 of 2

Telegram snapshots

Posted: Saturday 16 April 2022 19:56
by QNTL
Okay, I've lost it...

I just want a simple screenshot of my IP camera to Telegram.

I got a simple message working. The LUA I have(more like stole from someone with actual knowledge about this on the internet):
It works every time I test it.

Code: Select all

commandArray = {}
token = "53148xxxxx:AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx"
chatid = '-10017864xxxxx'
message = 'Dit is een bericht vanuit Domoticz!'
if (devicechanged['camtest'] == 'On') then
     os.execute('curl --data chat_id='..chatid..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..token..'/sendMessage" ') 
chat_id=-10017864xxxxxx -F photo="@https://192.168.2.x:xxxx/shot.jpg" ')

end
return commandArray
Then I found This. It provide me with the ability to send a photo.

Code: Select all

curl -s -X POST "https://api.telegram.org/bot<TOKEN>/sendPhoto" -F chat_id=<CHATID> -F photo="@</path/to/your/photo.jpg>"
But I wouldn't be here if it was working.

Then I found an Italian video (I'm not Italian btw). It led me to this site: This site.

But that simply doesn't work for me.

I've tested it succesfull with the email notification and attacement.
So the camera does work, and the script does do something, but it just doesn't go to Telegram...

I Also tried it with the prefix - user:[email protected]:xxxx/shot.jpg, but no photo.

I'm lost. Can someone help me..please! :? :o

Re: Telegram snapshots

Posted: Sunday 17 April 2022 8:15
by hoeby
QNTL wrote: Saturday 16 April 2022 19:56 Then I found This. It provide me with the ability to send a photo.

Code: Select all

curl -s -X POST "https://api.telegram.org/bot<TOKEN>/sendPhoto" -F chat_id=<CHATID> -F photo="@</path/to/your/photo.jpg>"
Then I found an Italian video (I'm not Italian btw). It led me to this site: This site.
Telegram can't take a snapshot from your camera, directly.
You first have to save a snapshot on your system. And then telegram can take that photo-file from your system and send it.

The one line code your have posted, where is the part that the code saves the snapshot to your system?

The italian script looks like it can work, everything is there in the code. But there are things you have to modify, to your system specifications. Simple copy / paste will not work.

Re: Telegram snapshots

Posted: Sunday 17 April 2022 12:05
by QNTL
Hello,

Thanks for the reply.

I came across that info as well. But the Italian script, doesn't seem to work. I've eddited it to my situation.

Code: Select all

-- dati servizio chat telegram
local bot = '5314xxxxxx';   -- Telegram Bot ID
local token = 'AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx';   -- Telegram Bot Token
local chatId = '-1001786xxxxxx';   -- Telegram Chat ID
local message = 'Dit is een test uit Script Italian';   -- messaggio di testo

commandArray = {}
if (devicechanged['camtest'] == 'On') then --esempio di stato di un sensore

--salvataggio in cartella temporanea snapshot
os.execute('wget http://192.168.2.x:xxxx/camsnapshot.jpg?idx=1 && sudo mv camsnapshot.jpg?idx=1  /var/tmp/camsnapshot.jpg ')

--invio snapshot a telegram
os.execute('curl -s -X POST "https://api.telegram.org/bot'..bot..':'..token..'/sendPhoto" -F chat_id='..chatId..' -F photo="@/var/tmp/camsnapshot.jpg " -F caption='..message..' ')

end
return commandArray

About the picture location...
When I'm in Domoticz/setup/cameras and press 'snapshot' it directs to http://192.168.2.x:xxxx/camsnapshot.jpg?idx=1
But it is a blank page. When I rightclick it, and press 'save link as' -> .jpg, I do get a screenshot.
In addition, if i just go to the direct link of the camera (https://192.168.2.x:xxxx/shot.jpg) in my browser, I do get the snapshot. If I do it in an incognito window, it needs to be like this https://admin:[email protected]:xxxx/shot.jpg in order to retreive it.

So I don't know which I should enter in the script.

I use Domoticz on a Pie, but when I go to the directory that is stated in the script, it doesn't have the screenshot.

If i check the log, there is no mention of the script. No error message neither.
So I disabled that script, copied the code of mr. Italian to my semi-working script, that does send a text message to Telegram. But the picture isn't save in the location.

In the log I get an error:
[string "commandarray = {} ...":5: Attemt to index a nil value (global 'devicechanged')
I also found this site. There they talk about a 'sendsnapshot.sh' script. It should be in the Domoticz folder but I don't know how to get it there. There is a github page with some scripts, but I don't know how to edit the script, so that they can use my variables.

I had hoped there would be a simple step by step guide to get them snapshots to Telegram, but when I google now, I see my post as one of the top results. :shock:

Re: Telegram snapshots

Posted: Sunday 17 April 2022 13:42
by hoeby
I find you capturing a little strang.
You make a capture and move the file.

I use this dzvents script without issues
You have to fill in, behind the = and between the ' '
* teleTok = your telegram ID
* chatId = your chat ID
* snapFile = the location where the snapshot will be stored
* domoReq = the http string of your camera
* camName = This is an addion the the "snapFile", if you have multi snapshots, then your can give them an unique name
* captiontxt = The text which is added to the picture when send by telegram

Also don't forget to change the trigger device

Code: Select all

return {
    on = { 
       devices = {'Camera voordeur'},
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("Camera voordeur") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = 'your telegram token'
            local chatId     = 'your chat id'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://192.168.1.XX:XXXX/camsnapshot.jpg?idx=1'
            local camName    = 'Voordeur'
            local captiontxt = 'Er wordt aangebeld aan de voordeur.'
            os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
            os.execute("rm " ..snapFile..camName..'.jpg')
        end
    end
}

Re: Telegram snapshots

Posted: Sunday 17 April 2022 16:10
by QNTL
Hello again,

I've entered your script with my variables.
Spoiler: show

Code: Select all

return {
    on = { 
       devices = {'camtest'},
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("camtest") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = '5314892761:AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx'
            local chatId     = '-1001786xxxxxx'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://192.168.2.x:xxxx/camsnapshot.jpg?idx=1'
            local camName    = 'camtest'
            local captiontxt = 'Er wordt aangebeld aan de voordeur.'
            os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
            os.execute("rm " ..snapFile..camName..'.jpg')
        end
    end
}

The log shows this:
Spoiler: show
2022-04-17 15:56:56.222 Status: dzVents: Info: Handling events for: "camtest", value: "On"
2022-04-17 15:56:56.223 Status: dzVents: Info: ------ Start internal script: Hoeby: Device: "camtest (Dummy)", Index: 72
2022-04-17 15:56:56.566 Status: dzVents: Info: ------ Finished Hoeby
But strangely nothing. Not even a textmessage.
Also, there is no file in the directory. And I know the last line makes sure it is deleted, but even with the line removed, there is no picture stored. I created the same directory to keep your script as close to the working thing.

Could it have something to do with permissions for domoticz to write something to my Pi? If so, where should I change that?
But something is working. With all my other scripts disable, I still get the snapshot to my email.
After disabling the camera snapshot as an attachement, I still get an email, but not the email (in .jpg anyways, it show up as text).

Re: Telegram snapshots

Posted: Sunday 17 April 2022 16:16
by QNTL
Oh oh oh...

Changed a little thing:
Spoiler: show

Code: Select all

return {
    on = { 
       devices = {'camtest'},
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("camtest") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = '5314892761:AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx'
            local chatId     = '-1001786xxxxxx'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://admin:[email protected]:xxxx/shot.jpg'
            local camName    = 'camtest'
            local captiontxt = 'Er wordt aangebeld aan de voordeur.'
            os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
            os.execute("rm " ..snapFile..camName..'.jpg')
        end
    end
}
I changed the link Domoticz made for the snapshot, to the link of the camera

So : http://192.168.2.x:xxxx/camsnapshot.jpg?idx=1
Became : 'http://admin:[email protected]:xxxx/shot.jpg'

And so I have received my first snapshot on Telegram!

Re: Telegram snapshots

Posted: Sunday 17 April 2022 20:11
by QNTL
Okay,

One last question.

When my Cam is offline, I don't get any messages.
Is it possible to always get the textmessage, and only get the attachment when the cam is online?

I've set my doorbel to sent the notification to telegram when it is pressed, and when the cam is online, your scripts works.

But that results in two messages on Telegram.

Coding is not my specialty but is there room for an:

if cam is active : Send message+attacement.
else : send message?

Re: Telegram snapshots

Posted: Sunday 17 April 2022 20:33
by hoeby
Does domoticz know if your camera is offline?

It is also prefered that you add this check.
The "os.execute" command holds your system. When the camera is offline, i think you get an error in the log, that it is running more then 10 seconds.

So start of your answer about different responses.
How does domoticz know that your camera is offline?

Re: Telegram snapshots

Posted: Sunday 17 April 2022 23:14
by QNTL
Hello,

I used an old phone as an ip camera as a testcase before buying a dedicated camera.
As a test I checked would have happened if the stream was offline, so I closed the stream.

It gave this error:

Error: (3.1.8) There is no device with that name or id: camtest

Which is not true, since there is a device, but the stream is off.
I would assume that Domoticz tries to get that snapshot and if it cannot find it, it says the device is missing.

Re: Telegram snapshots

Posted: Monday 18 April 2022 7:40
by hoeby
The error is a response because the script is trying to get a snapshot. Don't know how to let a dzvents script react on that kind of error.

If the stream is offline, is the device then also offline on your network?
I yes, then an option could be, to do a ping to the device and the response will change the telegram message/photo.
Could that be an option that can work?

Re: Telegram snapshots

Posted: Monday 18 April 2022 9:05
by QNTL
Hi,

I've checked out this wiki.

So now, when the camera is offline, it will show it in de devices tab.
How can I update the script so, when it'll will run the script, but only send the message?

Re: Telegram snapshots

Posted: Monday 18 April 2022 14:23
by hoeby
Try this one.
There is added local cameraname, fill in the name of the ping camera device, that domoticz knows if it is online or not.

Code: Select all

return {
    on = { 
       devices = {'camtest'},
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("camtest") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = '5314892761:AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx'
            local chatId     = '-1001786xxxxxx'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://192.168.2.x:xxxx/camsnapshot.jpg?idx=1'
            local camName    = 'camtest'
            local captiontxt = 'Er wordt aangebeld aan de voordeur.'
            local cameraname = 'pingcamera' -- This has to be the name of your camera device in domoticz

            if domoticz.devices(camera).state == 'On' then
                os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
                os.execute("rm " ..snapFile..camName..'.jpg')
            else
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage?chat_id='..chatId..'&text='..captiontxt ..'.'"')
            end            
        end
    end
}

Re: Telegram snapshots

Posted: Monday 18 April 2022 21:47
by QNTL
Hello,

Thanks Again. I think this is everything now. I've spotted a little error in the script.
In this line

Code: Select all

if domoticz.devices(camera).state == 'On' then
it should have been 'cameraname' instead of 'camera', but I saw it go wrong in the log.

Other than that, it works like a charm.
I've eddited the script a little, so there is another message when the camera is offline.
End result (thanks again!) is the code underneath!
The camera is bought, and I hope everthing goes smooth in terms of changing the variables. Thanks again (I've already said that :lol:)

Code: Select all

return {
    on = { 
       devices = {'Deurbel'},
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("Deurbel") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = '5314892761:AAGXWKsYHZYmepiSTvPWYE2EOn2CAxxxxxx'
            local chatId     = '-1001786xxxxxx'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://admin:[email protected]:xxxx/shot.jpg'
            local camName    = 'camtest'
            local captiontxt = 'Er staat iemand voor de deur. Bekijk de bezoeker!'
            local captiontxt2 = 'Er staat iemand voor de deur, maar je zal wel zelf moeten gaan kijken wie!'
            local cameraname = 'PINGcamtest' 

            if domoticz.devices(cameraname).state == 'On' then
                os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
                os.execute("rm " ..snapFile..camName..'.jpg')
            else
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage?chat_id='..chatId..'&text='..captiontxt2 ..'"')
            end            
        end
    end
}

Re: Telegram snapshots

Posted: Tuesday 19 April 2022 22:00
by hoeby
Sorry for the fault in the script.
But you found the solution, great.

Have fun with the script, glad that i could help

Re: Telegram snapshots

Posted: Friday 06 May 2022 16:25
by finch666
I'm running quite the same script:

Code: Select all

return {
    on = { 
       devices = {'doorbell'},  -- switch (pushbutton) device in domoticz
    },

    execute = function(domoticz, device)
        
        local Cam = domoticz.devices("doorbell") -- Actief
        
        if(device.state == 'On') then
            local teleTok    = xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            local chatId     = 'xxxxxxxxxxxxxxxxxx'
            local snapFile   = '/home/pi/domoticz/scripts/camera_'
            local domoReq    = 'http://192.168.1xx.xxx/capture'
            local camName    = 'ESP32CAM'                       -- cameraname in domoticz
            local captiontxt = 'Er staat iemand voor de deur. Bekijk de bezoeker!'
            local captiontxt2 = 'Er staat iemand voor de deur, maar je zal wel zelf moeten gaan kijken wie!'
            local cameraname = 'doorbell'        -- anything else causes a script error

            if domoticz.devices(cameraname).state == 'On' then
                os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg" -F caption="'.. captiontxt ..'"')
                os.execute("rm " ..snapFile..camName..'.jpg')
            else
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage?chat_id='..chatId..'&text='..captiontxt2 ..'"')
            end            
        end
    end
}
But it appears sometimes the second last picture is send, and not the last one taken. Anyone else got this problem? Or is there a glitch in my code?

Re: Telegram snapshots

Posted: Friday 06 May 2022 18:19
by QNTL
You should check if the picture gets deleted every time.
When I tested the script, I saw the picture being removed 100% of the time. With you it seems that is not the case.

As far as I know, the script can't overwrite a file if it already exists, so it'll just send the picture that was stored before the one the script is trying to save at that time.

If that makes sense..

Re: Telegram snapshots

Posted: Friday 06 May 2022 19:27
by finch666
Makes sense to me! Maybe there should be a little pause inserted after the command to get an image.
Anyway, found no snapshot anywhere in /home/pi/domoticz/scripts/, so that should be good.

Re: Telegram snapshots

Posted: Friday 06 May 2022 20:42
by QNTL
You could also try to move the command to remove the picture to the line above the line that takes the snapshot.

That way the last one is always back up on your location and before taking the snapshot, it should remove the one in the folder, prior to storing a new one.

And yes. The pause would be the best option, but I don't have the knowledge to tell you how to do that :lol:

Re: Telegram snapshots

Posted: Saturday 07 May 2022 8:01
by hoeby
As far that i know, the os.execute command pauses the script, untill that line is executed. No need for an extra pause.
By moving the remove picture line above the other line, that will give a delay before the new picture is made. The length of the delay is variable, because the system waits untill the remove is executed

Does your system have the rights to remove the file in the folder?

I am using this script for years without issues.
There is also an option to work with .sh files, you could try that option.

Re: Telegram snapshots

Posted: Saturday 07 May 2022 16:08
by finch666
Thanks, but today I also noted a significant delay in triggering the actual chime (a command from domoticz to trigger an event on a espeasy device), and I can't have that with a doorbell. So in my configuration reliability is an issue, somehow. Could be domoticz has a lot of other things to do, leading to unwanted delays.