2
0
Fork 0

Actually send SMS if sipgate credentials are set

This commit is contained in:
Luca 2022-04-29 02:14:14 +02:00
parent 826c3a1a7a
commit 3647ecc77a
1 changed files with 11 additions and 2 deletions

View File

@ -1,11 +1,20 @@
from celery import shared_task from celery import shared_task
from .models import Message, ShiftRegistration from .models import Message, ShiftRegistration
from django.conf import settings
from django.db import transaction from django.db import transaction
from django.utils import timezone from django.utils import timezone
from dynamic_preferences.registries import global_preferences_registry from dynamic_preferences.registries import global_preferences_registry
from .sipgate.sms import send as send_sms
global_preferences = global_preferences_registry.manager() global_preferences = global_preferences_registry.manager()
def send(msg):
if not (settings.SIPGATE_SMS_EXTENSION and settings.SIPGATE_TOKEN and settings.SIPGATE_TOKEN_ID):
print(f"would send message to {msg.to.phone}\n---\n{msg.text}")
return
send_sms(msg.to.phone, msg.text)
# cron task to send normal messages(reminders,changes) in batches # cron task to send normal messages(reminders,changes) in batches
@shared_task @shared_task
def send_messages(): def send_messages():
@ -16,7 +25,7 @@ def send_messages():
for msg in Message.objects.select_for_update().filter(sent_at__isnull=True): for msg in Message.objects.select_for_update().filter(sent_at__isnull=True):
if msg.sent_at: if msg.sent_at:
continue continue
print(f"TODO: send message @{msg.to.phone} {msg.text}") send(msg)
msg.sent_at = timezone.now() msg.sent_at = timezone.now()
msg.save() msg.save()
@ -33,7 +42,7 @@ def send_message(msgid):
msg = Message.objects.select_for_update().get(pk=msgid) msg = Message.objects.select_for_update().get(pk=msgid)
if msg.sent_at: if msg.sent_at:
return return
print(f"TODO: send message @{msg.to.phone} {msg.text}") send(msg)
msg.sent_at = timezone.now() msg.sent_at = timezone.now()
msg.save() msg.save()