Telegram bot next level

So after my first try with the telegram bot i wanted something more.
I wanted that the bot could be a little bit smarter en could give me some suggestions of devices if i don’t know the name exactly.

So i started again and created an dynamic telegram bot for domoticz using telepot.

To secure the bot so that not everyone could talk to it i build in an check for usernames which can be set in the telegram app.

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)

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.

you can find the code in my github repo:
Github Dynamic Telegram Bot

So an couple examples:

Selector Switch
select switch


Car Location
car location

Telegram Bot

So my doorbell was fixed, it creates an picture and sent in automatically to an telegram group where my newly created bot, the misses and i are members from.

I thought it was fun by creating an system/method to send posts to the telegram group so i wanted to see how far i could go with it.

At the moment the misses is 32 weeks pregnant so the words walking and fast cant be in the same sentence but our delivery guy doesn’t now that to well every morning.

So i thought what if i created an telegram which gives the misses (and me of course) the possibility to talk back or choose options so the delivery guy knows that someone is home but that it takes a moment before the door can be opened.

Easily enough there where plenty of examples on the net waiting to be used by an nerdy dad in progress 🙂

I already had sound in the bathroom where the speakers where loud enough to be heared at the frontdoor so only 2 thing where necessary:

  1. An telegrambot which excepts commands from only the misses and me.
  2. An api which plays an mp3 on the bathroom speakers 🙂

so first the bot, i just an standard example and added some features like an urlopener for the api but also for domoticz (if we wanted more commands in telegram it could be used for setting the light on and off in the living or something like that) and of course only the commands send by the misses and me must me executed.

After an hour i had an pretty simple telegram bot which runs on my main domoticz server which checks the telegram housegroup en sends an special keyboard/button to the enduser so we only had to push on an button to play an mp3 file in the bathroom 🙂

The steps to create an bot with botfather and get the token is explained on the telegram site itself where you can find more documentation about the possibility’s:

then the bot itself, it needed to install an python module named telepot so use the command pip install telepot for it.

import telepot
import urllib2
from telepot.namedtuple import ReplyKeyboardMarkup

def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
user = msg['from']['username']

markup = ReplyKeyboardMarkup(keyboard=[['Doorbell 1 moment','Doorbell not home'],
['Dog', 'surprise', 'Firework']],
print 'Got command: %s' % command
unames = ['user 1', 'user2', 'user3']
url = '' # url of domoticz
telbotApi = 'ip adres of the api server'
run = False
if user.lower() in unames:
if command.lower() == '/start':
bot.sendMessage(chat_id, 'What can I do for you?', reply_markup=markup)
if command.lower() == 'doorbell 1 moment':
url = 'http://' + telbotApi + '/speak?id=1'
run = True
elif command.lower() =='doorbell not home':
url = 'http://' + telbotApi + '/speak?id=2'
run = True
elif command.lower() == 'dog':
url = 'http://' + telbotApi + '/speak?id=3'
run = True
elif command.lower() == 'surprise':
url = 'http://' + telbotApi + '/speak?id=4'
run = True
elif command.lower() == 'firework':
url = 'http://' + telbotApi + '/speak?id=5'
run = True
elif command.lower() =='kitchenled purple':
url += '/json.htm?type=command¶m=switchlight&idx=245&switchcmd=Set%20Level&level=50'
run = True
elif command.lower() =='kitchenled off':
url += '/json.htm?type=command¶m=switchlight&idx=245&switchcmd=Set%20Level&level=0'
run = True
elif command.lower() =='kitchenled red':
url += '/json.htm?type=command¶m=switchlight&idx=245&switchcmd=Set%20Level&level=10'
run = True
if run:

bot = telepot.Bot('bot token in here')
print 'I am listening...'

while 1:

So this script only needs 3 things for it to work.

  1. an bot token in the line: telepot.Bot(‘bot token in here’)
  2. set the url and telbotApi address

after that it can run in the background and it will give you an keyboard button markup with 2 lines:

  1. First line is doorbell 1 moment and doorbell not home
  2. Second line gives you some fun options, Dog, Surprise and Firework (holidays are comming 🙂 )

After checking that the script was working like it supposed to i went to create an simple small api which plays an mp3 file with the name of the id which was given by the telegramBot.

On the raspberry in the bathroom i created an folder api which included 1 subfolder “files” i wasn’t pleased with the tts (Text to Speak) options for the dutch language so i thought i will create/use mp3 files which then can be played using the api.
So i created some mp3 files and of course for the upcomming new years events some firework audio and an loud dog just because it can.
after changing the names of the mp3’s to 1.mp3, 2.mp3 and so on i started by creating the simple api.

from flask import Flask
from flask import request
import subprocess
app = Flask(__name__)

def play_mp3(path):
subprocess.Popen(['mpg123', '-q', path]).wait()

def index():
return 'doorbell api'

def speak():
_par = str(request.args.get('id'))
play_mp3('/var/scripts/api/files/' + _par + '.mp3')
return 'error playing file'
return _par

if __name__ == '__main__':'',port=80, debug=True)

