import requests
import json
import sys
import re
from xml.etree import ElementTree as ET
# Fill these 3 lines with real login details
serverFQDN = "{FQDN}:{port}"
userName = "{owner}"
passWord = "{password}"
#---------------------------------------------------------------------------------------------
# Functions
#---------------------------------------------------------------------------------------------
# Function to check if BLF keys all valid
#def find_missing_ids(Ext: int, blfs: str, phones: list) -> bool:
def find_missing_ids(Ext: int, blfs: str, phones: list):
# Wrap the BLF elements in a root element for proper XML parsing
xml_data = f'<root>{blfs}</root>'
root = ET.fromstring(xml_data)
found = False
for blf in root.findall('BLF'):
# Extract attributes
blf_id = blf.get('ID')
blf_type = blf.get('BLFType')
blf_type_id = blf.get('BLFTypeID')
blf_no = blf.get('BLFNo')
extension = blf.text
# Check conditions: BLFType="BLF", BLFTypeID="0", and Extension not in array of Phones
if (blf_type == "BLF" and blf_type_id == "0"):
if (extension not in phones):
print(f"Extension: {Ext} - Key {blf_no}, Ext {extension}")
found = True
return found
#---------------------------------------------------------------------------------------------
# Main Script
#---------------------------------------------------------------------------------------------
# Authenticate on 3CX server with Username and Password to get an Access Token
# POST /webclient/api/Login/GetAccessToken
url = "https://" + serverFQDN + "/webclient/api/Login/GetAccessToken"
headers = {"Content-Type": "application/json; charset=utf-8"}
data = {"SecurityCode":"", "Username":userName, "Password":passWord}
response = requests.post(url, headers=headers, json=data)
access_token = response.json() ['Token']['access_token']
if response.status_code == 200:
#print ("We received an API Access Token")
print ()
else:
print ("ERROR: ", response.status_code)
print ("Request API Access Token, does NOT work")
sys.exit()
#print ("access_token ", access_token)
# Nah, it's all good
# Get full list of Extensions on the 3CX system
# GET /xapi/v1/Users
url = "https://" + serverFQDN + "/xapi/v1/Users"
headers = {"Content-Type": "application/json; charset=utf-8", 'Authorization': 'Bearer ' + access_token}
data = {}
# Here we go...
response = requests.get(url, headers=headers, json=data)
Anyfound= False
if response.status_code == 200:
response_data = response.json()
phones = [item['Number'] for item in response_data.get('value', [])]
blfsx = [item['Blfs'] for item in response_data.get('value', [])]
for i in range(len(phones)):
blf_content = blfsx[i]
blf_matches = re.findall(r'<BLF[^>]*>.*?</BLF>', blf_content)
blfsz = ''.join(blf_matches) if blf_matches else ""
blfs = blfsz.replace('<BLFS>', '')
Thisfound = find_missing_ids(phones[i], blfs, phones)
if (Thisfound):
Anyfound = True
else:
print ("ERROR: ", response.status_code)
print ("API request does NOT work")
sys.exit()
if (not Anyfound):
print("All good")
print ()
# Okay, it's done!
#---------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------