parse datetimes from mmg inbound sms

the DateRecieved field from MMG comes in with +s instead of spaces,
and uriencoded (the same as how they format their messages)
Make sure we decode this, and then convert to a UTC timestamp
This commit is contained in:
Leo Hemsted
2017-06-02 10:14:01 +01:00
parent cb219ffea8
commit d9bdacb5cd
2 changed files with 34 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
from urllib.parse import unquote
from flask import jsonify
from flask import Blueprint, current_app, request
import iso8601
from flask import jsonify, Blueprint, current_app, request
from notifications_utils.recipients import normalise_phone_number
from app import statsd_client
@@ -9,6 +9,7 @@ from app.dao.services_dao import dao_fetch_services_by_sms_sender
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.models import InboundSms
from app.errors import register_errors
from app.utils import convert_bst_to_utc
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
register_errors(receive_notifications_blueprint)
@@ -48,18 +49,33 @@ def receive_mmg_sms():
return 'RECEIVED', 200
def format_message(message):
def format_mmg_message(message):
return unquote(message.replace('+', ' '))
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)
def create_inbound_mmg_sms_object(service, json):
message = format_message(json['Message'])
message = format_mmg_message(json['Message'])
user_number = normalise_phone_number(json['MSISDN'])
provider_date = json.get('DateRecieved')
if provider_date:
provider_date = format_mmg_datetime(provider_date)
inbound = InboundSms(
service=service,
notify_number=service.sms_sender,
user_number=user_number,
provider_date=json.get('DateRecieved'),
provider_date=provider_date,
provider_reference=json.get('ID'),
content=message,
)