Skip to content

Commit

Permalink
Merge pull request #828 from MasoniteFramework/fix/671
Browse files Browse the repository at this point in the history
Refactor VonageDriver to validate phone numbers before sending SMS
  • Loading branch information
josephmancuso authored Oct 23, 2024
2 parents e5322d6 + 8f30981 commit fc44a6b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"werkzeug>=2,<3; python_version < '3.8'",
"werkzeug>=3,<4; python_version >= '3.8'",
"watchdog>=2,<=4",
"phonenumbers>=8.12,<9",
],
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
Expand Down
12 changes: 12 additions & 0 deletions src/masonite/notification/drivers/vonage/VonageDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def send(self, notifiable, notification):
sms = self.build(notifiable, notification)
client = self.get_sms_client()
recipients = sms._to
if not isinstance(recipients, list):
recipients = [recipients]
for recipient in recipients:
if not self.is_valid_phone_number(recipient):
raise NotificationException(f"Invalid phone number: {recipient}")
payload = sms.to(recipient).build().get_options()
response = client.send_message(payload)
self._handle_errors(response)
Expand All @@ -67,3 +71,11 @@ def _handle_errors(self, response):
status, message["error-text"]
)
)

def is_valid_phone_number(self, phone_number):
import phonenumbers
try:
parsed_number = phonenumbers.parse(phone_number, None)
return phonenumbers.is_valid_number(parsed_number)
except phonenumbers.NumberParseException:
return False
11 changes: 11 additions & 0 deletions tests/integrations/app/notifications/TestNotification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from src.masonite.notification import Notification
from src.masonite.mail import Mailable
from src.masonite.notification import Sms

class TestNotification(Notification, Mailable):

def to_vonage(self, notifiable):
return Sms().text("Test message" )

def via(self, notifiable):
return ["vonage"]

0 comments on commit fc44a6b

Please sign in to comment.