dtgbot - Domoticz TeleGram BOT

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

Moderator: leecollings

pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

dtgbot - Domoticz TeleGram BOT

Post by pvm »

The reason for asking is that it easier to use the standard supplied version of lua instead of the newer one. I did not think about integrating it in Domoticz itself. Which might not be a bad idea :) so I guess5.2 is needed if I understand your message correctly, then it might be a good idea to introduce this version in the default image for Domoticz.
Gizmocuz,what do you think about this?

(I will give the install a try tomorrow, it is not a hard thing to do)
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

pvm wrote:The reason for asking is that it easier to use the standard supplied version of lua instead of the newer one.
I thought 5.2 was now the standard version in Raspbian Wheezy.

Sorry I forgot 5.2 has always been the version in Domoticz.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by pvm »

Just to be sure I checked on my spare PI:
I downloaded the latest sd image from http://sourceforge.net/projects/domotic ... z/download as mentioned on the wiki

and performed
sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
sudo reboot
(2 times, just to be sure )
and got lua 5.1.5
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

Sorry, I must be wrong about Raspbian, I will add 5.2 as a pre-requisite on the Wiki.

I must have updated it to make it consistent with Domoticz.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by pvm »

I added the dependency to JQ on the wiki, this is needed for the temperature script (maybe better to rewrite to lua script?)

Code: Select all

#install JQ
wget http://stedolan.github.io/jq/download/source/jq-1.4.tar.gz
tar -zxvf jq-1.4.tar.gz
rm jq-1.4.tar.gz
cd jq-1.4
./configure
make
sudo make install
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by pvm »

Nice work! I like the removed dependency on the telegram-cli software (which also had changes outside of our scope causing some problems before)


Non major thingy: detected a non-fool-proof thing, sending "flick" (only this word) results in :

Code: Select all

/usr/bin/lua: /home/pi/dtgbot/dtgbot.lua:316: bad argument #1 to 'len' (string expected, got nil)
stack traceback:
        [C]: in function 'len'
        /home/pi/dtgbot/dtgbot.lua:316: in function 'HandleCommand'
        /home/pi/dtgbot/dtgbot.lua:375: in function 'on_msg_receive'
        /home/pi/dtgbot/dtgbot.lua:440: in main chunk
        [C]: in ?
same for sending "scenes" (ahhh... i do not have any scenes on my test environment... after adding a scene it does work, however some defensive programming is missing)

Code: Select all

/usr/bin/lua: /home/pi/dtgbot/lua//devices.lua:19: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
        [C]: in function 'pairs'
        /home/pi/dtgbot/lua//devices.lua:19: in function 'DevicesScenes'
        /home/pi/dtgbot/lua//devices.lua:45: in function 'handler'
        /home/pi/dtgbot/dtgbot.lua:291: in function 'HandleCommand'
        /home/pi/dtgbot/dtgbot.lua:375: in function 'on_msg_receive'
        /home/pi/dtgbot/dtgbot.lua:440: in main chunk
        [C]: in ?
hmmm... looking at this post... i only installed lua5.2 and none of the other libraries (on my clean domoticz install). however i do not see a direct relation. Other commands like 'list' do work. So the whole list of dependencies mentioned there are not needed, only lua5.2
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
Heisenberg
Posts: 135
Joined: Monday 27 January 2014 14:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Heisenberg »

I still can't get the BOT running :(

Code: Select all

pi@domoticzpi ~ $ sudo service dtgbot status
[FAIL] dtgbot is not running ... failed!
I have set the user variables in Domoticz:
Image
Raspberry Pi Type B
Raspberry Pi Type B2
RFXCOMtrx433e
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 »

@Simonrg, is there a way to change the scripts in the LUA folder so these send the message to TelegramChatId as defined in DomoticzData.sh?
Now it's sending the respons to the person who has sent the message to the group and I want the bot to send the answer to the group.

I have this working for the bash scripts, by changing Send(Msg)To. Should I make the changes in dtgbot.lua?
Last edited by G3rard on Saturday 25 July 2015 0:20, edited 2 times in total.
Not using Domoticz anymore
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 »

Heisenberg wrote:I still can't get the BOT running :(
Have you checked the dtb.log.errors file?
Not using Domoticz anymore
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

G3rard wrote:@Simonrg, is there a way to change the scripts in the LUA folder so these send the message to TelegramChatId as defined in DomoticzData.sh?
Now it's sending the respons to the person who has sent the message to the group and I want the bot to send the answer to the group.

I have this working for the bash scripts, by changing Send(Msg)To. Should I make the changes in dtgbot.lua?
My corrected answer:
Thanks, I now understand what is going on :shock: and if we change from using the from.id to chat.id, then if you send a private message dtgbot will respond privately to you and if you send a group chat then it should respond to the group. This should mean you don't have to hard code anything in either the bash scripts or the Lua code.

