Telegram bot next level

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
squandor
Posts: 213
Joined: Saturday 19 December 2015 22:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5837
Contact:

Telegram bot next level

Post by squandor »

What can it do?
Using this bot you can ask him for an switch/scene/group/utility switch. After the bot found the device it will ask you what you want to do with it (on,off)

To secure the bot you must place usernames in the array unames: unames = ['username1', 'username2'] by using this the bot will only react to the users which are in the array.

If the device is an selector switch the bot will build special keyboard in telegram where you can find you're selector options from domoticz.

But sometimes you don't know the full name of the device or you forget an character, in that case the bot will look for devices that could be the device that you want and will come with suggestions.

So for example if you have 2 switches called livingroomSpeaker, livingroomLights

If you type livingroomspeaker to the bot it will come with the switch and asks you what you want to do (on or off)

but if you type living the bot will make an suggestions and asks if you meant one of the following devices and comes with livingroomSpeaker and livingroomLights.

Systemd script
Thanks to so help someone made an easy service file so you can easy run the bot using the systemd service. The example is in the repo. You can use it using the following steps Copy code to /etc/systemd/system/messagebot.service Edit the values in the example script:

Code: Select all

[Unit]
Description=Telegram Bot for Domoticz After=multi-user.target
[Service]
Type=idle
User=
ExecStart=/usr/bin/python /home//scripts/telegram/dynamicTelegramBot/squandorDynamicTelBot.py
WorkingDirectory=/home/<username>/scripts/telegram/dynamicTelegramBot/
[Install]
WantedBy=multi-user.target

Code: Select all

sudo chmod 655 /etc/systemd/system/messagebot.service

Code: Select all

sudo systemctl daemon-reload

Code: Select all

sudo systemctl enable messagebot.service

Code: Select all

sudo systemctl start messagebot.service

Code: Select all

sudo systemctl status messagebot.service
Configuration:
On the first run it will ask you some information:
- url: <domoticz_url> (http://192.168.1.2:8080)
- bot_token
- unames (usernames seperated by an comma): user1, user2, user3
- car_location_idx: can be an idx number of an text utility which as the lat,long in it.

after that an config.ini will be created and it will run.

Examples:
Switch:
Image

Selector Switch:
Image

Suggestions:
Image

Car Location:
Image

The code can be found on github:
https://github.com/squandor/dynamicTelegramBot/

I made an python2 and python3 version.

I hope that it can be of use for you.
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: Telegram bot next level

Post by Brutus »

Looking nice. Can it also work on a Domoticz installation on Windows? I have Python 3.5.2 installed.
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
squandor
Posts: 213
Joined: Saturday 19 December 2015 22:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor »

Should not be an problem. It's using the domoticz Api so it should work.

Verstuurd vanaf mijn ONEPLUS A6003 met Tapatalk

User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: Telegram bot next level

Post by Brutus »

With some great help from Squandor this bot is working great on my Windows installation!
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
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Telegram bot next level

Post by EdwinK »

Loads of error''s.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/telepot/loop.py", line 60, in run_forever
allowed_updates=allowed_updates)
File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 1000, in getUpdates
return self._api_request('getUpdates', _rectify(p))
File "/usr/local/lib/python2.7/dist-packages/telepot/__init__.py", line 491, in _api_request
return api.request((self._token, method, params, files), **kwargs)
File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 155, in request
return _parse(r)
File "/usr/local/lib/python2.7/dist-packages/telepot/api.py", line 150, in _parse
raise exception.TelegramError(description, error_code, data)
TelegramError: (u'Not Found', 404, {u'error_code': 404, u'ok': False, u'description': u'Not Found'})
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
squandor
Posts: 213
Joined: Saturday 19 December 2015 22:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor »

That has something to do with you're bot token. Did you use the correct one?
User avatar
EdwinK
Posts: 1820
Joined: Sunday 22 January 2017 21:46
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Telegram bot next level

Post by EdwinK »

Yes, I believe so. Going to take another look, after the weekend is over.

Okay, well I checked today, and it seems I made a typo in that long key.
Running latest BETA on a Pi-3 | Toon® Thermostat (rooted) | Hue | Tuya | IKEA tradfri | Dashticz V3 on Lenovo Huawei Tablet | Conbee
Joep123
Posts: 56
Joined: Monday 26 March 2018 18:44
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Telegram bot next level

Post by Joep123 »

I get this error:

I am listening...
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/telepot/loop.py", line 37, in run_forever
self._handle(msg)
File "/usr/local/lib/python2.7/dist-packages/telepot/helper.py", line 1041, in route
return fn(msg, *args, **kwargs)
File "/home/pi/domoticz/scripts/telegram/dynamicTelegramBot/DynamicTelBot.py", line 200, in handle
user = msg['from']['username']
KeyError: 'username'
squandor
Posts: 213
Joined: Saturday 19 December 2015 22:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor »

You need to put an username in you're telegram at settings in lowercase
Derik
Posts: 1601
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Telegram bot next level

Post by Derik »

see this for the first time.. is ths option stil working? And is teh maps function lie?
i mean can i use the maps function for a llive local device, a dummy switch @ work and a switch @ home etc
Or what can i do with the maps function?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
karlzre
Posts: 26
Joined: Sunday 06 August 2017 18:48
Target OS: -
Domoticz version:
Contact:

Re: Telegram bot next level

Post by karlzre »

Hi,

i'm testing this script.

I set the toke ( same as the one i use for domoticz notification)
I set the username of this channel, i tryed with the [], with the @ in the front.

I see the service "i'm listening", but i didn't get any answer from telegram when i send a switch name.

could you help me to set it up?
Ultimato
Posts: 8
Joined: Sunday 05 July 2020 19:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Telegram bot next level

Post by Ultimato »

Is it also possible to have the bot actively work?
Eg, my wind sensor says itreached a certain valhe so the bot would send me a message like
"The windforce is 19m/s, do you want torun the storm scene and close all shutters?
[Yes] [no]
I'm Running domoticz beta on a Rpi 4
User avatar
waltervl
Posts: 5409
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Telegram bot next level

Post by waltervl »

I am testing with this script. I had to change the Domoticz API calls and using Python3 instead. Also added some extra functionality like Rooms and Device tabs. I removed the car location function (I did not see usage for that in my environment.

Still a lot to test and update. Running as a service not tested. Initial the updated script can be found here (be aware I am only a hobby Python programmer):
https://github.com/waltervl/dynamicTelegramBot
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5409
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Telegram bot next level

Post by waltervl »

Just to inform that the python Telegrambot has had some updates and improvements.
https://github.com/waltervl/dynamicTelegramBot
- Support of Blinds and Dimmers
- Support of Cameras
- Support of Rooms
- Running as a service tested and documented

Image
Image
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests