Page 1 of 1

Domoticz / Telegram bot uses

Posted: Friday 30 July 2021 2:38
by Welsyntoffie
Got my USB webcam sending an image whenever my PIR is tripped...

Bash script... saved as cam.sh inside dtgbot folder

Code: Select all

#!/bin/bash

fswebcam -r 1280x720 /var/tmp/image.jpg


Domotics LUA script...

Code: Select all

commandArray = {}
if (devicechanged['PIR'] == 'On') then
os.execute('/home/pi/dtgbot/cam.sh')
os.execute('curl -s -X POST "https://api.telegram.org/botTOKEN/sendPhoto" -F chat_id=YOURCHATID -F photo="@/var/tmp/image.jpg"')
end
return commandArray
I will add an alarm switch aswell so as to only capture image when alarm on and motion detected.
Check out viewtopic.php?f=21&t=7279 for more Domoticz / Telegram uses.

Re: Domoticz / Telegram bot uses

Posted: Friday 30 July 2021 11:15
by jvdz
Did you see the script I made to send a collage of multiple pictures taken at an interval with Telegram for this purpose? :)

EDIT: Found it.... just adapt it to your needs. This one can send it to 2 different Telegram ID's:

Code: Select all

#!/bin/sh
token="Put here your BOT token without bot prefix"
SendMsgToPerson1="Telegram id person 1"
SendMsgToPerson2="Telegram id person 2"
pausebetween=1
DomoUrl="127.0.0.1:8080"
SnapFile1="/tmp/1.jpg"
SnapFile2="/tmp/2.jpg"
SnapFile3="/tmp/3.jpg"
# get first Picture
wget -O $SnapFile1 $DomoUrl"/camsnapshot.jpg?idx=3"
FTime1=`/bin/date '+%H:%M:%S'`;
sleep $pausebetween
# get second Picture
wget -O $SnapFile2 $DomoUrl"/camsnapshot.jpg?idx=3"
FTime2=`/bin/date '+%H:%M:%S'`;
sleep $pausebetween
# get second Picture
wget -O $SnapFile3 $DomoUrl"/camsnapshot.jpg?idx=3"
FTime3=`/bin/date '+%H:%M:%S'`;
## ---- Telegram -------------------------------------------------------------------------
# Send to Telegram
curl -s \
-X POST "https://api.telegram.org/bot"$token"/sendMediaGroup" \
-F chat_id=$SendMsgToPerson1 \
-F media='[{"type":"photo","media":"attach://photo_2","caption":"'$FTime2'"},{"type":"photo","media":"attach://photo_1","caption":"'$FTime1'"},{"type":"photo","media":"attach://photo_3","caption":"'$FTime3'"}]' \
-F photo_1="@$SnapFile1" \
-F photo_2="@$SnapFile2" \
-F photo_3="@$SnapFile3"
# Send to Person2
curl -s \
-X POST "https://api.telegram.org/bot"$token"/sendMediaGroup" \
-F chat_id=$SendMsgToPerson2 \
-F media='[{"type":"photo","media":"attach://photo_2","caption":"'$FTime2'"},{"type":"photo","media":"attach://photo_1","caption":"'$FTime1'"},{"type":"photo","media":"attach://photo_3","caption":"'$FTime3'"}]' \
-F photo_1="@$SnapFile1" \
-F photo_2="@$SnapFile2" \
-F photo_3="@$SnapFile3"
# remove Pictures
rm $SnapFile1
rm $SnapFile2
rm $SnapFile3

Re: Domoticz / Telegram bot uses

Posted: Friday 30 July 2021 12:22
by Welsyntoffie
jvdz wrote: Friday 30 July 2021 11:15 Did you see the script I made to send a collage of multiple pictures taken at an interval with Telegram for this purpose? :)
This was going to be my next step or research. Thanks for this.

This command however does not work with my USB webcam. I have tried several different syntaxes from google search.
wget -O $SnapFile3 $DomoUrl"/camsnapshot.jpg?idx=3"

But luckily I do have a workaround to get the snapshot.

Re: Domoticz / Telegram bot uses

Posted: Friday 30 July 2021 17:33
by jvdz
My example script retrieves the snapshot from domoticz, not the cam directly, so it could be you need to change the domoticz url to includes https and/or userid/password.
Jos

Re: Domoticz / Telegram bot uses

Posted: Saturday 31 July 2021 3:04
by Welsyntoffie
jvdz wrote: Friday 30 July 2021 17:33 My example script retrieves the snapshot from domoticz, not the cam directly, so it could be you need to change the domoticz url to includes https and/or userid/password.
Jos
I read somewhere that other people also did not get the snapshot from a USB camera. Some got all green snapshot.
Also that the image url should display the image inside the browser and not be downloaded. Mine downloads everytime.

Re: Domoticz / Telegram bot uses

Posted: Saturday 31 July 2021 3:08
by Welsyntoffie
jvdz wrote: Friday 30 July 2021 11:15 Did you see the script I made to send a collage of multiple pictures taken at an interval with Telegram for this purpose? :)
I got your script working 100% to one person. Only have one telegram account in the house now.

Code: Select all

#!/bin/sh
token="XXXXXXXXXX:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SendMsgToPerson1="xxxxxxxxxx"
pausebetween=1
SnapFile1="/var/tmp1/image1.jpg"
SnapFile2="/var/tmp1/image2.jpg"
SnapFile3="/var/tmp1/image3.jpg"
# get first Picture
fswebcam -r 640x480 /var/tmp1/image1.jpg
sleep $pausebetween
# get second Picture
fswebcam -r 640x480 /var/tmp1/image2.jpg
sleep $pausebetween
# get second Picture
fswebcam -r 640x480 /var/tmp1/image3.jpg
## ---- Telegram -------------------------------------------------------------------------
# Send to Telegram
curl -s \
-X POST "https://api.telegram.org/bot"$token"/sendMediaGroup" \
-F chat_id=$SendMsgToPerson1 \
-F media='[{"type":"photo","media":"attach://photo_1"},{"type":"photo","media":"attach://photo_2"},{"type":"photo","media":"attach://photo_3"}]' \
-F photo_1="@$SnapFile1" \
-F photo_2="@$SnapFile2" \
-F photo_3="@$SnapFile3"

# remove Pictures
rm $SnapFile1
rm $SnapFile2
rm $SnapFile3
Need to convince my wife to get telegram so she can also get snapshot updates.

Re: Domoticz / Telegram bot uses

Posted: Sunday 01 August 2021 23:32
by Welsyntoffie
SO arming my system and sending snapshots whenever the PIR gets triggered worked, but I get snapshots every 10 seconds while the motion continues.
I made these modifications to prevent further sending of snapshots for the next 5 minutes...
Seems to be working fine.

Code: Select all

commandArray = {}

if (devicechanged['PIR TV Room'] == 'On' and (uservariables['Send_Once'] == 'Off')) then
	os.execute('/home/pi/dtgbot/cam_collage.sh &') 
	commandArray['Variable:Send_Once'] = "On"
	commandArray['Variable:Send_Once_RESET'] = "Off"
end

if (devicechanged['PIR TV Room'] == 'Off' and (uservariables['Send_Once_RESET'] == 'Off')) then
	commandArray['Variable:Send_Once_RESET'] = "On"
	commandArray['Variable:Send_Once'] = 'Off AFTER 300'
end

return commandArray