Please could you comment out around line 373 (within on_msg_receive function) in dtgbot.lua, i.e. the first line below and add the comment plus the extra line - so basically msg_from will now be msg.chat.id rather than msg.from.id:

Code: Select all

--    msg_from = msg.from.id
--  Changed from from.id to chat.id to allow group chats to work as expected.
    msg_from = msg.chat.id
Let me know if this solves your problem with group chats.

My first answer, just kept for the thought process :lol: :
It would be easy to change, but you would loose any flexibility and according to the documentation of the api you shouldn't need to do this.

Are you sure you are sending your message as Group Chat message and not as a private message? (I hadn't understood that from was the identified as the user rather than the group chat).

If you look at the api reference - https://core.telegram.org/bots/api#message - it returns a unique identifier for the user or the group chat, it is this identifier which is used to return the message. So it would appear that your message is being interpreted as private and not one from a group, hence it is being returned to you not to the group.

I am using the getUpdates method to retrieve the messages, https://core.telegram.org/bots/api#getupdates, this return updates, https://core.telegram.org/bots/api#update, I use the messages, https://core.telegram.org/bots/api#message, the message should contain either a user https://core.telegram.org/bots/api#user or group chat https://core.telegram.org/bots/api#groupchat, both of which contain a unique id which will be user_id or group_id. Actually from.id is always the user, it is only chat.id which changes from user to group chat, :oops: I have been using the from.id, using the chat.id should make it behave as I assumed it was already.

Full on_msg_receive function:

Code: Select all

function on_msg_receive (msg)
  if started == 0 then
    return
  end
  if msg.out then
    return
  end
      
  if msg.text then   -- check if message is text
    --  ReceivedText = string.lower(msg.text)
    ReceivedText = msg.text
  
--    if msg.to.type == "chat" then -- check if the command was given in a group chat
--      msg_from = msg.to.print_name -- if yes, take the group name as a destination for the reply
--    else
--      msg_from = msg.from.print_name -- if no, take the users name as destination for the reply
--    end
--    msg_from = msg.from.id
--  Changed from from.id to chat.id to allow group chats to work as expected.
    msg_from = msg.chat.id
    msg_id =msg.message_id
    if HandleCommand(ReceivedText, tostring(msg_from),msg_id) == 1 then
      print "Succesfully handled incoming request"
    else
      print "Invalid command received"
      print(msg_from)
      send_msg(msg_from,'⚡️ INVALID COMMAND ⚡️',msg_id)
--      os.execute("sleep 5")
--      Help(tostring (msg_from))
    end
  end
--  mark_read(msg_from)
end
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
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 »

simonrg wrote: My corrected answer:
Thanks, I now understand what is going on :shock: and if we change from using the from.id to chat.id, then if you send a private message dtgbot will respond privately to you and if you send a group chat then it should respond to the group. This should mean you don't have to hard code anything in either the bash scripts or the Lua code.

Please could you comment out around line 373 (within on_msg_receive function) in dtgbot.lua, i.e. the first line below and add the comment plus the extra line - so basically msg_from will now be msg.chat.id rather than msg.from.id:

Code: Select all

--    msg_from = msg.from.id
--  Changed from from.id to chat.id to allow group chats to work as expected.
    msg_from = msg.chat.id
Let me know if this solves your problem with group chats.
That works indeed! Thanks for the quick respons. It works for both the lua and bash scripts.
When I send a message to the bot, it responds to me. When I send a message in the group, the bot responds to the group :).
Not using Domoticz anymore
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

G3rard wrote:That works indeed! Thanks for the quick response. It works for both the lua and bash scripts.
When I send a message to the bot, it responds to me. When I send a message in the group, the bot responds to the group :).
8-) 8-) 8-) 8-)
Phew / Great - I had been watching the issue with the group chat but I just couldn't understand what was wrong, it was only when I worked back through the api method by method, object by object, that I saw the ambiguity and why there was both a from and chat object which seemed to contain the same information at least for a private message, that it dawned on me that it wouldn't in the case of a group chat.

In the end the fix was simple, I now need to add some error checking into the way in which the bot handles incoming messages and reponses from Domoticz, once I have had a chance to do this I will upload a new version.

:D
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
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 »

simonrg wrote:
G3rard wrote:That works indeed! Thanks for the quick response. It works for both the lua and bash scripts.
When I send a message to the bot, it responds to me. When I send a message in the group, the bot responds to the group :).
8-) 8-) 8-) 8-)
Phew / Great - I had been watching the issue with the group chat but I just couldn't understand what was wrong, it was only when I worked back through the api method by method, object by object, that I saw the ambiguity and why there was both a from and chat object which seemed to contain the same information at least for a private message, that it dawned on me that it wouldn't in the case of a group chat.

In the end the fix was simple, I now need to add some error checking into the way in which the bot handles incoming messages and reponses from Domoticz, once I have had a chance to do this I will upload a new version.

:D
Great, looking forward to the new version. Your work is much appreciated!!!

Maybe you can also have a look into 2 minor issues I have seen:
1) when sending only the command "/On" dtgbot gets in a loop and no other messages are accepted anymore
2) when sending "/On Lamp (kitchen)" this is interpreted as "/On Lamp kitchen". So the brackets are removed from the device name, meaning you can't switch a device with brackets in the name.
Not using Domoticz anymore
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

G3rard wrote:Maybe you can also have a look into 2 minor issues I have seen:
1) when sending only the command "/On" dtgbot gets in a loop and no other messages are accepted anymore
2) when sending "/On Lamp (kitchen)" this is interpreted as "/On Lamp kitchen". So the brackets are removed from the device name, meaning you can't switch a device with brackets in the name.
1) is one of the errors I should catch, 2) will need a little more thought.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
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 »

2) is not important, I will just rename my switch :-)
Not using Domoticz anymore
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 »

@simonrg, I have changed the function send_msg in dtgbot.lua a little bit so the bot only sends messages to the chat id which is defined in DomoticzData.sh.
This way you can control to which user/group the bot sends answers. I didn't find any whitelisting options for a bot so I thought this could be a nice way.

Code: Select all

function send_msg(SendTo, Message,MessageId)
  print("sendto: "..SendTo)
  print("telegramchatid: "..TelegramChatId)
  if (SendTo == TelegramChatId) then --user/group is the same as defined in domoticzdata.sh
	print(telegram_url..'sendMessage?timeout=60&chat_id='..SendTo..'&reply_to_message_id='..MessageId..'&text='..url_encode(Message))
	response, status = https.request(telegram_url..'sendMessage?chat_id='..SendTo..'&reply_to_message_id='..MessageId..'&text='..url_encode(Message))
  else --unknown user/group, don't send the message, send a warning to the user/group defined in domoticzdata.sh
	text_unkown_user = "Unknown user "..SendTo.." using Domoticz bot"
	print(text_unkown_user)
	response, status = https.request(telegram_url..'sendMessage?chat_id='..TelegramChatId..'&reply_to_message_id='..MessageId..'&text='..text_unkown_user)
  end
--  response, status = https.request(telegram_url..'sendMessage?chat_id='..SendTo..'&text=hjk')
  print(status)
  return
end
This only works for the scripts in the lua folder.
I have changed each of the bash scripts to check for this as well. There will be an easier way for this (probably from dtgbot.lua), but I haven't found that yet.
Maybe this is a nice feature to add to your next release (including a nice solution for the bash scripts)?

I have also added some

Code: Select all

print (os.date ("%a %d-%m-%Y %H:%M:%S"))
lines in dtgbot.lua so it is easier to see when a request is handled.

Please let me know your thoughts on this.
Not using Domoticz anymore
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

G3rard wrote:@simonrg, I have changed the function send_msg in dtgbot.lua a little bit so the bot only sends messages to the chat id which is defined in DomoticzData.sh.
........
I have also added some

Code: Select all

print (os.date ("%a %d-%m-%Y %H:%M:%S"))
lines in dtgbot.lua so it is easier to see when a request is handled.
I haven't quite worked out the security model of the bots, I guess if you know the name of a bot then you can interact with it.

The idea is to either add something into dtgbot.cfg that defines a list of acceptable ids or put a variable in Domoticz.

I need to have a think about logging, certainly a few times and dates would be a good idea.

Thanks.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

dtgbot - Domoticz TeleGram BOT

Post by G3rard »

simonrg wrote: I haven't quite worked out the security model of the bots, I guess if you know the name of a bot then you can interact with it.

The idea is to either add something into dtgbot.cfg that defines a list of acceptable ids or put a variable in Domoticz.

I need to have a think about logging, certainly a few times and dates would be a good idea.

Thanks.
I think you can add any bot (did a search on Domoticz and then you find several bots) and that's not handy for Domoticz bots.
Looking forward to your solution and in the meanwhile my solution is working fine for me.
Not using Domoticz anymore
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by Brutus »

Hi Simonrg,

Is it possible to get this some how working on Windows?
My doorbel (picture and message) already functions with a LUA and curl using the Telegram Bot. Already happy with that but chatting to Domoticz and receiving info back would be very nice :)

Hope you can help us.

Greetings Brutus.
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: dtgbot - Domoticz TeleGram BOT

Post by simonrg »

Brutus wrote:Is it possible to get this some how working on Windows?
My doorbel (picture and message) already functions with a LUA and curl using the Telegram Bot. Already happy with that but chatting to Domoticz and receiving info back would be very nice :)
Brutus, could be easy could be difficult, it depends on whether you have the Lua libraries available for Windows or not. If yes easy, if no then why not move to Linux. So you need to find socket, ssl and json Lua libraries for Windows. Simon
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest