Made SMS messages go through celery

- twilio client pulled in from delivery app
- made method to perform task
This commit is contained in:
Martyn Inglis
2016-02-15 16:01:14 +00:00
parent ffbe94f390
commit 223cb8c2dd
9 changed files with 145 additions and 31 deletions

13
app/clients/__init__.py Normal file
View File

@@ -0,0 +1,13 @@
class ClientException(Exception):
'''
Base Exceptions for sending notifications that fail
'''
pass
class Client(object):
'''
Base client for sending notifications.
'''
pass

View File

@@ -0,0 +1,17 @@
from app.clients import (Client, ClientException)
class SmsClientException(ClientException):
'''
Base Exception for SmsClients
'''
pass
class SmsClient(Client):
'''
Base Sms client for sending smss.
'''
def send_sms(self, *args, **kwargs):
raise NotImplemented('TODO Need to implement.')

48
app/clients/sms/twilio.py Normal file
View File

@@ -0,0 +1,48 @@
import logging
from app.clients.sms import (
SmsClient, SmsClientException)
from twilio.rest import TwilioRestClient
from twilio import TwilioRestException
logger = logging.getLogger(__name__)
class TwilioClientException(SmsClientException):
pass
class TwilioClient(SmsClient):
'''
Twilio sms client.
'''
def init_app(self, config, *args, **kwargs):
super(TwilioClient, self).__init__(*args, **kwargs)
self.client = TwilioRestClient(
config.config.get('TWILIO_ACCOUNT_SID'),
config.config.get('TWILIO_AUTH_TOKEN'))
self.from_number = config.config.get('TWILIO_NUMBER')
print(config.config)
def send_sms(self, notification, content):
try:
response = self.client.messages.create(
body=content,
to=notification['to'],
from_=self.from_number
)
return response.sid
except TwilioRestException as e:
logger.exception(e)
raise TwilioClientException(e)
def status(self, message_id):
try:
response = self.client.messages.get(message_id)
if response.status in ('delivered', 'undelivered', 'failed'):
return response.status
return None
except TwilioRestException as e:
logger.exception(e)
raise TwilioClientException(e)