kbr / fritzconnection

Python-Tool to communicate with the AVM Fritz!Box by the TR-064 protocol and the AHA-HTTP-Interface
MIT License
304 stars 59 forks source link

Add contact entry to phonebook [Feature] - call_action mockup attached #50

Open bufemc opened 4 years ago

bufemc commented 4 years ago

It would be nice if you could add new phone numbers by command line. Reason: I get a lot of cold calls and it drives me nuts always to have to re-login to the Fritzbox and go to the place of the correct phonebook, then to add it. How it could work I have found here: https://forum.fhem.de/index.php?topic=64152.0

get FritzBox tr064Command X_AVM-DE_OnTel:1 x_contact SetPhonebookEntryUID NewPhonebookID 1 NewPhonebookEntryData '<Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><contact><category>0</category><person><realName>Sperr-KONTAKT</realName></person><telephony nid="1"><number type="home" prio="1" id="0">030123456789</number></telephony></contact>'

another solution (German) here: https://administrator.de/wissen/powershell-fritzbox-tr-064-netzwerk-konfigurieren-auslesen-303474.html - check method "Add-FBPhoneBookEntry".

I currently have two phonebooks: the first for normal users, and the other is called "ColdCalls". The latter one is configured to block all contained numbers. Of course it have to be ensured you add the number to the right list ;-)

The idea would be to add it by command line, something like:

fritzphonebook -i ip -p pass -add "ColdCalls | 0700123456 | CompanyXY"

or maybe add it by the phonebook id, in my case normal numbers is 0, ColdCalls is 1:

fritzphonebook -i ip -p pass -add -pbid 1 -phone 0700123456 -name CompanyXY

I would also understand if you want to keep the phonebook read-only. So I already forked this nice repository in case I have to solve it on my own ;-)

bufemc commented 4 years ago

I did a quick mockup, it works! Of course it should not be with fixed values and even the envelope hardcoded, just serve as a template. Is it worth a PR?

        fconn = FritzConnection(address=FRITZ_IP, password=FRITZ_PASS)

        arg = {'NewPhonebookID': 2, # phonebook id, JUST IN MY CASE 2 for auto blocked cold calls
               'NewPhonebookEntryID': '', # DO NOT SET TO 0, WILL OVERWRITE FIRST ENTRY!
               'NewPhonebookEntryData': '<Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><contact><category>0</category><person><realName>AutoBlockTest</realName></person><telephony nid="1"><number type="home" prio="1" id="0">009912345</number></telephony></contact></Envelope>'}

        result = fconn.call_action('X_AVM-DE_OnTel:1', 'SetPhonebookEntry', arguments=arg)

Update: Important is to set an empty string for NewPhonebookEntryID, first had it set to 0, so it always overwrote my first entry in the phonebook ;-)

cryzed commented 3 years ago

I would also understand if you want to keep the phonebook read-only.

Why would that be desired? Is there some specific reason? I would also like to see an API for creating new entries.

cryzed commented 3 years ago

Here's some nicer code to create the needed entry data XML:

import xml.etree.ElementTree as ET

def get_phonebook_entry_data(name: str, number: str) -> str:
    envelope = ET.Element("Envelope", {"xmlns:s": "http://www.w3.org/2003/05/soap-envelope"})
    contact = ET.SubElement(envelope, "contact")
    category = ET.SubElement(contact, "category")
    category.text = "0"
    person = ET.SubElement(contact, "person")
    real_name = ET.SubElement(person, "realName")
    real_name.text = name
    telephony = ET.SubElement(contact, "telephony")
    number_ = ET.SubElement(telephony, "number")
    number_.text = number
    return ET.tostring(envelope, encoding="unicode")