From 2db0369d7ce5e1c0bc7cb2a80df491883fee6af6 Mon Sep 17 00:00:00 2001 From: Shihaam Abdul Rahman Date: Sun, 2 Aug 2026 06:04:20 +0500 Subject: [PATCH] update sms api for smspipe --- api/notifications.py | 24 ++++++++++++++++-------- djangopasswordlessknox/utils.py | 9 +++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/api/notifications.py b/api/notifications.py index 1e30bca..0cc5a74 100644 --- a/api/notifications.py +++ b/api/notifications.py @@ -10,6 +10,16 @@ bot_token = str(config("TG_BOT_TOKEN", cast=str, default="")) chat_id = str(config("TG_CHAT_ID", cast=str, default="")) +def format_mobile(mobile: str) -> str: + """Normalize a Maldives mobile number to E.164 (+960XXXXXXX).""" + number = str(mobile).strip().replace(" ", "") + if number.startswith("+"): + return number + if number.startswith("960"): + return "+" + number + return "+960" + number + + def send_otp(mobile: str, message: str): if not api_url or not api_key: logger.debug("Failed to send SMS. Missing SMS_API_URL or SMS_API_KEY.") @@ -17,12 +27,11 @@ def send_otp(mobile: str, message: str): headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {api_key}", + "X-API-Key": api_key, } data = { - "number": mobile, - "message": message, - "check_delivery": False, + "to": format_mobile(mobile), + "text": message, } response = requests.post(api_url, headers=headers, data=json.dumps(data)) if response.status_code == 200: @@ -41,12 +50,11 @@ def send_sms(mobile: str, message: str): headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {api_key}", + "X-API-Key": api_key, } data = { - "number": mobile, - "message": message, - "check_delivery": False, + "to": format_mobile(mobile), + "text": message, } response = requests.post(api_url, headers=headers, data=json.dumps(data)) if response.status_code == 200: diff --git a/djangopasswordlessknox/utils.py b/djangopasswordlessknox/utils.py index 7fb7a6a..63a5e1e 100644 --- a/djangopasswordlessknox/utils.py +++ b/djangopasswordlessknox/utils.py @@ -232,14 +232,15 @@ def send_sms_with_callback_token(user, mobile_token, **kwargs): logger.debug("Failed to send SMS. Missing SMS_API_URL or SMS_API_KEY.") return False + from api.notifications import format_mobile + headers = { "Content-Type": "application/json", - "Authorization": f"Bearer {api_key}", + "X-API-Key": api_key, } data = { - "number": to_number, - "message": base_string % mobile_token.key, - "check_delivery": False, + "to": format_mobile(to_number), + "text": base_string % mobile_token.key, } print(mobile_token.key) response = requests.post(api_url, headers=headers, data=json.dumps(data))