Skip to content

Commit 972a101

Browse files
author
Amar Khanshali
committed
added empty message exception and custom phone number formatting
1 parent 032c655 commit 972a101

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/notify_mi/helper.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AT_SYMBOL = "@"
1818
EMAIL_SUFFIX = ".com"
1919
MB = 1000000
20+
PHONE_NUMBER_LENGTH = 10
2021

2122
# exceptions
2223
class FileSizeExceeded(Exception):
@@ -37,19 +38,19 @@ def __str__(self):
3738

3839
class EmailFormatError(Exception):
3940
def __str__(self):
40-
return "The receiver email should follow the following format -> name@email.com."
41+
return "The receiver email should follow the following format -> email@gmail.com."
4142

4243
class MessageError(Exception):
4344
def __str__(self):
4445
return "Message type not specified. Pick text, email, or both and include the respective parameters."
4546

4647
class MessageTextError(Exception):
4748
def __str__(self):
48-
return "Phone number AND phone provider is required for sending a text message."
49+
return "Phone number AND phone provider are required for sending a text message."
4950

50-
class MessageEmailError(Exception):
51+
class MessageFieldError(Exception):
5152
def __str__(self):
52-
return "Receiver's email is required to send a message."
53+
return "Interesting...trying to send a message without a message."
5354

5455
# decorator to automatically launch a function in a thread
5556
def threaded(func):
@@ -60,6 +61,12 @@ def wrapper(*args, **kwargs):
6061
return thread
6162
return wrapper
6263

64+
65+
# remove all non-digit characters from phone number
66+
def filter_phone_number(phone_number):
67+
# filter requires a function, wrap phone number in type function
68+
return ''.join(filter(type(phone_number).isdigit, phone_number))
69+
6370
extensions_and_mime_types = [
6471
(".aac", "audio/aac"),
6572
(".abw", "application/x-abiword"),

src/notify_mi/notify.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __send_message_via_email(
5656
file_attachment = None):
5757

5858
# ensure that credentials and info added meets specifications
59-
__check_for_exceptions(phone_number, phone_provider, send_to)
59+
__check_for_exceptions(message, phone_number, phone_provider, send_to)
6060

6161
# initialize variables needed
6262
_phone_number: str = phone_number
@@ -86,7 +86,7 @@ def __send_message_via_email(
8686
if PROVIDERS.get(_phone_provider).get(helper.MMS_SUPPORT_KEY) \
8787
else helper.MESSAGE_TYPE[1]
8888
# create receiver email based on their phone number and carrier
89-
receiver_phone_number = f'{_phone_number}@{PROVIDERS.get(_phone_provider).get(message_type)}'
89+
receiver_phone_number = f'{helper.filter_phone_number(_phone_number)}@{PROVIDERS.get(_phone_provider).get(message_type)}'
9090

9191
# create gmail body
9292
email_message = MIMEMultipart()
@@ -126,7 +126,10 @@ def __send_message_via_email(
126126
email.sendmail(sender_email, send_to, email_message.as_string())
127127

128128

129-
def __check_for_exceptions(phone_number, phone_provider, send_to):
129+
def __check_for_exceptions(message, phone_number, phone_provider, send_to):
130+
# check is message is empty
131+
if message is None or message is helper.EMPTY:
132+
raise helper.MessageFieldError
130133
# check if at least one message type is being sent
131134
# check if all paramters is empty
132135
if None in [phone_number, phone_provider]:
@@ -143,9 +146,8 @@ def __check_for_exceptions(phone_number, phone_provider, send_to):
143146
if helper.AT_SYMBOL not in send_to or helper.EMAIL_SUFFIX not in send_to:
144147
raise helper.EmailFormatError
145148
# verify phone number is formatted correctly
146-
if len(phone_number) != 10 or not phone_number.isdigit():
149+
if len(helper.filter_phone_number(phone_number)) != helper.PHONE_NUMBER_LENGTH:
147150
raise helper.PhoneNumberError
148151
# verify provider given is found in providers.py
149152
if PROVIDERS.get(phone_provider) is None:
150153
raise helper.ProviderNotRecognized
151-

0 commit comments

Comments
 (0)