Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for configurable sms backend #2024

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ requests = "==2.31.0"
sentry-sdk = "==1.30.0"
whitenoise = "==6.5.0"
redis-om = "==0.2.1"
django-sms = "==0.7.0"

[dev-packages]
black = "==23.9.1"
Expand Down
112 changes: 64 additions & 48 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions care/facility/api/serializers/patient_otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from django.utils.timezone import localtime, now
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from sms import send_sms

from care.facility.models.patient import PatientMobileOTP
from care.utils.sms.sendSMS import sendSMS


def rand_pass(size):
Expand All @@ -21,14 +21,14 @@ def rand_pass(size):
return generate_pass


def send_sms(otp, phone_number):
def send_msg(otp, phone_number):
if settings.USE_SMS:
sendSMS(
phone_number,
(
send_sms(
body=(
f"Open Healthcare Network Patient Management System Login, OTP is {otp} . "
"Please do not share this Confidential Login Token with anyone else"
),
recipients=[phone_number],
)
else:
print(otp, phone_number)
Expand Down Expand Up @@ -56,7 +56,7 @@ def create(self, validated_data):
otp_obj = super().create(validated_data)
otp = rand_pass(settings.OTP_LENGTH)

send_sms(otp, otp_obj.phone_number)
send_msg(otp, otp_obj.phone_number)

otp_obj.otp = otp
otp_obj.save()
Expand Down
9 changes: 4 additions & 5 deletions care/utils/notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.apps import apps
from django.conf import settings
from pywebpush import WebPushException, webpush
from sms import send_sms

from care.facility.models.daily_round import DailyRound
from care.facility.models.facility import Facility, FacilityUser
Expand All @@ -16,7 +17,6 @@
)
from care.facility.models.shifting import ShiftingRequest
from care.users.models import User
from care.utils.sms.sendSMS import sendSMS


class NotificationCreationException(Exception):
Expand Down Expand Up @@ -392,10 +392,9 @@ def generate(self):
medium == Notification.Medium.SMS.value
and settings.SEND_SMS_NOTIFICATION
):
sendSMS(
self.generate_sms_phone_numbers(),
self.generate_sms_message(),
many=True,
send_sms(
body=self.generate_sms_message(),
recipients=self.generate_sms_phone_numbers(),
)
elif medium == Notification.Medium.SYSTEM.value:
if not self.message:
Expand Down
36 changes: 35 additions & 1 deletion care/utils/sms/sendSMS.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import logging
from typing import List

import boto3
from django.conf import settings
from django.core.exceptions import ValidationError
from sms import Message
from sms.backends.base import BaseSmsBackend

from care.utils.models.validators import mobile_validator

logger = logging.getLogger(__name__)


def sendSMS(phone_numbers, message, many=False):
if not many:
Expand All @@ -11,7 +19,8 @@ def sendSMS(phone_numbers, message, many=False):
for phone in phone_numbers:
try:
mobile_validator(phone)
except Exception:
except ValidationError as e:
logger.info(e.messages[0])
continue
client = boto3.client(
"sns",
Expand All @@ -21,3 +30,28 @@ def sendSMS(phone_numbers, message, many=False):
)
client.publish(PhoneNumber=phone, Message=message)
return True


class CustomSMSBackendSNS(BaseSmsBackend):
def __init__(self):
self.client = boto3.client(
"sns",
aws_access_key_id=settings.SNS_ACCESS_KEY,
aws_secret_access_key=settings.SNS_SECRET_KEY,
region_name=settings.SNS_REGION,
)

def send_messages(self, messages: List[Message]):
for message in messages:
for phone_number in message.recipients:
message_body = message.body

try:
mobile_validator(phone_number)
except ValidationError as e:
logger.info(e.messages[0])
continue

self.client.publish(PhoneNumber=phone_number, Message=message_body)

return len(messages)
Loading