mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Basic (Very basic) implementation of the fire text API.
[https://www.firetext.co.uk/docs#sendingsms](https://www.firetext.co.uk/docs#sendingsms) Not to be merged. This API has a limit on it at the moment that will need to be removed before it is used in anger.
This commit is contained in:
@@ -8,12 +8,14 @@ from werkzeug.local import LocalProxy
|
||||
from utils import logging
|
||||
from app.celery.celery import NotifyCelery
|
||||
from app.clients.sms.twilio import TwilioClient
|
||||
from app.clients.sms.firetext import FiretextClient
|
||||
from app.encryption import Encryption
|
||||
|
||||
db = SQLAlchemy()
|
||||
ma = Marshmallow()
|
||||
notify_celery = NotifyCelery()
|
||||
twilio_client = TwilioClient()
|
||||
firetext_client = FiretextClient()
|
||||
encryption = Encryption()
|
||||
|
||||
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
||||
@@ -30,6 +32,7 @@ def create_app():
|
||||
init_app(application)
|
||||
logging.init_app(application)
|
||||
twilio_client.init_app(application)
|
||||
firetext_client.init_app(application)
|
||||
notify_celery.init_app(application)
|
||||
encryption.init_app(application)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from app import notify_celery, twilio_client, encryption
|
||||
from app import notify_celery, twilio_client, encryption, firetext_client
|
||||
from app.clients.sms.twilio import TwilioClientException
|
||||
from app.dao.templates_dao import get_model_templates
|
||||
from app.dao.notifications_dao import save_notification
|
||||
@@ -24,6 +24,7 @@ def send_sms(service_id, notification_id, encrypted_notification):
|
||||
|
||||
try:
|
||||
twilio_client.send_sms(notification['to'], template.content)
|
||||
firetext_client.send_sms(notification['to'], template.content)
|
||||
except TwilioClientException as e:
|
||||
current_app.logger.debug(e)
|
||||
save_notification(notification_db_object, {"status": "failed"})
|
||||
|
||||
55
app/clients/sms/firetext.py
Normal file
55
app/clients/sms/firetext.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import logging
|
||||
from app.clients.sms import (
|
||||
SmsClient,
|
||||
SmsClientException
|
||||
)
|
||||
from requests import request, RequestException, HTTPError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FiretextClientException(SmsClientException):
|
||||
pass
|
||||
|
||||
|
||||
class FiretextClient(SmsClient):
|
||||
'''
|
||||
FireText sms client.
|
||||
'''
|
||||
|
||||
def init_app(self, config, *args, **kwargs):
|
||||
super(SmsClient, self).__init__(*args, **kwargs)
|
||||
print(config.config)
|
||||
self.api_key = config.config.get('FIRETEXT_API_KEY')
|
||||
self.from_number = config.config.get('FIRETEXT_NUMBER')
|
||||
|
||||
def send_sms(self, to, content):
|
||||
|
||||
data = {
|
||||
"apiKey": self.api_key,
|
||||
"from": self.from_number,
|
||||
"to": to.replace('+', ''),
|
||||
"message": content
|
||||
}
|
||||
|
||||
print(data)
|
||||
|
||||
try:
|
||||
response = request(
|
||||
"POST",
|
||||
"https://www.firetext.co.uk/api/sendsms",
|
||||
data=data
|
||||
)
|
||||
response.raise_for_status()
|
||||
except RequestException as e:
|
||||
api_error = HTTPError.create(e)
|
||||
print(
|
||||
"API {} request on {} failed with {} '{}'".format(
|
||||
"POST",
|
||||
"https://www.firetext.co.uk/api/sendsms",
|
||||
api_error.status_code,
|
||||
api_error.message
|
||||
)
|
||||
)
|
||||
raise api_error
|
||||
return response
|
||||
@@ -41,6 +41,8 @@ class Config(object):
|
||||
TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID')
|
||||
TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN')
|
||||
TWILIO_NUMBER = os.getenv('TWILIO_NUMBER')
|
||||
FIRETEXT_NUMBER = os.getenv('FIRETEXT_NUMBER')
|
||||
FIRETEXT_API_KEY = os.getenv("FIRETEXT_API_KEY")
|
||||
|
||||
|
||||
class Development(Config):
|
||||
|
||||
Reference in New Issue
Block a user