mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-15 09:42:38 -05:00
Made SMS messages go through celery
- twilio client pulled in from delivery app - made method to perform task
This commit is contained in:
@@ -1,9 +1,33 @@
|
||||
from app import celery
|
||||
from app.dao.services_dao import get_model_services
|
||||
from itsdangerous import URLSafeSerializer
|
||||
from app import celery, twilio_client, db
|
||||
from app.clients.sms.twilio import TwilioClientException
|
||||
from app.dao.templates_dao import get_model_templates
|
||||
from app.models import Notification
|
||||
from flask import current_app
|
||||
|
||||
|
||||
@celery.task(name="refresh-services")
|
||||
def refresh_services():
|
||||
print(get_model_services())
|
||||
for service in get_model_services():
|
||||
celery.control.add_consumer(str(service.id))
|
||||
@celery.task(name="send-sms", bind="True")
|
||||
def send_sms(service_id, notification_id, encrypted_notification, secret_key, salt):
|
||||
serializer = URLSafeSerializer(secret_key)
|
||||
|
||||
notification = serializer.loads(encrypted_notification, salt=salt)
|
||||
template = get_model_templates(notification['template'])
|
||||
|
||||
status = 'sent'
|
||||
|
||||
try:
|
||||
twilio_client.send_sms(notification, template.content)
|
||||
except TwilioClientException as e:
|
||||
current_app.logger.info(e)
|
||||
status = 'failed'
|
||||
|
||||
notification_db_object = Notification(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
status=status
|
||||
)
|
||||
|
||||
db.session.add(notification_db_object)
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user