2016-02-16 15:28:30 +00:00
|
|
|
from app import notify_celery, twilio_client, db, encryption
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.clients.sms.twilio import TwilioClientException
|
|
|
|
|
from app.dao.templates_dao import get_model_templates
|
2016-02-16 17:17:02 +00:00
|
|
|
from app.dao.notifications_dao import save_notification
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.models import Notification
|
|
|
|
|
from flask import current_app
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
@notify_celery.task(name="send-sms")
|
2016-02-16 15:28:30 +00:00
|
|
|
def send_sms(service_id, notification_id, encrypted_notification):
|
|
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-02-15 16:01:14 +00:00
|
|
|
template = get_model_templates(notification['template'])
|
|
|
|
|
|
|
|
|
|
status = 'sent'
|
|
|
|
|
|
|
|
|
|
try:
|
2016-02-16 17:17:02 +00:00
|
|
|
twilio_client.send_sms(notification['to'], template.content)
|
2016-02-15 16:01:14 +00:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
save_notification(notification_db_object)
|