mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 10:42:41 -05:00
modify inbound notif processing
This commit is contained in:
@@ -2,15 +2,16 @@ from datetime import datetime
|
||||
from urllib.parse import unquote
|
||||
|
||||
import iso8601
|
||||
from flask import Blueprint, abort, current_app, jsonify, request
|
||||
from flask import Blueprint, abort, current_app, jsonify, request, json
|
||||
from gds_metrics.metrics import Counter
|
||||
from notifications_utils.recipients import try_validate_and_format_phone_number
|
||||
|
||||
from app.celery import tasks
|
||||
from app.celery.validate_sns_message import sns_notification_handler
|
||||
from app.config import QueueNames
|
||||
from app.dao.inbound_sms_dao import dao_create_inbound_sms
|
||||
from app.dao.services_dao import dao_fetch_service_by_inbound_number
|
||||
from app.errors import register_errors
|
||||
from app.errors import register_errors, InvalidRequest
|
||||
from app.models import INBOUND_SMS_TYPE, SMS_TYPE, InboundSms
|
||||
|
||||
receive_notifications_blueprint = Blueprint('receive_notifications', __name__)
|
||||
@@ -23,6 +24,64 @@ INBOUND_SMS_COUNTER = Counter(
|
||||
['provider']
|
||||
)
|
||||
|
||||
@receive_notifications_blueprint.route('/notifications/sms/receive/sns', methods=['POST'])
|
||||
def receive_sns_sms():
|
||||
"""
|
||||
{
|
||||
"originationNumber":"+14255550182",
|
||||
"destinationNumber":"+12125550101",
|
||||
"messageKeyword":"JOIN", # unique to our sending number
|
||||
"messageBody":"EXAMPLE",
|
||||
"inboundMessageId":"cae173d2-66b9-564c-8309-21f858e9fb84",
|
||||
"previousPublishedMessageId":"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
||||
}
|
||||
"""
|
||||
|
||||
try:
|
||||
post_data = sns_notification_handler(request.data, request.headers)
|
||||
except Exception as e:
|
||||
raise InvalidRequest(f"SMS-SNS callback failed with error: {e}", 400)
|
||||
|
||||
message = json.loads(post_data.get("Message"))
|
||||
# TODO wrap this up
|
||||
if "inboundMessageId" in message:
|
||||
# TODO use standard formatting we use for all US numbers
|
||||
inbound_number = message['destinationNumber'].replace('+','')
|
||||
|
||||
service = fetch_potential_service(inbound_number, 'sns')
|
||||
if not service:
|
||||
# since this is an issue with our service <-> number mapping, or no inbound_sms service permission
|
||||
# we should still tell SNS that we received it successfully
|
||||
current_app.logger.warning(f"Mapping between service and inbound number: {inbound_number} is broken, or service does not have permission to receive inbound sms")
|
||||
return jsonify(
|
||||
result="success", message="SMS-SNS callback succeeded"
|
||||
), 200
|
||||
|
||||
INBOUND_SMS_COUNTER.labels("sns").inc()
|
||||
|
||||
content = message.get("messageBody")
|
||||
from_number = message.get('originationNumber')
|
||||
provider_ref = message.get('inboundMessageId')
|
||||
date_received = post_data.get('Timestamp')
|
||||
provider_name = "sns"
|
||||
|
||||
inbound = create_inbound_sms_object(service,
|
||||
content=content,
|
||||
from_number=from_number,
|
||||
provider_ref=provider_ref,
|
||||
date_received=date_received,
|
||||
provider_name=provider_name)
|
||||
|
||||
# TODO ensure inbound sms callback endpoints are accessible and functioning for notify api users, then uncomment the task below
|
||||
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
|
||||
|
||||
current_app.logger.debug(
|
||||
'{} received inbound SMS with reference {} from SNS'.format(service.id, inbound.provider_reference))
|
||||
|
||||
return jsonify(
|
||||
result="success", message="SMS-SNS callback succeeded"
|
||||
), 200
|
||||
|
||||
|
||||
@receive_notifications_blueprint.route('/notifications/sms/receive/mmg', methods=['POST'])
|
||||
def receive_mmg_sms():
|
||||
|
||||
Reference in New Issue
Block a user