Skip to content

Add Clean Phone Numbers #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions nextsms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import requests
import phonenumbers
from base64 import b64encode
from collections import namedtuple
from typing import List, Dict, Union, Iterable
Expand Down Expand Up @@ -121,6 +122,32 @@ def create_header(self) -> Dict:
'Authorization': f'Basic {self._user.secret_key}'
}

def clean_phone_numbers(self, phone_numbers: List[str], country_code: str = 'TZ') -> List[str]:
"""
Standardize phone numbers to E164 format

Args:
phone_numbers (List[str]): List of phone numbers to be standardized
country_code (str, optional): Country code to be used. Defaults to 'TZ'

Raises:
TypeError: If phone_number is not of type <class 'list'>

Returns:
List[str]: List of standardized phone numbers

Example:
>> import nextsms
>> sender = nextsms('KalebuJordan', 'kalebu@opensource')
>> sender.clean_phone_numbers(['0757294146', '0754205561'])
"""
if not isinstance(phone_number, list):
raise TypeError(
f"phone_number should be of type <class 'list'> not {type(phone_number)}")

return [phonenumbers.format_number(
phonenumbers.parse(number, country_code), phonenumbers.PhoneNumberFormat.E164) for number in phone_number]

def sendsms(self, message: str, recipients: Union[str, List[str]], sender_id: str = "NEXTSMS") -> Dict:
"""Method to send sms using nextsms gateway

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

install_requires=[
'requests',
'phonenumbers',
],

python_requires=">=3.6",
Expand Down