2017-05-22 11:26:47 +01:00
|
|
|
from urllib.parse import unquote
|
2017-03-16 18:15:49 +00:00
|
|
|
|
2017-06-02 10:14:01 +01:00
|
|
|
import iso8601
|
|
|
|
|
from flask import jsonify, Blueprint, current_app, request
|
2017-06-02 12:57:28 +01:00
|
|
|
from notifications_utils.recipients import validate_and_format_phone_number
|
2017-05-22 11:26:47 +01:00
|
|
|
|
2017-06-02 16:37:57 +01:00
|
|
|
from app import statsd_client, firetext_client, mmg_client
|
2017-06-20 17:13:40 +01:00
|
|
|
from app.celery import tasks
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2017-05-22 11:26:47 +01:00
|
|
|
from app.dao.services_dao import dao_fetch_services_by_sms_sender
|
|
|
|
|
from app.dao.inbound_sms_dao import dao_create_inbound_sms
|
2017-06-29 15:33:44 +01:00
|
|
|
from app.models import InboundSms, INBOUND_SMS_TYPE, SMS_TYPE
|
2017-03-16 18:15:49 +00:00
|
|
|
from app.errors import register_errors
|
2017-06-02 10:14:01 +01:00
|
|
|
from app.utils import convert_bst_to_utc
|
2017-03-16 18:15:49 +00:00
|
|
|
|
|
|
|
|
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
|
|
|
|
|
register_errors(receive_notifications_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receive_notifications_blueprint.route('/notifications/sms/receive/mmg', methods=['POST'])
|
|
|
|
|
def receive_mmg_sms():
|
2017-05-22 11:26:47 +01:00
|
|
|
"""
|
|
|
|
|
{
|
|
|
|
|
'MSISDN': '447123456789'
|
|
|
|
|
'Number': '40604',
|
|
|
|
|
'Message': 'some+uri+encoded+message%3A',
|
|
|
|
|
'ID': 'SOME-MMG-SPECIFIC-ID',
|
|
|
|
|
'DateRecieved': '2017-05-21+11%3A56%3A11'
|
|
|
|
|
}
|
|
|
|
|
"""
|
2017-03-16 18:15:49 +00:00
|
|
|
post_data = request.get_json()
|
2017-06-06 11:50:30 +01:00
|
|
|
|
|
|
|
|
inbound_number = strip_leading_forty_four(post_data['Number'])
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
potential_services = fetch_potential_services(inbound_number, 'mmg')
|
|
|
|
|
if not potential_services:
|
2017-06-29 15:33:44 +01:00
|
|
|
# since this is an issue with our service <-> number mapping, or no inbound_sms service permission
|
|
|
|
|
# we should still tell MMG that we received it successfully
|
2017-06-01 13:13:51 +01:00
|
|
|
return 'RECEIVED', 200
|
|
|
|
|
|
2017-06-05 11:51:30 +01:00
|
|
|
statsd_client.incr('inbound.mmg.successful')
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
service = potential_services[0]
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
inbound = create_inbound_sms_object(service,
|
|
|
|
|
content=format_mmg_message(post_data["Message"]),
|
|
|
|
|
from_number=post_data['MSISDN'],
|
|
|
|
|
provider_ref=post_data["ID"],
|
|
|
|
|
date_received=post_data.get('DateRecieved'),
|
|
|
|
|
provider_name="mmg")
|
2017-05-22 11:26:47 +01:00
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
|
2017-05-22 11:26:47 +01:00
|
|
|
|
|
|
|
|
return 'RECEIVED', 200
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
|
|
|
|
|
def receive_firetext_sms():
|
|
|
|
|
post_data = request.form
|
|
|
|
|
|
|
|
|
|
inbound_number = strip_leading_forty_four(post_data['destination'])
|
|
|
|
|
|
|
|
|
|
potential_services = fetch_potential_services(inbound_number, 'firetext')
|
|
|
|
|
if not potential_services:
|
|
|
|
|
return jsonify({
|
|
|
|
|
"status": "ok"
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
service = potential_services[0]
|
|
|
|
|
inbound = create_inbound_sms_object(service=service,
|
|
|
|
|
content=post_data["message"],
|
|
|
|
|
from_number=post_data['source'],
|
|
|
|
|
provider_ref=None,
|
|
|
|
|
date_received=post_data['time'],
|
|
|
|
|
provider_name="firetext")
|
|
|
|
|
|
|
|
|
|
statsd_client.incr('inbound.firetext.successful')
|
|
|
|
|
|
|
|
|
|
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
|
2017-06-21 15:29:55 +01:00
|
|
|
current_app.logger.info(
|
|
|
|
|
'{} received inbound SMS with reference {} from Firetext'.format(service.id, inbound.provider_reference))
|
2017-06-20 17:13:40 +01:00
|
|
|
return jsonify({
|
|
|
|
|
"status": "ok"
|
|
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
|
2017-06-02 10:14:01 +01:00
|
|
|
def format_mmg_message(message):
|
2017-05-22 11:26:47 +01:00
|
|
|
return unquote(message.replace('+', ' '))
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
|
2017-06-02 10:14:01 +01:00
|
|
|
def format_mmg_datetime(date):
|
|
|
|
|
"""
|
|
|
|
|
We expect datetimes in format 2017-05-21+11%3A56%3A11 - ie, spaces replaced with pluses, and URI encoded
|
|
|
|
|
(the same as UTC)
|
|
|
|
|
"""
|
|
|
|
|
orig_date = format_mmg_message(date)
|
|
|
|
|
parsed_datetime = iso8601.parse_date(orig_date).replace(tzinfo=None)
|
|
|
|
|
return convert_bst_to_utc(parsed_datetime)
|
|
|
|
|
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
def create_inbound_sms_object(service, content, from_number, provider_ref, date_received, provider_name):
|
|
|
|
|
user_number = validate_and_format_phone_number(from_number, international=True)
|
2017-06-02 10:14:01 +01:00
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
provider_date = date_received
|
2017-06-02 10:14:01 +01:00
|
|
|
if provider_date:
|
|
|
|
|
provider_date = format_mmg_datetime(provider_date)
|
|
|
|
|
|
2017-05-22 11:26:47 +01:00
|
|
|
inbound = InboundSms(
|
|
|
|
|
service=service,
|
|
|
|
|
notify_number=service.sms_sender,
|
|
|
|
|
user_number=user_number,
|
2017-06-02 10:14:01 +01:00
|
|
|
provider_date=provider_date,
|
2017-06-20 17:13:40 +01:00
|
|
|
provider_reference=provider_ref,
|
|
|
|
|
content=content,
|
|
|
|
|
provider=provider_name
|
2017-05-22 11:26:47 +01:00
|
|
|
)
|
|
|
|
|
dao_create_inbound_sms(inbound)
|
|
|
|
|
return inbound
|
2017-05-31 16:15:25 +01:00
|
|
|
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
def fetch_potential_services(inbound_number, provider_name):
|
2017-06-06 11:50:30 +01:00
|
|
|
potential_services = dao_fetch_services_by_sms_sender(inbound_number)
|
2017-06-20 17:13:40 +01:00
|
|
|
|
2017-06-02 15:58:36 +01:00
|
|
|
if len(potential_services) != 1:
|
2017-06-20 17:13:40 +01:00
|
|
|
current_app.logger.error('Inbound number "{}" from {} not associated with exactly one service'.format(
|
|
|
|
|
inbound_number, provider_name
|
2017-06-02 15:58:36 +01:00
|
|
|
))
|
2017-06-20 17:13:40 +01:00
|
|
|
statsd_client.incr('inbound.{}.failed'.format(provider_name))
|
|
|
|
|
return False
|
2017-06-29 15:33:44 +01:00
|
|
|
|
2017-07-05 15:08:53 +01:00
|
|
|
if not has_inbound_sms_permissions(potential_services[0].permissions):
|
2017-06-29 15:33:44 +01:00
|
|
|
current_app.logger.error(
|
|
|
|
|
'Service "{}" does not allow inbound SMS'.format(potential_services[0].id))
|
|
|
|
|
return False
|
|
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
return potential_services
|
2017-06-06 11:50:30 +01:00
|
|
|
|
|
|
|
|
|
2017-07-05 15:08:53 +01:00
|
|
|
def has_inbound_sms_permissions(permissions):
|
|
|
|
|
str_permissions = [p.permission for p in permissions]
|
|
|
|
|
return set([INBOUND_SMS_TYPE, SMS_TYPE]).issubset(set(str_permissions))
|
|
|
|
|
|
|
|
|
|
|
2017-06-06 11:50:30 +01:00
|
|
|
def strip_leading_forty_four(number):
|
|
|
|
|
if number.startswith('44'):
|
|
|
|
|
return number.replace('44', '0', 1)
|
|
|
|
|
return number
|