Telegram bot next level

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Post Reply
squandor
Posts: 213
Joined: Saturday 19 December 2015 23:34
Target OS: Raspberry Pi
Domoticz version: 3.5837
Contact:

Telegram bot next level

Post by squandor » Friday 07 December 2018 12:19

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: 326
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Telegram bot next level

Post by Brutus » Saturday 08 December 2018 17:17

Looking nice. Can it also work on a Domoticz installation on Windows? I have Python 3.5.2 installed.
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

squandor
Posts: 213
Joined: Saturday 19 December 2015 23:34
Target OS: Raspberry Pi
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor » Saturday 08 December 2018 17:23

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: 326
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Heerlen, The Netherlands
Contact:

Re: Telegram bot next level

Post by Brutus » Saturday 08 December 2018 22:12

With some great help from Squandor this bot is working great on my Windows installation!
1x Intel NUC D54250WYK (Windows 7 x64)
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
19x Fibaro Modules

User avatar
EdwinK
Posts: 1412
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Telegram bot next level

Post by EdwinK » Saturday 08 December 2018 22:28

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) | RFxcom | Dashticz V2

squandor
Posts: 213
Joined: Saturday 19 December 2015 23:34
Target OS: Raspberry Pi
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor » Sunday 09 December 2018 12:09

That has something to do with you're bot token. Did you use the correct one?

User avatar
EdwinK
Posts: 1412
Joined: Sunday 22 January 2017 22:46
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Rhoon
Contact:

Re: Telegram bot next level

Post by EdwinK » Sunday 09 December 2018 12:12

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) | RFxcom | Dashticz V2

Joep123
Posts: 36
Joined: Monday 26 March 2018 18:44
Target OS: Raspberry Pi
Domoticz version: Beta
Contact:

Re: Telegram bot next level

Post by Joep123 » Sunday 09 December 2018 15:09

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 23:34
Target OS: Raspberry Pi
Domoticz version: 3.5837
Contact:

Re: Telegram bot next level

Post by squandor » Sunday 09 December 2018 17:06

You need to put an username in you're telegram at settings in lowercase

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests