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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please write down the showed numbers *