diff --git a/aleksis/core/templates/sms/notification.txt b/aleksis/core/templates/sms/notification.txt new file mode 100644 index 0000000000000000000000000000000000000000..2282970e81bcd0aff735bb0dd376357eddeccf04 --- /dev/null +++ b/aleksis/core/templates/sms/notification.txt @@ -0,0 +1 @@ +🔔 {{ notification.sender }}: {{ notification.description }} · {{ notification.link }} diff --git a/aleksis/core/util/notifications.py b/aleksis/core/util/notifications.py index e3650d544ce946e2c784aede93d4654770d22e5f..319518d764317c49b56e4c8f3e51f4be01cf371c 100644 --- a/aleksis/core/util/notifications.py +++ b/aleksis/core/util/notifications.py @@ -1,15 +1,33 @@ """ 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