Page 19 of 30

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 21 April 2020 13:29
by pesa1234
beamzer wrote: Sunday 28 July 2019 11:48 Ok, this is the complete script where i can read and set domoticz variables.
If someone knows how to do this script in a telegram menu structure, i would be very interested. But at the moment this works for me.

Code: Select all

#!/bin/bash
#
# Ewald 20190728
#
# if you would like to display with index in front, use this:
# mapfile -t varia  < <(curl --silent 'http://domoticz.local:8080/json.htm?type=command&param=getuservariables' | jq -r -c '.result[]| {Name,idx,Value}' | perl -ne '/Name\":\"(\S+?)\".*idx\":\"(\d+).*Value\":\"(\S+?)\"/ && print "\[$2\] $1: $3\n"')
# but display without the index allows for easier copy and paste to set a variable

SendMsgTo=$1

if [ -z "$2" ]; then
	mapfile -t varia  < <(curl --silent 'http://domoticz.local:8080/json.htm?type=command&param=getuservariables' | jq -r -c '.result[]| {Name, Value}' | perl -ne '/Name\":\"(\S+?)\".*Value\":\"(\S+?)\"/ && print "$1 $2\n"')

	for v in "${varia[@]}"
		do
		    if [[ $v != Telegram* ]]; then		# leave out the dtgbot variables that start with Telegram
		# another option to display only the variables ending in Alert is:  [[ $v == *Alert ]]
   			curl --silent --data 'chat_id='$SendMsgTo --data-urlencode 'text='"$v" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
		    fi
		done
	else
		if [[ "$#" -ne 3 ]]; then
                        curl --silent --data 'chat_id='$SendMsgTo --data-urlencode 'text='"Sorry, i need TWO arguments, VariableName and Value" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
                        exit 0
		fi
		VAR=$2; VAL=$3
		if [[ $VAR == Telegram* ]]; then
			curl --silent --data 'chat_id='$SendMsgTo --data-urlencode 'text='"Sorry, no changing Telegram variables" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
			exit 0
		fi
		CHECK=$(curl --silent "http://"$DomoticzIP":"$DomoticzPort"/json.htm?type=command&param=updateuservariable&vname="$2"&vtype=0&vvalue="$3 | jq -r '.status')
		if [[ $CHECK == "OK" ]]; then
			curl --silent --data 'chat_id='$SendMsgTo --data-urlencode 'text='"$VAR = $VAL" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
		else
			curl --silent --data 'chat_id='$SendMsgTo --data-urlencode 'text='"Oops, that didn't work" 'https://api.telegram.org/bot'$TelegramBotToken'/sendMessage'
		fi
	fi
	
Usage:
the script is named varia.sh and put into dtgbot/bash. So entering varia in Telegram will give you all the variables except for the dtgbot specific variables. I left those out because they are security related, especially the TelegramBotWhiteListedIDs. The script doesn't allow you to change those either.
Using varia variablename value you can change the value of a specific variable.
Thank you! Would be great if available on standard

In my case I use variable for playlist of media player and I change it to:

Code: Select all

CHECK=$(curl --silent "http://"$DomoticzIP":"$DomoticzPort"/json.htm?type=command&param=updateuservariable&vname="$2"&vtype=String&vvalue="$3 | jq -r '.status')
		

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 16:53
by hoeby
I did a fresh install on my new sd-card, after having a corrupt section on the card.
Run all updates (the one i know) and domoticz works.
Running on e Pi4 with the latest buster (release 2020-05-27)

But dgtbot :roll:
I have followed the instruction on the wiki

When i give the command

Code: Select all

sudo service dtgbot status
Then i get the following result, looks good i think, because it is active

Code: Select all

pi@raspberrypi:~ $ sudo service dtgbot status
● dtgbot.service - LSB: A Telegram bot designed to control Domoticz
   Loaded: loaded (/etc/init.d/dtgbot; generated)
   Active: active (exited) since Wed 2020-08-19 15:38:31 BST; 57s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1039 ExecStart=/etc/init.d/dtgbot start (code=exited, status=0/SUCCES

Aug 19 15:38:31 raspberrypi systemd[1]: Starting LSB: A Telegram bot designed to
Aug 19 15:38:31 raspberrypi dtgbot[1039]: Starting Telegram Bot for Domoticz ...
Aug 19 15:38:31 raspberrypi systemd[1]: Started LSB: A Telegram bot designed to

When giving this command

Code: Select all

sudo tail -f /var/log/monit.log
I get the following result, which doens't look good.

Code: Select all

'
[BST Aug 19 15:43:30] error    : 'dtgbot' failed to start (exit status 0) -- no output
[BST Aug 19 15:45:30] error    : 'dtgbot' process is not running
[BST Aug 19 15:45:30] info     : 'dtgbot' trying to restart
[BST Aug 19 15:45:30] info     : 'dtgbot' start: '/home/pi/dtgbot/restartbot.sh'
[BST Aug 19 15:45:51] error    : 'dtgbot' failed to start (exit status 0) -- no output
[BST Aug 19 15:47:51] error    : 'dtgbot' process is not running
[BST Aug 19 15:47:51] info     : 'dtgbot' trying to restart
[BST Aug 19 15:47:51] info     : 'dtgbot' start: '/home/pi/dtgbot/restartbot.sh'
[BST Aug 19 15:48:12] error    : 'dtgbot' failed to start (exit status 0) -- no output
This is my monit config.
I changed the pad for dtgbot.pid from /var/run/dtgbot.pid to /run/dtgbot.pid
When i go to the /var directory i see a shortcut to /run directory.
The /var and /run are in the same root, therefor changed it. Also tried to add it /var, but error remains.
What am i missing here :?:

Code: Select all

# Monitor the DTGBOT Service
 check process dtgbot with pidfile /run/dtgbot.pid
   start program = "/home/pi/dtgbot/restartbot.sh" timeout 20 seconds
   stop program = "home/pi/dtgbot/stopbot.sh" timeout 20 seconds
   if 5 restart within 5 cycles then timeout

# Monitor the DTGBOT Loop file which should be updated each minute
# Restart DTGBOT when not updated in 2 minutes
 check file dtgloop with path /var/tmp/dtgloop.txt
   start program = "/home/pi/dtgbot/restartbot.sh" timeout 20 seconds
   if timestamp > 2 minutes then restart
 if 5 restart within 5 cycles then timeout
The log files say this
dtb.log
I mis the chat ID in de list, is this correct :?:

Code: Select all

2020-08-19 15:54:45 - DomoticzIP: 127.0.0.1
2020-08-19 15:54:45 - DomoticzPort: 8080
2020-08-19 15:54:45 - BotHomePath: /home/pi/dtgbot/
2020-08-19 15:54:45 - TempFileDir: /var/tmp/
2020-08-19 15:54:45 - BotLuaScriptPath: /home/pi/dtgbot/lua/
2020-08-19 15:54:45 - BotBashScriptPath: /home/pi/dtgbot/bash/
2020-08-19 15:54:45 - TelegramBotToken: 12345678:123456789012345667
2020-08-19 15:54:45 - TelegramBotOffset: TelegramBotOffset
2020-08-19 15:54:45 - -----------------------------------------
2020-08-19 15:54:45 - Starting Telegram api Bot message handler
2020-08-19 15:54:45 - -----------------------------------------
dtb.log.errors
Checked for the notification that it says "No such file or directory", i can say, the file is there.
If what is in the ssl.so written is correct, that i don't know

Code: Select all

/usr/bin/lua5.2: error loading module 'ssl.core' from file '/usr/local/lib/lua/5.2/ssl.so':
	libssl.so.1.0.0: cannot open shared object file: No such file or directory
stack traceback:
	[C]: in ?
	[C]: in function 'require'
	/usr/local/share/lua/5.2/ssl.lua:7: in main chunk
	[C]: in function 'require'
	/usr/local/share/lua/5.2/ssl/https.lua:10: in main chunk
	[C]: in function 'require'
	/home/pi/dtgbot/dtgbot.lua:132: in main chunk
	[C]: in ?
dtgloop

Code: Select all

19/08/20 15:50:12 restartbot.sh started  (2)
19/08/20 15:50:12  Restarting
19/08/20 15:50:28  Restart done
19/08/20 15:54:29 restartbot.sh started  (2)
19/08/20 15:54:29  Restarting
19/08/20 15:54:45  Restart done
19/08/20 15:58:45 restartbot.sh started  (2)
19/08/20 15:58:45  Restarting
19/08/20 15:59:02  Restart done
What i also missing in the wiki, is how to do a propper test.
What and how to test, to know if the install is done correctly :?:

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 17:13
by jvdz
Making it a service and adding it to MONIT is something for later. First priority is to get the basic bot running.
hoeby wrote: Wednesday 19 August 2020 16:53 dtb.log.errors
Checked for the notification that it says "No such file or directory", i can say, the file is there.
If what is in the ssl.so written is correct, that i don't know

Code: Select all

/usr/bin/lua5.2: error loading module 'ssl.core' from file '/usr/local/lib/lua/5.2/ssl.so':
	libssl.so.1.0.0: cannot open shared object file: No such file or directory
It seems you have a similar issue as others had before: https://www.domoticz.com/forum/viewtopi ... .0#p203049
I am still running an "older" setup so can test this myself, but that link should help solving this issue.
Will do a some testing later on a VM with the latest debian and domoticz to see what the steps are with a new build these days.
(could take a couple of days though!)

Jos

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 18:23
by hoeby
It solved the issue to add the archive and install the libssl1.0.0

dtb.log has found commands
dtb.log.errors is empty
dtgloop is stopped with all those restarting rows

monit now also works without issues

Code: Select all

[BST Aug 19 17:09:00] info     : 'raspberrypi' Monit 5.25.2 started
[BST Aug 19 17:09:00] error    : 'domoticz' process is not running
[BST Aug 19 17:09:00] info     : 'domoticz' trying to restart
[BST Aug 19 17:09:00] info     : 'domoticz' start: '/etc/init.d/domoticz.sh start'
[BST Aug 19 17:09:02] error    : 'dtgloop' file doesn't exist
[BST Aug 19 17:09:02] info     : 'dtgloop' trying to restart
[BST Aug 19 17:09:02] info     : 'dtgloop' start: '/home/pi/dtgbot/restartbot.sh'
[BST Aug 19 17:09:22] error    : 'dtgloop' failed to start (exit status -1) -- Program '/home/pi/dtgbot/restartbot.sh' timed out after 20 s
[BST Aug 19 17:11:36] info     : 'domoticz' process is running with pid 590
[BST Aug 19 17:11:36] info     : 'dtgloop' file exists
doing command "dgtmenu" gives me a menu in telegram, not all working (yet).
The tempall also works and gives me the available temperatures.

Now have to figure out how those bash/lua scripts work to make something i am looking for.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 19:09
by jvdz
hoeby wrote: Wednesday 19 August 2020 18:23 Now have to figure out how those bash/lua scripts work to make something i am looking for.
Great you have it working!

The Wiki explains how the bach/lua scripts are fired by DTGBOT. Just give the filename an unique name and that will also be the command.

Jos

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 20:44
by hoeby
jvdz wrote: Wednesday 19 August 2020 19:09The Wiki explains how the bash/lua scripts are fired by DTGBOT. Just give the filename an unique name and that will also be the command.
That is easier said then done.
I never worked with bash or lua like this.
Only very small script with a couples of lines. This is some stuff from an other world.

When i first look at them i don't understand them.
Maybe i need to take one and trying to modify it to what i am looking for. :?

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 22:11
by hoeby
I looked over and over. But don't know how to start further with the script.
Does somebody have a great link, where to get information to get this done. I don't find it in de wiki's

What do i want to make:
- Doorbell is pushed (idx number = on) and a image with capture text is send.
- But also a replykeyboard is send, with the questions "would like a new image" and "no new image needed"
- When chosen "would like a new image", then a new image is taken and send. And again the replykeyboard is added
- When chosen "now new imaged needed" then all replykeyboard questions are removed (deletemessage by message_id)
- When nothing is chosen, after 60 seconds timer the replykeyboard is removed.

At the moment i have this working. But not with dtgbot.
I have it working with a dzvents script, which send the image and replykeyboard question.
To have something to respond on the choice you made, i have no a esp with universal telegram code on it.
The deletemessage is also done by the dzvents script.

This evening i also noted that 2 active devices which respons on telegram messages is not a good idea.
They do not know all commands of each other. Sometimes nothing happens, but that is because the wrong devices answers the telegram message. Therefor i hope to go with dtgbot, to getting everyhing for domoticz in 1 device and not 2.

This script from the wiki answer option 1, capture and send an image.
But how to add those other options?

Code: Select all

#!/bin/sh
SendTo=$1
#################################################################
IP=$2                                     # IP address Camera
##########################################################
SnapFile=$TempFileDir"snapshot.jpg"
$TelegramScript msg $SendTo Er is zojuist aangebeld!
if ping -c 1 $IP > /dev/null ; then  # if IPCAM is online then:
(su - pi -c "wget "$DomoticzIP":"$DomoticzPort"/camsnapshot.jpg?idx=1 && sudo mv camsnapshot.jpg?idx=1 "$SnapFile)&
$TelegramScript msg $SendTo Kijk wie er aangebeld heeft: 
sleep 1
$TelegramScript send_photo $SendTo $SnapFile 
/bin/rm $SnapFile        
else
$TelegramScript msg $SendTo IP-cam niet beschikbaar.  
fi

Re: dtgbot - Domoticz TeleGram BOT

Posted: Wednesday 19 August 2020 22:54
by jvdz
Lets see if the whole process is clear:
1. When the doorbell is pushed a script is triggered which sends a Telegram with images and a Keyboard with options "Yes" & "No". (Makes sense!)
2. So when you press "Yes", you will respond to DTGBOT the command "Yes", so how would DTGBOT know what it is about? (Here lies the issue)
So when you change the buttontext to something like "Meer_Fotos" and "Geen_Fotos" and you create a sh or lua script with that exact name, it will be ran by DTGBOT when these buttons are pressed and can perform that wanted task..
When you use the Inline keyboard option you can define the response to be send to the BOT when "Yes" is pressed, which makes it easier to handle that, as you could define command to be run.
Does that make sense?

Jos

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 8:30
by hoeby
The text of the buttons is not the issue at the moment.

When reading your text. I do not need to try to make it in 1 script, but build multi scripts to get the same result.

I will try to make a start with multi scripts

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 9:50
by jvdz
hoeby wrote: Thursday 20 August 2020 8:30 When reading your text. I do not need to try to make it in 1 script, but build multi scripts to get the same result.
Correct, Domoticz will send the Images and the initial Keyboard with the script that is activated after the doorbell is pressed. Then when you press one of the buttons on this custom keyboard in Telegram, you send that command to the BOT which will be read by DTGBOT and DTGBOT will perform the task belonging to the send command.

Jos

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 10:02
by hoeby
Got it done, but not how you meant it to work (i think).

I left my dzvents script how it is.
This sends the image and initial keyboard.

The answer which is chosen, is handled by the bot.
Choice "yes_more_pictures" send a customevent to domoticz, to retrigger the dzvents script.
Choice "no_more_pictures" sends nothing, it sleeps for 1 sec. But this needs to be made, to not get the error "invalid command" from dtgbot.
Unfortunally it doesn't work without the underscore between it, because it otherwise can't find the .sh files.
In the esp universal telegram it looks at the text it receives and executes something. In the log files it looks like dtgbot also can receive the text without the underscores, but after that it can't find the .sh files
Is there a work around to get it work without the underscores, or is this that deep in the code that it is a problem.
A workaround would be to use inline instead of reply. But i like the reply buttons more than the inline buttons.

Maybe there are other and better ways to do this. But i am happy with my first customize scripts for dtgbot.

Changed the dgtbot.lua to this.
Because i didn't like the "done" answers. Can i do this without getting in throubles?
I am aware, that when i do an update of dtgbot in the future, that i need to redo this.

Code: Select all

    if channelmsg then
--      send_msg(Group,"done",MessageId)
    elseif Group ~= "" then
--      send_msg(Group,"done",MessageId,replymarkup)
    else
--      send_msg(SendTo,"done",MessageId,replymarkup)
    end

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 10:37
by jvdz
Sounds like you made it exactly as I (tried) to describe. :)
There is no simple way for replacing the underscore with spaces as that would change a one-word command like "yes_more_pictures" into a command "yes" with 2 parameters (more, pictures), but I guess it should work when you make a "yes.sh" or "yes.lua" and read those parameters so you can decide what to do with them.
No issue with remaking the "done" message and maybe that should be configurable anyways but is there to provide feedback that the command was performed when e.g. switching on a light or something.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 12:31
by hoeby
Is it possible when the command "device" that you can exclude some devices in the answer?

Now it is a long list, i know you can switch with just the number and command. But it makes it easier (for me) to only see the device which are switchable

Re: dtgbot - Domoticz TeleGram BOT

Posted: Thursday 20 August 2020 16:26
by jvdz
The standard script provided allows to filter on a string eg: "devices xyz"
This would list all devices starting with these 3 letters.

You can always make your own version of any of these scripts. :)

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 10:36
by hoeby
I thoughed i did read it somewhere, but can't find it anymore.

In the wiki is written, that the command and file name need to have exactly the same name.
I made a curtain.sh, curtain_open.sh and curtain_close.sh

When i give the command curtain
Then all 3 files are executed. Looks like it does curtain+wildcard
Also when given the command cur then i executeds the commands

Can i disable this?
That is does only does the command that is written and not all commands that look like it.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 11:11
by jvdz
hoeby wrote: Saturday 22 August 2020 10:36 Can i disable this?
That is does only does the command that is written and not all commands that look like it.
Think that is indeed how Simon coded this looking at line 349 in dtgbot.lua:

Code: Select all

        if(line:match(cmda)) then
Try changing it to:

Code: Select all

        if (line==cmda) then
.. which I guess should make it exact match but haven't tested it.

Jos

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 11:43
by hoeby
jvdz wrote: Saturday 22 August 2020 11:11
hoeby wrote: Saturday 22 August 2020 10:36 Can i disable this?
That is does only does the command that is written and not all commands that look like it.
Think that is indeed how Simon coded this looking at line 349 in dtgbot.lua:

Code: Select all

        if(line:match(cmda)) then
Try changing it to:

Code: Select all

        if (line==cmda) then
.. which I guess should make it exact match but haven't tested it.

Jos
I found the command on line 359.
Changed it and restarted the dgtbot service (with +60seconds wait in between).
All command than don't work anymore.

Put it back how it was, then it runs again

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 11:51
by jvdz
Ok... will have a look tomorrow when I will be able to test first. :)

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 12:13
by hoeby
Thanks Jos.

Some extra info:
It is only the bash scripts where it doesn't work, all bash scripts don't work after changing the dtgbot.lua

When test with command for a lua script. These work.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Saturday 22 August 2020 12:35
by jvdz
It is correct that only bash doesn't work as that is the only part that is changed for the moment as that is what you used.
Think I know why it doesn't work. the variable line will contain the full scriptname "xyz.sh" so that test should be:

Code: Select all

 if (line==cmda..".sh") then
... but this is still untested. ;)