Skip to content

Commit 527e2e3

Browse files
Add SMS configuration with environment variables only (no hardcoded secrets)
1 parent 0fdeee1 commit 527e2e3

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

‎api/src/sms/sms.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import os
2+
import requests
3+
from twilio.rest import Client
4+
from src.logger.logger import logger
5+
6+
# -----------------------
7+
# GLOBAL VARIABLES
8+
# -----------------------
9+
# WhatsApp CONFIG - All values must be set via environment variables
10+
TWILIO_ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID")
11+
TWILIO_AUTH_TOKEN = os.environ.get("TWILIO_AUTH_TOKEN")
12+
TWILIO_WHATSAPP_NUMBER = os.environ.get("TWILIO_WHATSAPP_NUMBER")
13+
TWILIO_MESSAGING_SERVICE_ID = os.environ.get("TWILIO_MESSAGING_SERVICE_ID")
14+
TWILIO_CONTENT_ID = os.environ.get("TWILIO_CONTENT_ID")
15+
# SMS CONFIG - All values must be set via environment variables
16+
SMS_API_ENDPOINT = os.environ.get("SMS_API_ENDPOINT")
17+
SMS_SENDER_USERNAME = os.environ.get("SMS_SENDER_USERNAME")
18+
SMS_SENDER_PASSWORD = os.environ.get("SMS_SENDER_PASSWORD")
19+
SMS_SENDER = os.environ.get("SMS_SENDER")
20+
SMS_SENDER_MODE = os.environ.get("SMS_SENDER_MODE", "1")
21+
22+
# -----------------------
23+
# FUNCTIONS
24+
# -----------------------
25+
26+
def send_sms(to_number: str, message: str):
27+
"""Send SMS using KWT SMS API."""
28+
params = {
29+
"username": SMS_SENDER_USERNAME,
30+
"password": SMS_SENDER_PASSWORD,
31+
"sender": SMS_SENDER,
32+
"mobile": to_number.replace("+", ""),
33+
"lang": "1",
34+
"test": SMS_SENDER_MODE,
35+
"message": message
36+
}
37+
try:
38+
resp = requests.post(SMS_API_ENDPOINT, data=params)
39+
if resp.text:
40+
return {"success": True, "data": resp.text}
41+
else:
42+
logger.error("Failed to send SMS: Empty response from SMS API", module="SMS")
43+
return {"success": False, "message": "Failed to send message"}
44+
except Exception as e:
45+
logger.error(f"Error sending SMS to {to_number}: {e}", exc_info=True, module="SMS")
46+
return {"success": False, "error": str(e)}
47+
48+
def send_whatsapp(to_number: str, otp: str):
49+
"""Send WhatsApp message via Twilio."""
50+
try:
51+
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
52+
message = client.messages.create(
53+
from_=f"whatsapp:{TWILIO_WHATSAPP_NUMBER}",
54+
to=f"whatsapp:{to_number}",
55+
body=f"Your OTP is: {otp}"
56+
)
57+
return {"success": True, "sid": message.sid}
58+
except Exception as e:
59+
logger.error(f"Error sending WhatsApp to {to_number}: {e}", exc_info=True, module="SMS")
60+
return {"success": False, "error": str(e)}
61+

0 commit comments

Comments
 (0)