Skip to content

Commit

Permalink
Added whatsapp as a medium to send OTPs
Browse files Browse the repository at this point in the history
  • Loading branch information
djamg committed Nov 10, 2023
1 parent c7bcdf3 commit e52c7cc
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions funnel/views/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TransportRecipientError,
TransportTransactionError,
sms,
whatsapp,
)
from ..transports.email import jsonld_view_action, send_email
from ..utils import blake2b160_hex, mask_email, mask_phone
Expand Down Expand Up @@ -272,6 +273,30 @@ def send_sms(
return msg
return None

# Send whatsapp message
def send_whatsapp(
self, flash_success: bool = True, flash_failure: bool = True
) -> str | None:
"""Send an OTP via WhatsApp to a phone number."""
if not self.phone:
return None
message = whatsapp.OTPTemplate(self.otp)
try:
whatsapp.send_via_meta(self.phone, message)
except TransportRecipientError as exc:
if flash_failure:
flash(str(exc), 'error')
else:
raise
if flash_success:
flash(
_("An OTP has been sent to your phone number {number}").format(
number=self.display_phone
),
'success',
)
return message

def send_email(
self, flash_success: bool = True, flash_failure: bool = True
) -> str | None:
Expand Down Expand Up @@ -388,6 +413,30 @@ def send_sms(
return msg
return None

# Send whatsapp message
def send_whatsapp(
self, flash_success: bool = True, flash_failure: bool = True
) -> str | None:
"""Send an OTP via WhatsApp to a phone number."""
if not self.phone:
return None
message = whatsapp.OTPTemplate(self.otp)
try:
whatsapp.send_via_meta(self.phone, message)
except TransportRecipientError as exc:
if flash_failure:
flash(str(exc), 'error')
else:
raise
if flash_success:
flash(
_("An OTP has been sent to your phone number {number}").format(
number=self.display_phone
),
'success',
)
return message

def send_email(
self, flash_success: bool = True, flash_failure: bool = True
) -> str | None:
Expand Down

0 comments on commit e52c7cc

Please sign in to comment.