Package: azure-communication-phonenumbers
Azure Communication Phone Numbers Package client library for PythonAzure Communication Phone Numbers client package is used to administer Phone Numbers.DisclaimerAzure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer tohttps://github.com/Azure/azure-sdk-for-python/issues/20691Getting startedPrerequisitesPython 3.7 or later is required to use this package.You must have anAzure subscriptionA deployed Communication Services resource. You can use theAzure Portalor theAzure PowerShellto set it up.Install the packageInstall the Azure Communication Phone Numbers client library for Python withpip:pipinstallazure-communication-phonenumbersKey conceptsThis SDK provides functionality to easily managedirect offeranddirect routingnumbers.Thedirect offernumbers come in two types: Geographic and Toll-Free. Geographic phone plans are phone plans associated with a location, whose phone numbers' area codes are associated with the area code of a geographic location. Toll-Free phone plans are phone plans not associated location. For example, in the US, toll-free numbers can come with area codes such as 800 or 888.
They are managed using thePhoneNumbersClientThedirect routingfeature enables connecting your existing telephony infrastructure to ACS.
The configuration is managed using theSipRoutingClient, which provides methods for setting up SIP trunks and voice routing rules, in order to properly handle calls for your telephony subnet.Initializing ClientClient can be initialized using the AAD authentication.importosfromazure.communication.phonenumbersimportPhoneNumbersClientfromazure.identityimportDefaultAzureCredentialendpoint="https://<RESOURCE_NAME>.communication.azure.com"# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have your# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.phone_numbers_client=PhoneNumbersClient(endpoint,DefaultAzureCredential())importosfromazure.communication.phonenumbers.siproutingimportSipRoutingClientfromazure.identityimportDefaultAzureCredentialendpoint="https://<RESOURCE_NAME>.communication.azure.com"# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have your# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.sip_routing_client=SipRoutingClient(endpoint,DefaultAzureCredential())Another option is to initialize the client using connection string of the resource.# You can find your connection string from your resource in the Azure Portalimportosfromazure.communication.phonenumbersimportPhoneNumbersClientconnection_str="endpoint=ENDPOINT;accessKey=KEY"phone_numbers_client=PhoneNumbersClient.from_connection_string(connection_str)# You can find your connection string from your resource in the Azure Portalimportosfromazure.communication.phonenumbers.siproutingimportSipRoutingClientconnection_str="endpoint=ENDPOINT;accessKey=KEY"sip_routing_client=SipRoutingClient.from_connection_string(connection_str)Phone numbers clientPhone number types overviewPhone numbers come in two types; Geographic and Toll-Free. Geographic phone numbers are phone numbers associated with a location, whose area codes are associated with the area code of a geographic location. Toll-Free phone numbers are phone numbers with no associated location. For example, in the US, toll-free numbers can come with area codes such as 800 or 888.Searching and Purchasing and Releasing numbersPhone numbers can be searched through the search creation API by providing an area code, quantity of phone numbers, application type, phone number type, and capabilities. The provided quantity of phone numbers will be reserved for ten minutes and can be purchased within this time. If the search is not purchased, the phone numbers will become available to others after ten minutes. If the search is purchased, then the phone numbers are acquired for the Azure resources.Phone numbers can also be released using the release API.SIP routing clientDirect routing feature allows connecting customer-provided telephony infrastructure to Azure Communication Resources. In order to setup routing configuration properly, customer needs to supply the SIP trunk configuration and SIP routing rules for calls. SIP routing client provides the necessary interface for setting this configuration.When a call is made, system tries to match the destination number with regex number patterns of defined routes. The first route to match the number will be selected. The order of regex matching is the same as the order of routes in configuration, therefore the order of routes matters.
Once a route is matched, the call is routed to the first trunk in the route's trunks list. If the trunk is not available, next trunk in the list is selected.ExamplesPhoneNumbersClientGet All Purchased Phone NumbersLists all of your purchased phone numberspurchased_phone_numbers=phone_numbers_client.list_purchased_phone_numbers()foracquired_phone_numberinpurchased_phone_numbers:print(acquired_phone_number.phone_number)Get Purchased Phone NumberGets the information from the specified phone numberresult=phone_numbers_client.get_purchased_phone_number("<phone number>")print(result.country_code)print(result.phone_number)Long Running OperationsThe Phone Number Client supports a variety of long running operations that allow indefinite polling time to the functions listed down below.Search for Available Phone NumberYou can search for available phone numbers by providing the capabilities of the phone you want to acquire, the phone number type, the assignment type, and the country code. It's worth mentioning that for the toll-free phone number type, proving the area code is optional.
The result of the search can then be used to purchase the number in the corresponding API.capabilities=PhoneNumberCapabilities(calling=PhoneNumberCapabilityType.INBOUND,sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND)poller=phone_numbers_client.begin_search_available_phone_numbers("US",PhoneNumberType.TOLL_FREE,PhoneNumberAssignmentType.APPLICATION,capabilities,area_code="833",# Area code is optional for toll-free numbersquantity=2,# Quantity is optional. If not set, default is 1polling=True)search_result=poller.result()Purchase Phone NumbersThe result of your search can be used to purchase the specified phone numbers. This can be done by passing thesearch_idfrom the search response to the purchase phone number API.purchase_poller=phone_numbers_client.begin_purchase_phone_numbers(search_result.search_id,polling=True)Release Phone NumberReleases an acquired phone number.poller=self.phone_number_client.begin_release_phone_number("<phone number>",polling=True)Updating Phone Number CapabilitiesUpdates the specified phone number capabilities for Calling and SMS to one of:PhoneNumberCapabilityType.NONEPhoneNumberCapabilityType.INBOUNDPhoneNumberCapabilityType.OUTBOUNDPhoneNumberCapabilityType.INBOUND_OUTBOUNDpoller=self.phone_number_client.begin_update_phone_number_capabilities("<phone number>",PhoneNumberCapabilityType.OUTBOUND,PhoneNumberCapabilityType.INBOUND_OUTBOUND,polling=True)SipRoutingClientRetrieve SIP trunks and routesGet the list of currently configured trunks or routes.trunks=sip_routing_client.list_trunks()fortrunkintrunks:print(trunk.fqdn)print(trunk.sip_signaling_port)routes=sip_routing_client.list_routes()forrouteinroutes:print(route.name)print(route.description)print(route.number_pattern)fortrunk_fqdninroute.trunks:print(trunk_fqdn)Replace SIP trunks and routesReplace the list of currently configured trunks or routes with new values.new_trunks=[SipTrunk(fqdn="sbs1.contoso.com",sip_signaling_port=1122),SipTrunk(fqdn="sbs2.contoso.com",sip_signaling_port=1123)]new_routes=[SipTrunkRoute(name="First rule",description="Handle numbers starting with '+123'",number_pattern="\+123[0-9]+",trunks=["sbs1.sipconfigtest.com"])]sip_routing_client.set_trunks(new_trunks)sip_routing_client.set_routes(new_routes)Retrieve single trunktrunk=sip_routing_client.get_trunk("sbs1.contoso.com")Set single trunk# Set function will either modify existing item or add new item to the collection.# The trunk is matched based on it's FQDN.new_trunk=SipTrunk(fqdn="sbs3.contoso.com",sip_signaling_port=5555)sip_routing_client.set_trunk(new_trunk)Delete single trunksip_routing_client.delete_trunk("sbs1.contoso.com")TroubleshootingThe Phone Numbers Administration client will raise exceptions defined inAzure Core.Next stepsMore sample codePlease take a look at thesamplesdirectory for detailed examples of how to use this library.Provide FeedbackIf you encounter any bugs or have suggestions, please file an issue in theIssuessection of the projectContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visithttps://cla.microsoft.com.When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the
PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor contactopencode@microsoft.comwith any additional questions or comments.