dtgbot - Domoticz TeleGram BOT

Client tools or tools that can connect with Domoticz. Tools for Windows, iOS, Android, Linux etc.

Moderator: leecollings

User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

You probably need to check many more parameters in your Announcement.sh as they are space delimited, so each word is an parameter on its own unless the text was quoted.
In a bash script for example: (untested)

Code: Select all

TEXT=""
for WORD in "$@"
do
 TEXT="$TEXT $WORD"
done
echo "total text:$TEXT"
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

jvdz wrote: Thursday 10 August 2017 10:12 You probably need to check many more parameters in your Announcement.sh as they are space delimited, so each word is an parameter on its own unless the text was quoted.
In a bash script for example: (untested)

Code: Select all

TEXT=""
for WORD in "$@"
do
 TEXT="$TEXT $WORD"
done
echo "total text:$TEXT"
Jos
Yes, true. But actually in current format of dtgbot.lua I cannot pass multiple parameters to the script as it trims away everything else than alphanumerical characters, underscore and hyphen (and åäö as I added those as allowed as well). So the script sees only the trimmed parameter which is constructed from the message text processed in the above described manner. I tested once what happens if I add space as allowed but it wouldn't then execute the script and I had no time to debug further where the issue was.
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

I will check to see whether we should/could change that in case of regular commands and pass the original formatted string after the found command.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

Thanks a lot! The new dtgbot.lua and voice.sh are working perfectly! :) The announcement script is now:

Code: Select all

#!/bin/sh
SendMsgTo=$1
Message=`echo "$@" | cut -d " " -f2-`
ResultString=`curl 'http://'$DomoticzIP':'$DomoticzPort'/json.htm?type=devices&rid=80' 2>/dev/null | jq -r .result[].Data | tr -d ' :%'`
if [ "$ResultString" = "SetLevel10" ] ; then
   Status="InputBd"
   Receiver="On"
elif [ "$ResultString" = "SetLevel20" || "$ResultString" = "SetLevel30" ] ; then
   Status="InputTv"
   Receiver="On"
elif [ "$ResultString" = "SetLevel40" ] ; then
   Status="InputDvd"
   Receiver="On"
else
   Status="PowerOff"
   Receiver="Off"
fi

/bin/bash /home/pi/domoticz/scripts/tts.sh $Status "$Message" $Receiver
curl --data 'chat_id='$SendMsgTo --data-urlencode 'text=The following message was played at home: '"$Message" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
The tts-script takes the receiver target status and the current status as input parameters (which is derived from a selector switch) and it outputs the message.

Then the voice.sh script I modified to:

Code: Select all

#!/bin/sh
#Variables
VoiceFile="/home/pi/scripts/voice.oga"
ResultString=`curl 'http://'$DomoticzIP':'$DomoticzPort'/json.htm?type=devices&rid=80' 2>/dev/null | jq -r .result[].Data | tr -d ' :%'`
if [ "$ResultString" = "SetLevel10" ] ; then
   Status="InputBd"
elif [ "$ResultString" = "SetLevel20" || "$ResultString" = "SetLevel30" ] ; then
   Status="InputTv"
elif [ "$ResultString" = "SetLevel40" ] ; then
   Status="InputDvd"
else
   Status="PowerOff"
fi

#Set the receiver to correct input / volume and wait a bit if it has been off
node /home/pi/harmonyHubCLI/harmonyHubCli.js -l 192.168.11.70 -d Vahvistin -c '["InputSat"]' -m
if [ "$Status" = "PowerOff" ] ; then
   sleep 4
fi
node /home/pi/harmonyHubCLI/harmonyHubCli.js -l 192.168.11.70 -d Vahvistin -c '["VolumeUp","VolumeUp","VolumeUp","VolumeUp","VolumeUp"]' -m

#Fetch the file and play it, then remove
wget -O "$VoiceFile" "https://api.telegram.org/file/bot"'$TelegramBotToken'"/"'$2'"
omxplayer -o hdmi "$VoiceFile"
sudo rm "$VoiceFile"

#Put receiver back to correct input/volume
node /home/pi/harmonyHubCLI/harmonyHubCli.js -l 192.168.11.70 -d Vahvistin -c '["VolumeDown","VolumeDown","VolumeDown","VolumeDown","VolumeDown","'$Status'"]' -m
So thanks again, now I can send announcements as plain text (including spaces) and record something to be played as well...:D
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

Does the text with special character also function correctly?
Lets test this version for a few days after which I will commit the changed to the GITHUB.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by G3rard »

Very interested in this as well. I tried a while ago to send TTS messages to my Sonos with dtgbot, but didn't get working properly.
So I will try this version once it's on Github :D
Not using Domoticz anymore
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

jvdz wrote: Thursday 10 August 2017 23:52 Does the text with special character also function correctly?
Lets test this version for a few days after which I will commit the changed to the GITHUB.

Jos
Yes, at least all characters I've tried so far ( = äåö)
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

G3rard wrote: Thursday 10 August 2017 23:58 So I will try this version once it's on Github :D
Just pushed the update to the Master branch. :-)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

Nautilus wrote: Friday 11 August 2017 0:04 Yes, at least all characters I've tried so far ( = äåö)
It should be supporting all characters now as I am now simply concatenate the raw command given to the shelled script.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

Haven't witnessed any issues so far, thanks again! :)

Already thinking about completing this with adding a similar logic for video files...:D
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

Nautilus wrote: Sunday 13 August 2017 0:36 Haven't witnessed any issues so far, thanks again! :)
You're welcome ;)
Nautilus wrote: Sunday 13 August 2017 0:36 Already thinking about completing this with adding a similar logic for video files...:D
.. and then play that automatically on the bigscreen TV and scare the shit out of the family? :D

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

Exactly! :D
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by G3rard »

jvdz wrote: Friday 11 August 2017 11:35 Just pushed the update to the Master branch. :-)

Jos
Thanks!
Installed the new version, it took me some time to get it working with my Sonos, but now I am able to play typed messages and voice messages :D

Here are the scripts in case someone can reuse (parts of) it.
announcement.sh

Code: Select all

#!/bin/sh
#Can be used to send text message to Sonos
#Example: /announcement "This is a test" 20 office
Msg=$3
Vol=$4
Sonos=$5

if [ "$Msg" == "" ]  ; then 
    Msg="No message found"
fi

#replace spaces with %20
Text=${Msg// /%20}

#if no volume is given the set volume to 5
if [ "$Vol" == "" ] ; then 
    Vol=5
fi

if [ "$Sonos" == "" ] || [ "$Sonos" == "office" ] || [ "$Sonos" == "Office" ] ; then 
    Son=115
    both=false
elif [ "$Sonos" == "kitchen" ] || [ "$Sonos" == "Kitchen" ] ; then 
    Son=116
    both=false
elif [ "$Sonos" == "all" ] || [ "$Sonos" == "both" ] ; then 
    both=true
fi

if [ "$both" == false ] ; then 
    curl --url "http://192.168.1.157/fp/sonos/index.php?zone="$Son"&action=sendMessage&message="$Text"&volume="$Vol
else
    Son=115
    curl --url "http://192.168.1.157/fp/sonos/index.php?zone="$Son"&action=sendMessage&message="$Text"&volume="$Vol
    Son=116
    curl --url "http://192.168.1.157/fp/sonos/index.php?zone="$Son"&action=sendMessage&message="$Text"&volume="$Vol
fi
For voice messages I am using avconv in voice.sh to convert the oga file to mp3, to be able to play this on a Sonos Player.
voice.sh

Code: Select all

#!/bin/sh
#Variables
VoiceFile="/home/gerard/temp/voice.oga"
OutputFile="/var/www/html/fp/sonos/spraak/voice.mp3"

#Fetch the file, convert it and play it
wget -O "$VoiceFile" "https://api.telegram.org/file/bot"$TelegramBotToken"/"$2
avconv -i $VoiceFile -c:a libmp3lame -q:a 2 $OutputFile -y
curl --url "http://192.168.1.157/fp/sonos/index.php?zone=115&action=sendMessage&message=voice&volume=20"
Not using Domoticz anymore
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nautilus »

jvdz wrote: Sunday 13 August 2017 9:24
Nautilus wrote: Sunday 13 August 2017 0:36 Haven't witnessed any issues so far, thanks again! :)
You're welcome ;)
Nautilus wrote: Sunday 13 August 2017 0:36 Already thinking about completing this with adding a similar logic for video files...:D
.. and then play that automatically on the bigscreen TV and scare the shit out of the family? :D

Jos
This was very simple to add after the latest additions to dtgbot.lua. Just add another elseif after msg.voice for msg.video_note and add video_note.sh with appropriate content to process it (format is .mp4 so suggest to name it something like ~/video.mp4). And to the end (near end) of the script add

Code: Select all

(msg ~= nil and (msg.text ~= nil or msg.voice ~= nil))
=>

Code: Select all

(msg ~= nil and (msg.text ~= nil or msg.voice ~= nil or msg.video_note ~= nil))
Now just to tweak the script a bit so that there aren't any text visible from the command line...:D
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

Nice you got it working and understand this should be a trivial change to add. ;-)
Just submit a push request when you want to have it added to the Github master.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nielsch
Posts: 10
Joined: Saturday 19 August 2017 20:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nielsch »

I know the question has been raised before, but I cannot find an answer.
Is it in some way possible to send a message to the bot using a URL?
Reason why I'm asking is that I want Tasker to send a message to switch off a switch in Domoticz whenever I leave home. I don't want to forward ports to Domoticz and I also don't want my VPN connected all the time, so this would be ideal...
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

How is tasker sending telegram messages? Using another BOT or a general Telegram userid?
In the latter case you can without any issue, but sending BOT messages from another bot I don't think is allowed.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Nielsch
Posts: 10
Joined: Saturday 19 August 2017 20:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Nielsch »

Tasker can a.o. do HTTP Get requests; it cannot communicate with Telegram directly.
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by thorbj »

Hi, when sending domoticz notifications from LUA, I don't get the notifications on Telegram, I only gett messages with "-2" as the only content.
I can send commands like /systemstatus and receive the correct values. So the -2 ony occurs when sending notifications.
What can cause this?
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by jvdz »

You will have to explain how you are sending the notification as there isn't an link with the dtgbot part really, but you can use the Telegram bot to send notifications?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests