Page 1 of 1

temporally store image, for sending notification telegram

Posted: Sunday 27 January 2019 15:07
by hoeby
I want to send a camera snapshot by telegram.
The link for the camera snapshot is http://XXX.XXX.XXX.XXX/jpg/1/image.jpg
Unfortunally i can't get this type of camera added in domoticz. Therefor i was hoping to get 1 scripts for DZvents.

What i have is this. But i got stuck how i can temporally save a snapshot by dzvents.
Googled some and found some, but those scripts are way over my head to get the idea how they exactly work

Code: Select all

local camera1 = http://XXX.XXX.XXX.XXX/jpg/1/image.jpg

return {
    on = { 
       devices = {'Backlight Dashboard'},
    },

    execute = function(domoticz, device)
        if(device.state == 'On') then
          --os.execute('/home/pi/domoticz/scripts/snapshot.sh')
	   domoticz.log('Foto gemaakt en verstuurd', domoticz.LOG_INFO)
	   domoticz.notify('snapshot', camera1, domoticz.PRIORITY_HIGH)
        end
end
}
I have this script, which works.
But was hoping everything could be run by dzvents and not 2 scripts

Code: Select all

#!/bin/sh
SnapFile="/var/tmp/camsnapshot.jpg"
# Krijg snapshot van camera.
# Voer IP-adres in op XXX.XXX.XXX.XXX (houd rekening met user/password, dat script ook snapshot kan maken)
wget -O $SnapFile "http://XXX.XXX.XXX.XXX/jpg/1/image.jpg"
# Send Telegram message with image
curl -s -X POST "https://api.telegram.org/bot"DEZE TEKST VERVANGEN VOOR TELEGRAM API SLEUTEL"/sendPhoto" -F chat_id=DEZE TEKST VERVANGEN VOOR CHAT_ID -F photo="@$SnapFile"
# Remove Image
/bin/rm $SnapFile

Re: temporally store image, for sending notification telegram

Posted: Sunday 27 January 2019 15:16
by emme

Re: temporally store image, for sending notification telegram  [Solved]

Posted: Sunday 27 January 2019 15:40
by hoeby
Thanks Emme,
I got it working. Changed a few thinks and tested it in steps to see what happens (with domoticz.log)
And yes, it works.

Only need to test if it overwrites the made .jpg file. Or i need to delete the made .jpg file.
That's for later to test.

This is what i finished with

Code: Select all

return {
    on = { 
       devices = {'Backlight Dashboard'},
    },

    execute = function(domoticz, device)
        if(device.state == 'On') then
            local teleTok   = 'xxxxxxxxxxxxxxxxxxxxxx'
            local chatId    = 'xxxxxxxxxxxxxxxxxxxxxx'
            local snapFile  = '/home/pi/domoticz/scripts/camera_'
            local domoReq   = 'http://xxx.xxx.xxx.xxx/jpg/1/image.jpg'
            local camName   = '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"')
        end
    end
}

Re: temporally store image, for sending notification telegram

Posted: Sunday 27 January 2019 18:42
by hoeby
Added this code undernead the 2 other os.execute rows.
Be sure to add it undernead, otherwise the .jpg is deleted before it is sended with telegram

Code: Select all

os.execute("rm " ..snapFile..camName..'.jpg')

This removes the .jpg file. So i am sure that there are no problems with overwrite permisions.

Re: temporally store image, for sending notification telegram

Posted: Thursday 16 January 2020 18:36
by DieHappy
Hi, can You explain how to set user/ and password.
I get the error 401 connection refused. Looks like it is used and password related.

Re: temporally store image, for sending notification telegram

Posted: Wednesday 20 May 2020 14:50
by wervisser
Works perfectly, also with a username/password protected camera.

For some reason it keeps sending two same pictures :?

Re: temporally store image, for sending notification telegram

Posted: Wednesday 20 May 2020 15:22
by waaren
wervisser wrote: Wednesday 20 May 2020 14:50 For some reason it keeps sending two same pictures :?
Can you switch on scriptlevel debug logging? It might reveal what is happening. Could it be that the script is triggered twice?

Re: temporally store image, for sending notification telegram

Posted: Sunday 11 February 2024 16:02
by ssk17051980
this script still works ?

Re: temporally store image, for sending notification telegram

Posted: Saturday 02 March 2024 20:06
by habahabahaba
Here is my way to send fotos from cams on motion detect.

But i'm on windows, so some code may not work.

Code: Select all

execute = function(domoticz, device)

	local tBot = '<your bot number>'
	local tChatId = '<your chat id>'
	    
	    local current_date_time = os.date("%Y-%m-%d_%H-%M-%S")

        local camera_url1 = '"http://<user>:<password>@192.168.0.36/webcapture.jpg?command=snap&amp;channel=1"' -- adress of camera snapshot
        
        local image_path1 = '"C:/Domoticz/screensfromcams/cam2floor/'.. current_date_time ..'.jpeg"' -- path to store snapshot
        
        local command1 = 'curl ' .. camera_url1 .. ' > ' ..image_path1  -- команда для выполнения 1
            
              
                os.execute(command1) -- saving snapshot to disk
                    
                --getting the name of last image in folder:
                local command1 = "dir /B /O-D /A:-D C:\\Domoticz\\screensfromcams\\cam2floor\\"
                local f = io.popen(command1)
                local last_file1 = string.sub( f:read("*a"):gsub("\n", ""), 1, 24)
                -- end
                
                -- sending image to telegram:
                domoticz.executeShellCommand('curl -s -X POST "https://api.telegram.org/bot'.. tBot  ..'/sendPhoto" -F chat_id='.. tChatId  ..' -F photo="@C:/Domoticz/screensfromcams/cam2floor/'..last_file1..'" -F disable_notification=false -F caption="Motion 2 floor"')

	end