From e52c7cc82787c496df4026272837fd1ecc8b1024 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Fri, 10 Nov 2023 19:52:13 +0530 Subject: [PATCH] Added whatsapp as a medium to send OTPs --- funnel/views/otp.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/funnel/views/otp.py b/funnel/views/otp.py index cc9c7b711..baf892fab 100644 --- a/funnel/views/otp.py +++ b/funnel/views/otp.py @@ -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 @@ -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: @@ -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: