Page 1 of 1

Telegram bot next level

Posted: Friday 07 December 2018 11:19
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.

Re: Telegram bot next level

Posted: Saturday 08 December 2018 16:17
by Brutus
Looking nice. Can it also work on a Domoticz installation on Windows? I have Python 3.5.2 installed.

Re: Telegram bot next level

Posted: Saturday 08 December 2018 16:23
by squandor
Should not be an problem. It's using the domoticz Api so it should work.

Verstuurd vanaf mijn ONEPLUS A6003 met Tapatalk


Re: Telegram bot next level

Posted: Saturday 08 December 2018 21:12
by Brutus
With some great help from Squandor this bot is working great on my Windows installation!

Re: Telegram bot next level

Posted: Saturday 08 December 2018 21:28
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'})

Re: Telegram bot next level

Posted: Sunday 09 December 2018 11:09
by squandor
That has something to do with you're bot token. Did you use the correct one?

Re: Telegram bot next level

Posted: Sunday 09 December 2018 11:12
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.

Re: Telegram bot next level

Posted: Sunday 09 December 2018 14:09
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'

Re: Telegram bot next level

Posted: Sunday 09 December 2018 16:06
by squandor
You need to put an username in you're telegram at settings in lowercase

Re: Telegram bot next level

Posted: Sunday 13 October 2019 19:58
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?

Re: Telegram bot next level

Posted: Friday 04 September 2020 15:35
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?

Re: Telegram bot next level

Posted: Wednesday 28 October 2020 22:25
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]

Re: Telegram bot next level

Posted: Monday 26 February 2024 17:22
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

Re: Telegram bot next level

Posted: Monday 29 July 2024 11:10
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