Python code to place a call via fritzbox

Hi everyone,

I recently started working with openhab and it’s awesome!
Most of the stuff I am trying to do works well. However, I wasn’t able to place a call on a specific event and as far as I can see, more people do have this problem.
But I just came across an old post by @tbbear with a python code, which I posted below. Does someone know if this is still working? I do have a Fritzbox, too, but I don’t know much about python. I tried to use the code, but all the apostrophes used seem to be incorrect, right? So I changed them and then I get error messages because of missing indentation…
Long story short, could someone help me to get this working?

Thanks a lot,
celdrion

import re,hashlib,requests,sys
host = '192.168.2.1’
pw = 'xxxxxxx’
SSID = 'wlan-ssid’
url = ‘http://’+host
headers = {
‘Host’: host,
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0’,
‘Accept’: ‘text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8’,
‘Accept-Language’: ‘de,en-US;q=0.7,en;q=0.3’,
‘Content-Type’: ‘application/x-www-form-urlencoded; charset=UTF-8’,
‘Referer’: ‘’,
‘Connection’: ‘keep-alive’,
‘Pragma’: ‘no-cache’,
‘Cache-Control’: ‘no-cache’,
}
try:
commandarg = sys.argv[1]
except Exception as ce:
commandarg = ‘-Stat’

def login():
try:
r = requests.get(url + “/login_sid.lua”)
challengereg = re.search(r’(.)</\Challenge>’, r.text, re.M|re.I).group(1)
challengestr = challengereg+"-"+pw
responsehash = hashlib.md5(challengestr.encode(‘utf-16le’)).hexdigest()
response = challengereg+"-"+responsehash
data = {‘response’ : response,‘page’: ‘’,‘username’ : ‘’}
rp = requests.post(url+’/login_sid.lua’,headers=headers, data=data)
sidreg = re.search(r’(.)’, rp.text, re.M|re.I).group(1)
return(sidreg)
except Exception as e:
print(str(e))

def status():
sid=login()
r = requests.get(url + “/wlan/wlan_settings.lua?sid=”+sid)
if ‘name=“active” checked> WLAN-Funknetz aktiv’ in r.text:
print(“W-Lan an!”)
elif ‘name=“active” > WLAN-Funknetz aktiv’ in r.text:
print(“W-Lan aus!”)
else:
print(“Fehler”)
requests.get(url + “/login_sid.lua?logout=&sid=+”+sid)

def call(inputvar):
sid=login()
requests.get(url + “/fon_num/dial_foncalls.lua?sid=”+sid+"&dial="+inputvar+"&xhr=1")
requests.get(url + “/login_sid.lua?logout=&sid=+”+sid)

def useron():
r = requests.get(url + “/net/network_user_devices.lua?sid=”+login())
userreg = re.findall(r’(?m)[“name”]\s=\s"([^"]+?)",[^[]+?[“online”]\s=\s"1",’, r.text)
return(userreg)

def main():
if commandarg == “-Call”:
call(sys.argv[2])
elif commandarg == “-Stat”:
status()
elif commandarg == “-User”:
print(useron())
main()

The syntax is

python FritzI.py -Stat ==> the actual WLAN Status
python Fritz.py -User ==> shows all ‘online’ devices
python Frits.py -Call phonenumber ==> calls the given phone number 

Edit: here is the link to the original topic
https://community.openhab.org/t/solved-is-it-possiple-to-call-a-number-using-fritzbox-binding/7234

Ok

I cleaned it up
The problem was the the OP didn’t use the code fences

Here you go
NOT TESTED!!

import re,hashlib,requests,sys
host = '192.168.2.1'
pw = 'xxxxxxx'
SSID = 'wlan-ssid'
url = 'http://'+host
headers = {
    'Host': host,
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
    'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Referer': '',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
}
try:
    commandarg = sys.argv[1]
except Exception as e:
    commandarg = '-Stat'

def login():
    try:
        r = requests.get(url + "/login_sid.lua")
        challengereg = re.search(r'(.)</\Challenge>', r.text, re.M|re.I).group(1)
        challengestr = challengereg+"-"+pw
        responsehash = hashlib.md5(challengestr.encode('utf-16le')).hexdigest()
        response = challengereg+"-"+responsehash
        data = {'response' : response,'page': '','username' : ''}
        rp = requests.post(url+'/login_sid.lua',headers=headers, data=data)
        sidreg = re.search(r'(.)', rp.text, re.M|re.I).group(1)
        return(sidreg)
    except Exception as e:
        print(str(e))

def status():
    sid=login()
    r = requests.get(url + "/wlan/wlan_settings.lua?sid="+sid)
    if 'name="active" checked> WLAN-Funknetz aktiv' in r.text:
        print("W-Lan an!")
    elif 'name="active" > WLAN-Funknetz aktiv' in r.text:
        print("W-Lan aus!")
    else:
        print("Fehler")
    requests.get(url + "/login_sid.lua?logout=&sid=+"+sid)

def call(inputvar):
    sid=login()
    requests.get(url + "/fon_num/dial_foncalls.lua?sid="+sid+"&dial="+inputvar+"&xhr=1")
    requests.get(url + "/login_sid.lua?logout=&sid=+"+sid)

def useron():
    r = requests.get(url + "/net/network_user_devices.lua?sid="+login())
    userreg = re.findall(r'(?m)["name"]\s=\s"([^"]+?)",[^[]+?["online"]\s=\s"1",', r.text)
    return(userreg)

def main():
    if commandarg == "-Call":
        call(sys.argv[2])
    elif commandarg == "-Stat":
        status()
    elif commandarg == "-User":
        print(useron())
        
main()

Awesome, thank you very much! I’m gonna try tomorrow.

Hi!
Is it still working ?
I have tried with my Fritzbox 7490 with FRITZ!OS: [07.29] but can’t make it working…

command:
python3 /etc/openhab2/scripts/Fritz2.py -Stat
always get a fail message:
Fehler

Thanks for any help !!!

not possible anymore. Quite a while ago AVM changed their API.