Skip to content
Snippets Groups Projects
Verified Commit d0ba42f0 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Implement SMS sending for notifications

parent 2f49ce1b
No related branches found
No related tags found
1 merge request!137Generalise notifications and implement SMS notifications
🔔 {{ notification.sender }}: {{ notification.description }} · {{ notification.link }}
""" Utility code for notification system """
from typing import Sequence
from django.conf import settings
from django.template.loader import get_template
from django.utils.translation import gettext_lazy as _
from constance import config
from templated_email import send_templated_mail
try:
from twilio.rest import Client as TwilioClient
except ImportError:
TwilioClient = None
from ..models import Notification
from .core_helpers import celery_optional
def send_templated_sms(template_name: str, from_number: str, recipient_list: Sequence[str], context: dict):
""" Render a plan-text template and send via SMS to all recipients. """
template = get_template(template_name)
text = template.render(context)
client = TwilioClient(settings.TWILIO_SID, settings.TWILIO_TOKEN)
for recipient in recipient_list:
client.messages.create(body=text, to=recipient, from_=from_number)
def _send_notification_email(notification: Notification, template: str = "notification") -> None:
context = {
"notification": notification,
......@@ -23,6 +41,19 @@ def _send_notification_email(notification: Notification, template: str = "notifi
)
def _send_notification_sms(notification: Notification, template: str = "sms/notification.txt") -> None:
context = {
"notification": notification,
"notification_user": notification.person.adressing_name,
}
send_templated_sms(
template_name=template,
from_number=settings.TWILIO_CALLER_ID,
recipient_list=[notification.person.mobile_number],
context=context,
)
# Mapping of channel id to name and two functions:
# - Check for availability
# - Send notification through it
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment