This is a little script which runs on my home-server and watches my AVM Fritz!Box 7270 for incoming calls.
Before the script will work, you have to enable the Call Monitor on the Fritz!Box which opens Port 1012 and sends all phone events on that port.
You can enable it, by dialling #96*5* on a phone connected to the Fritz!Box. To disable the feature, you would dial #96*4*.
This script will announce a name for known numbers instead of the digits. You can create such a phonebook by creating a file callclient.list in the same directory as the script. The format is:
0123456789 John Doe 01234887766553 Jane Doe
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import telnetlib import os import sys HOST = "fritz.box" PORT = 1012 ESPEAK_BINARY = "/usr/bin/espeak -v german+f2" print( "Started in %s" % sys.path[0] ) f = open( sys.path[0] + "/callclient.list", "rt" ) pb = {} for line in f: line = line.strip(" \n") ( number, sep, name ) = line.partition(' ') name = name.strip() pb[number] = name print( "Loaded %s entries from local phonebook." % len( pb ) ) print( pb ) tn = telnetlib.Telnet( HOST, PORT ) try: while True: event = tn.read_until( b"\n" ).decode( "utf-8" ) edata = event.split( ';' ) print( edata ) if edata[1] == "RING": caller = edata[3] print( "Incoming call from %s" % caller ) if caller in pb: caller = pb[caller] print( "Caller found in phonebook: %s" % caller ) os.system( "/usr/bin/play /usr/share/sounds/generic.wav" ) os.system( ESPEAK_BINARY + " \"Anruf von " + caller + ".\"" ) except EOFError as eof: print( "Connection closed by remote host." ) print( "All done." )
Basically, the script connects to port 1012 on the Fritz!Box and waits for log entries. If it receives a ”RING” (=incoming call), it will play the soundfile /usr/share/sounds/generic.wav and then use eSpeak to tell you the number of the incoming call. That's all it does for now. Maybe I will implement a caller lookup later.
Just create this Upstart job as /etc/init/fbclient.conf:
start on runlevel [2345] stop on shutdown respawn exec sudo -H -n -u mbirth LC_CTYPE="en_US.UTF-8" /opt/mbirth/fbclient/callclient.py
This will start the script located in /opt/mbirth/fbclient/callclient.py automatically (as user mbirth) on runlevels 2, 3, 4 and/or 5. It will be stopped on shutdown.
LC_CTYPE needs to be set because Python can't correctly guess the encoding of the terminal when started from UpStart. It then assumes ascii encoding and reading special characters like umlauts from the phonebook file fails with an error.