for this to work you need to install a couple of things on the raspberry:

sudo apt-get install mpg123
sudo pip install flask

after running this it will wait for an url with the following parameters: ‘/speak?id=’ and the id of the mp3 file you want to play.
i start the api by adding the following line to bottom of /etc/rc.local before exit 0

python /path/to/ &

and voila it works when the system reboots.
It was fun creating this and it works like an charm so i am thinking to explore more things on telegram to combine it with domoticz or an babyroom 🙂

The python files and mp3 samples used in this project can be downloaded using the following link if you are interested.

RGB Led Strip

So i have a couple of raspberry pi’s running in my house so i can control my thermostat, audio, lights and hoses for the garden but i wanted to change my kitchen a little and i saw some kitchens on the internet with lights in the bottom of the whole kitchen.

I thought hmm could it be possible, so i started to search on the web if other people tried to control an led strip.. duhh so many people did it but an specific instruction was a little bit difficult to find.

After a while i did find enough instructions so i made an list with the hardware i needed to control an rgb led strip and the supply’s to install it in the kitchen.

So the list was:

  • RGB ledstrip, i didn’t want to spend a lot of money because i haven’t worked with it so i could wreck it the first time 🙂 so i bought this one: Renkforce 5MAC862W  for € 17,99
  • Then i needed some transistors, for the sake of safety that if i wreck an transistor and cant keep trying to control the led strip i bought 40 of them: Transistor TIP120STM TO220 for € 0,28 a piece
  • Raspberry pi Zero
  • Microsd card
  • Breadboard
  • Some Male Female jumper cables, just enough to connect everything. i bought enough last year on AliExpress so no worry’s here.
  • Cable tray 2.5 m
  • UTP Cable, 2 m

To install the led strip in the kitchen i bought an simple cable tray of 2.5  meters, we will use both sites of the tray because we don’t want the ledstrip closed inside the tray but the tray will hold the ledstrip in one line and displays it nicely on the ground 🙂

So the next friday everything was delivered at home so i started by connecting the led strip to the raspberry pi using the breadboard, jumpercables and the transistors.

i used the instructions on: using the following scheme’s


for the easy modus i also added an push button to the breadboard so i could test my api and ledstrip. To simple add an button just have 1 pin added to ground and the other to an bcm gpio pin.

When i was done with the instructions i tried the example script and you know what the led strip wasn’t working… After an hour looking at a simple 4 line script to turn the led strip on and checking the breadboard again and again i was sick of it and begun to mess around with the led strip. apparently the rgb connections where different than the led strip itself say’d so my + was on an other pin and my r and b where switched…. strange that they wrote it down differently on the led itself but pff i was happy that it was working 🙂 !!


So the led strip was working and i could control the colors, i was so happy until i thought about the installation in the kitchen. How could i connect the led strip to the raspberry without seeing any awful cables or jumper wires. After some thinking i  thought an UTP cable has enough connectors in it maybe it could work so i tried that and yes after some soldering and connecting the right cables to each other it was working 🙂 so now the final step, installation in the kitchen itself.

First a nice place for the raspberry and the power for the led strip and the raspberry itself. It seemed that i was lucky because an kitchen tray wasn’t as high as it could be so i’ve had enough space in an tray 🙂

Than it was just installing the cable tray beneath the kitchen and put the led strip in it

First i tried a couple of meters to look how it was, and actually it wasn’t to bad. so i went on with the rest of it and tadaa.

Now i needed to write the api for it so i could control the led strip using my domoticz installation.

1,2,3 and poof i’ve had an simple api which controls the transistors so i could control the colors using an selector switch in domoticz.

But i wasn’t ready. my purpose of these actions is at an moment in the future my house can think a little bit for itself. I remembered what an friend of my has in his own house. And lamp or led strip that colors blue when it starts to rain within 20 min.

I thought you now what, i will do that as wel. so i’ve searched the domoticz sites for the buienradar lua script and found the is it gonna rain wiki:

Here i found the script for the rain and created an extra lua script which checks if the switch rain is on it will change the kitchen led strip to Blue and saves the old value to an user variable.

The reason for this was that if the rain switch went off that the kitchen led strip will change into his old value again.

and you know what, for the first time it worked at once 😀

so another project done and that in one weekend of work 🙂 up to the next one