32 lines
868 B
Python
32 lines
868 B
Python
import sentry_sdk
|
|
from celery import shared_task
|
|
from django.conf import settings
|
|
|
|
from shiftregister.app.sipgate.history import list_incoming_sms
|
|
|
|
from .models import IncomingMessage
|
|
|
|
|
|
@shared_task
|
|
def receive_messages():
|
|
if not settings.SIPGATE_INCOMING_TOKEN or not settings.SIPGATE_INCOMING_TOKEN_ID:
|
|
return
|
|
|
|
try:
|
|
from_dt = IncomingMessage.objects.latest().created_at
|
|
except IncomingMessage.DoesNotExist:
|
|
from_dt = None
|
|
|
|
try:
|
|
for sms in reversed(list_incoming_sms(from_dt)):
|
|
IncomingMessage.objects.get_or_create(
|
|
id=sms.id,
|
|
defaults={
|
|
"content": sms.content,
|
|
"created_at": sms.created_at,
|
|
"sender": sms.sender,
|
|
},
|
|
)
|
|
except Exception as e:
|
|
sentry_sdk.capture_exception(e)
|