Page 13 of 30

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 10:12
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 10:27
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.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 10:46
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 23:34
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 23:52
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 10 August 2017 23:58
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Friday 11 August 2017 0:04
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 ( = äåö)

Re: dtgbot - Domoticz TeleGram BOT

Posted: Friday 11 August 2017 11:35
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Friday 11 August 2017 11:36
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Sunday 13 August 2017 0:36
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Sunday 13 August 2017 9:24
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Sunday 13 August 2017 9:46
by Nautilus
Exactly! :D

Re: dtgbot - Domoticz TeleGram BOT

Posted: Sunday 13 August 2017 14:49
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"

Re: dtgbot - Domoticz TeleGram BOT

Posted: Monday 14 August 2017 14:29
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Monday 14 August 2017 14:48
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 19 August 2017 20:56
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...

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 19 August 2017 22:17
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

Re: dtgbot - Domoticz TeleGram BOT

Posted: Sunday 20 August 2017 19:59
by Nielsch
Tasker can a.o. do HTTP Get requests; it cannot communicate with Telegram directly.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 05 October 2017 22:55
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?

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 05 October 2017 23:02
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