Page 1 of 30

dtgbot - Domoticz TeleGram BOT

Posted: Monday 13 July 2015 21:49
by simonrg
While telegram-cli is a very useful telegram client for Linux, Telegram have now released a new bot api, which makes it possible to send notifications without installing any Telegram software using https requests (http://www.domoticz.com/wiki/Telegram_Bot) and to connect Telgram to Domoticz more directly.

I have created a new handler dtgbot which builds on the previous bots (receive.lua for telegram-cli and the XMPP bot for Google Hangouts), providing the same functionality (http://www.domoticz.com/wiki/Telegram_B ... ctionality)

dtgbot requires installation of a number of Lua libraries which are attached in the two tar.gz files and installation of its own scripts which are attached in the tar file - I have detailed the installation in this Wiki page - http://www.domoticz.com/wiki/Remote_Con ... legram_Bot - however first you need to complete the actions described on the Telegram Bot page to create your own Telegram Bot for dtgbot to use - http://www.domoticz.com/wiki/Remote_Con ... legram_Bot.

If you have already installed telegram-cli then you do not need to remove or change anything (apart from DomoticzData.sh) in order to use dtgbot, in fact both can be used simultaneously.

Currently this is very much alpha software, however once it is stable then I suggest we depricate the exisiting Installing Telegram Notification System pages, in favour of this approach as it should be far more supportable (https://www.domoticz.com/wiki/Installin ... ion_System)

Comments / improvements / suggestions most welcome.

Edit: Updated dtgbot.tar - based on feedback and added extra log file to capture Lua errors - by default will be /var/tmp/dtb.log.errors

Re: dtgbot - Domoticz TeleGram BOT

Posted: Monday 13 July 2015 23:24
by G3rard
Great! Will certainly try this as I still have some issues with Telegram-CLI (only resolved after rebooting the Pi).

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 8:16
by fransiefrans
Thanks for the tutorial!
sadly im not able to get dtg bot to work.
When I do a status check I get: [FAIL] dtgbot is not running ... failed!


I don't have the following folder: cd /usr/local/share/lua/5.2/ and cd /usr/local/share/lua/5.2/

Code: Select all

cd /usr/local/share/lua/5.2/
sudo tar -xvf ~/temp/usrlocalsharelua52.tar.gz
cd /usr/local/lib/lua/5.2/
sudo tar -xvf ~/temp/usrlocalliblua52.tar.gz
I created them myself.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 8:35
by simonrg
fransiefrans wrote:Thanks for the tutorial!
sadly im not able to get dtg bot to work.
When I do a status check I get: [FAIL] dtgbot is not running ... failed!


I don't have the following folder: cd /usr/local/share/lua/5.2/

Code: Select all

cd /usr/local/share/lua/5.2/
sudo tar -xvf ~/temp/usrlocalsharelua52.tar.gz
cd /usr/local/lib/lua/5.2/
sudo tar -xvf ~/temp/usrlocalliblua52.tar.gz
Do I need this one?
Yes you need the directories, so I have changed the wiki to:

Code: Select all

sudo mkdir /usr/local/share/lua/5.2/ #Only needed if this directory does not exist
cd /usr/local/share/lua/5.2/
sudo tar -xvf ~/temp/usrlocalsharelua52.tar.gz
sudo mkdir /usr/local/lib/lua/5.2/ #Only needed if this directory does not exist
cd /usr/local/lib/lua/5.2/
sudo tar -xvf ~/temp/usrlocalliblua52.tar.gz
If dtgbot fails then you can find the reason why in its log file - dtb.log - which will be in /var/tmp unless you have a different temporary directory.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 8:49
by fransiefrans
This is my log

Code: Select all

---------------------------------
Starting Telegram message handler
---------------------------------
DomoticzIP: 127.0.0.1
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: Correct token
TelegramBotOffset: TelegramBotOffset
Loading command modules...
Loading module <list>
found command <dump>
function: 0x1af9b50
found command <list>
function: 0x1af9b50
Loading module <devices>
found command <devices>
function: 0x1afd118
found command <scenes>
function: 0x1afd118
Loading module <flick>
found command <flick>
function: 0x1b0eef0
Loading module <nflick>
found command <nflick>
function: 0x1b0c5a8
Loading module <on>
found command <off>
function: 0x1afdd40
found command <on>
function: 0x1afdd40
Loading module <battery>
found command <battery>
function: 0x1b3c020
found command <batteries>
function: 0x1b3c020
Loading module <help>
found command <help>
function: 0x1b3cfa8
JSON request <http://127.0.0.1:8080/json.htm?type=command&param=getuservariables>
Domoticz returned getuservariables after 1 attempts

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 9:07
by simonrg
fransiefrans wrote:This is my log

Code: Select all

Domoticz returned getuservariables after 1 attempts
It looks like you haven't created a user variable in Domoticz - TelegramBotOffset - http://www.domoticz.com/wiki/Remote_Con ... r_Variable.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 10:58
by thorbj
@fransiefrans, try to replace DomoticzIP: 127.0.0.1 with the internal IP of your server running Domoticz and dtgbot. i.e. DomoticzIP: 192.168.149.52...

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 11:11
by thorbj
Thanks for the great job getting this to work with Domoticz, @simonrg!

It worked great for me, until I issued the command "battery". This makes the dtgbot-service crash. See log below.
Have I done something wrong here? should I add more parameters to the command, or should I edit the battery.lua file somehow?

I try to issue sudo service dtgbot start, but it just crashes again immideately. How can I clear the command-queue so that the service don't crash again?

Thanks!

Log:

Code: Select all

---------------------------------
Starting Telegram message handler
---------------------------------
DomoticzIP: 10.10.10.127
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: keykeykyekye
TelegramBotOffset: TelegramBotOffset
Loading command modules...
Loading module <list>
found command <list>
function: 0x1e279a8
found command <dump>
function: 0x1e279a8
Loading module <devices>
found command <scenes>
function: 0x1e163c0
found command <devices>
function: 0x1e163c0
Loading module <flick>
found command <flick>
function: 0x1e5b4b0
Loading module <nflick>
found command <nflick>
function: 0x1e44638
Loading module <on>
found command <on>
function: 0x1e57f90
found command <off>
function: 0x1e57f90
Loading module <battery>
found command <battery>
function: 0x1e2fb80
found command <batteries>
function: 0x1e2fb80
Loading module <help>
found command <help>
function: 0x1e56c60
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariables>
Domoticz returned getuservariables after 1 attempts
1
TBOidx 1
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariable&idx=1>
Decoded 277849972
TBO 277849972
https://api.telegram.org/botkeykeykeykey/
.Message: 1
update_id 	277849972
Battery
Handle command function started with Battery and IDIDIDIDI
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariables>

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 11:57
by simonrg
thorbj wrote:Thanks for the great job getting this to work with Domoticz, @simonrg!

It worked great for me, until I issued the command "battery". This makes the dtgbot-service crash. See log below.
Have I done something wrong here? should I add more parameters to the command, or should I edit the battery.lua file somehow?

I try to issue sudo service dtgbot start, but it just crashes again immideately. How can I clear the command-queue so that the service don't crash again?

Thanks!

Log:

Code: Select all

---------------------------------
Starting Telegram message handler
---------------------------------
DomoticzIP: 10.10.10.127
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: keykeykyekye
TelegramBotOffset: TelegramBotOffset
Loading command modules...
Loading module <list>
found command <list>
function: 0x1e279a8
found command <dump>
function: 0x1e279a8
Loading module <devices>
found command <scenes>
function: 0x1e163c0
found command <devices>
function: 0x1e163c0
Loading module <flick>
found command <flick>
function: 0x1e5b4b0
Loading module <nflick>
found command <nflick>
function: 0x1e44638
Loading module <on>
found command <on>
function: 0x1e57f90
found command <off>
function: 0x1e57f90
Loading module <battery>
found command <battery>
function: 0x1e2fb80
found command <batteries>
function: 0x1e2fb80
Loading module <help>
found command <help>
function: 0x1e56c60
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariables>
Domoticz returned getuservariables after 1 attempts
1
TBOidx 1
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariable&idx=1>
Decoded 277849972
TBO 277849972
https://api.telegram.org/botkeykeykeykey/
.Message: 1
update_id 	277849972
Battery
Handle command function started with Battery and IDIDIDIDI
JSON request <http://10.10.10.127:8080/json.htm?type=command&param=getuservariables>
To get dtgbot to work again, you need to set TelegramBotOffset to 1 higher than the current update id, i.e. 277849973, this stops dtgbot from re-reading the message causing the error.

The error is caused because I am expecting you to have a Domoticz variable called DevicesWithBatteries which should contain A Device|Another Device|Yet Another Device, this is one of the many bits that needs documenting and looking at the code tidying up as well.

As I capture errors I am updating the wiki pages and modifying the code.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:32
by thorbj
Thank you @simonrg!
So I should create a new user variable named "DevicesWithBatteries" and add devices in a string here? by idx or name?

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:37
by simonrg
thorbj wrote:Thank you @simonrg!
So I should create a new user variable named "DevicesWithBatteries" and add devices in a string here? by idx og name?
Names not idxs, the single user variable DevicesWithBatteries set in Domoticz with the name of each device seperated by a | i.e. stored as "name one|name 2|name 3|name Forty one" with correct case and spacing.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:41
by fransiefrans
still no luck after adding user variable.
Also tried changing ip to network ip
when I open the Json request in the logfile I do get the following:

{
"result" : [
{
"LastUpdate" : "2015-07-14 12:23:42",
"Name" : "TelegramBotOffset",
"Type" : "0",
"Value" : "1",
"idx" : "1"
}
],
"status" : "OK",
"title" : "GetUserVariables"
}

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:45
by thorbj
simonrg wrote:
thorbj wrote:Thank you @simonrg!
So I should create a new user variable named "DevicesWithBatteries" and add devices in a string here? by idx og name?
Names not idxs, the single user variable DevicesWithBatteries set in Domoticz with the name of each device seperated by a | i.e. stored as "name one|name 2|name 3|name Forty one" with correct case and spacing.
Thank you, that worked! :)
I noticed I can only have one instance of each device in the variable. Like if I have a device that has both a Motion Sensor, Lux sensor and a Temp sensor, I can not put "Motion Sensor | Lux sensor | Temp sensor" in the variable-string. Is this correct?

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:53
by fransiefrans
Hi,

in the following step of the instrucion:
As Lua functions run by Domoticz are run within Domoticz, the environment variables defined in DomoticzData.sh need to be available to Domoticz, add these two lines into /etc/init.d/domoticz.sh, just before #Exit

Do I have the added lines in the correct place?

#! /bin/sh
### BEGIN INIT INFO
# Provides: domoticz
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Home Automation System
# Description: This daemon will start the Domoticz Home Automation Sy$
### END INIT INFO

# Do NOT "set -e"

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Domoticz Home Automation System"
NAME=domoticz
USERNAME=pi
DAEMON=/home/$USERNAME/domoticz/$NAME
#DAEMON_ARGS="-daemon -www 8080 -sslwww 443 -log /tmp/domoticz.txt"
#DAEMON_ARGS="-daemon -www 8080 -sslwww 443 -syslog"
DAEMON_ARGS="-daemon -www 8080 -sslwww 443"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Get the preset variables so that they are available for Lua functions wit$
. /etc/profile.d/DomoticzData.sh


# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --chuid $USERNAME --start --quiet --pidfile $PIDF$
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON$
$DAEMON_ARGS \
|| return 2
}

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 12:55
by simonrg
fransiefrans wrote:Do I have the added lines in the correct place?
Yes that is the right place.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 17:45
by Jan-
Hello,

I have to chance the curl to send the photo by lua. Can someone help me with this?

os.execute('curl -s -X POST "https://api.telegram.org/bot3333333:key ... /sendPhoto" -F chat_id=123456789 -F photo="@/home/pi/domoticz/camera/snapshot.jpg" ')

Something like this:
commandArray = {}
token = "0000000:KEYKEYKEYKEYKEYKEY"
chatid = 12345678
if (devicechanged['Test'] == 'On') then
os.execute('curl --data chat_id='..chatid..' --data-urlencode "text="'..message..'" "https://api.telegram.org/bot'..token..'/SendPhoto" ')
end
return commandArray


Thanks,
Regards,
Jan.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 18:48
by simonrg
thorbj wrote: I noticed I can only have one instance of each device in the variable. Like if I have a device that has both a Motion Sensor, Lux sensor and a Temp sensor, I can not put "Motion Sensor | Lux sensor | Temp sensor" in the variable-string. Is this correct?
I don't think it would mind if you had Motion Sensor|Lux Sensor|Temp Sensor as the contents of DevicesWithBatteries as long as they all return idx values, then the query for battery status should work, why not have a go.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 21:35
by fransiefrans
Started with a new domoticz img, restored the backup. tried the tutorial again with no luck. I'm getting the following fault in the log.

Code: Select all

Starting Telegram message handler
---------------------------------
DomoticzIP: 192.168.2.7
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: key
TelegramBotOffset: TelegramBotOffset

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 22:28
by simonrg
fransiefrans wrote:Started with a new domoticz img, restored the backup. tried the tutorial again with no luck. I'm getting the following fault in the log.

Code: Select all

Starting Telegram message handler
---------------------------------
DomoticzIP: 192.168.2.7
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: key
TelegramBotOffset: TelegramBotOffset
If it is crashing there then not sure what is wrong.

On the command line try:

Code: Select all

cd ~/dtgbot
lua dtgbot.lua
Hopefully you will get a lua error.

Re: dtgbot - Domoticz TeleGram BOT

Posted: Tuesday 14 July 2015 22:53
by Asa67
fransiefrans wrote:Started with a new domoticz img, restored the backup. tried the tutorial again with no luck. I'm getting the following fault in the log.

Code: Select all

Starting Telegram message handler
---------------------------------
DomoticzIP: 192.168.2.7
DomoticzPort: 8080
BotHomePath: /home/pi/dtgbot/
BotLuaScriptPath: /home/pi/dtgbot/lua/
BotBashScriptPath: /home/pi/dtgbot/bash/
TelegramBotToken: key
TelegramBotOffset: TelegramBotOffset
Hi Frans,

You problably (just like me) do not have lua installed:

From: https://www.domoticz.com/wiki/Installin ... ion_System

Code: Select all

sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev lua-lgi glib-2.0 make git-core libjansson-dev
Don't know if all the packages are needed but it did the trick for me.

@simonrg, thanks for your work !! and maybe you can add the right packages in your - otherwise good - wiki

Greetz Erwin