2022-12-16 12:21:40 -05:00
|
|
|
from flask import Blueprint, current_app, json, jsonify, request
|
2017-05-22 11:26:47 +01:00
|
|
|
|
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.inbound_sms_dao import dao_create_inbound_sms
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_inbound_number
|
2024-01-10 12:45:03 -05:00
|
|
|
from app.enums import ServicePermissionType
|
2022-10-03 17:16:59 -07:00
|
|
|
from app.errors import InvalidRequest, register_errors
|
2024-01-10 12:45:03 -05:00
|
|
|
from app.models import InboundSms
|
2022-10-03 17:16:59 -07:00
|
|
|
from app.notifications.sns_handlers import sns_notification_handler
|
2024-05-16 10:17:45 -04:00
|
|
|
from notifications_utils.recipients import try_validate_and_format_phone_number
|
2017-03-16 18:15:49 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
receive_notifications_blueprint = Blueprint("receive_notifications", __name__)
|
2017-03-16 18:15:49 +00:00
|
|
|
register_errors(receive_notifications_blueprint)
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@receive_notifications_blueprint.route(
|
|
|
|
|
"/notifications/sms/receive/sns", methods=["POST"]
|
|
|
|
|
)
|
2022-10-03 09:05:34 -07:00
|
|
|
def receive_sns_sms():
|
|
|
|
|
"""
|
2022-10-04 17:42:04 -07:00
|
|
|
Expected value of the 'Message' key in the incoming payload from SNS
|
2022-10-03 09:05:34 -07:00
|
|
|
{
|
|
|
|
|
"originationNumber":"+14255550182",
|
|
|
|
|
"destinationNumber":"+12125550101",
|
|
|
|
|
"messageKeyword":"JOIN", # unique to our sending number
|
|
|
|
|
"messageBody":"EXAMPLE",
|
|
|
|
|
"inboundMessageId":"cae173d2-66b9-564c-8309-21f858e9fb84",
|
|
|
|
|
"previousPublishedMessageId":"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
|
|
|
}
|
|
|
|
|
"""
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-10-07 17:25:31 -07:00
|
|
|
# Whether or not to ignore inbound SMS replies
|
2023-08-29 14:54:30 -07:00
|
|
|
if not current_app.config["RECEIVE_INBOUND_SMS"]:
|
|
|
|
|
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
2022-10-14 14:45:27 +00:00
|
|
|
|
2022-10-03 09:05:34 -07:00
|
|
|
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
|
2023-08-29 14:54:30 -07:00
|
|
|
inbound_number = message["destinationNumber"].replace("+", "")
|
2022-10-03 09:05:34 -07:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
service = fetch_potential_service(inbound_number, "sns")
|
2022-10-03 09:05:34 -07:00
|
|
|
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
|
2022-10-14 14:45:27 +00:00
|
|
|
current_app.logger.warning(
|
2022-10-21 13:29:52 +00:00
|
|
|
f"Mapping between service and inbound number: {inbound_number} is broken, "
|
|
|
|
|
f"or service does not have permission to receive inbound sms"
|
2022-10-14 14:45:27 +00:00
|
|
|
)
|
2023-08-29 14:54:30 -07:00
|
|
|
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
2022-10-03 09:05:34 -07:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
inbound = create_inbound_sms_object(
|
|
|
|
|
service,
|
2023-10-03 07:31:24 -07:00
|
|
|
content=message.get("messageBody"),
|
|
|
|
|
from_number=message.get("originationNumber"),
|
|
|
|
|
provider_ref=message.get("inboundMessageId"),
|
|
|
|
|
date_received=post_data.get("Timestamp"),
|
|
|
|
|
provider_name="sns",
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2022-10-03 09:05:34 -07:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
tasks.send_inbound_sms_to_service.apply_async(
|
|
|
|
|
[str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY
|
|
|
|
|
)
|
2022-10-03 09:05:34 -07:00
|
|
|
|
|
|
|
|
current_app.logger.debug(
|
2023-08-29 14:54:30 -07:00
|
|
|
"{} received inbound SMS with reference {} from SNS".format(
|
|
|
|
|
service.id, inbound.provider_reference
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-10-03 09:05:34 -07:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
return jsonify(result="success", message="SMS-SNS callback succeeded"), 200
|
2022-10-03 09:05:34 -07:00
|
|
|
|
2020-06-29 20:31:17 +01:00
|
|
|
|
2017-11-08 13:32:30 +00:00
|
|
|
def unescape_string(string):
|
2023-08-29 14:54:30 -07:00
|
|
|
return string.encode("raw_unicode_escape").decode("unicode_escape")
|
2017-05-22 11:26:47 +01:00
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
def create_inbound_sms_object(
|
|
|
|
|
service, content, from_number, provider_ref, date_received, provider_name
|
|
|
|
|
):
|
2017-11-23 15:22:18 +00:00
|
|
|
user_number = try_validate_and_format_phone_number(
|
|
|
|
|
from_number,
|
|
|
|
|
international=True,
|
2023-08-29 14:54:30 -07:00
|
|
|
log_msg=f'Invalid from_number received for service "{service.id}"',
|
2017-11-23 15:22:18 +00:00
|
|
|
)
|
2017-06-02 10:14:01 +01:00
|
|
|
|
2017-06-20 17:13:40 +01:00
|
|
|
provider_date = date_received
|
2017-05-22 11:26:47 +01:00
|
|
|
inbound = InboundSms(
|
|
|
|
|
service=service,
|
2017-08-14 19:47:09 +01:00
|
|
|
notify_number=service.get_inbound_number(),
|
2017-05-22 11:26:47 +01:00
|
|
|
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,
|
2023-08-29 14:54:30 -07:00
|
|
|
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-08-23 13:03:52 +01:00
|
|
|
def fetch_potential_service(inbound_number, provider_name):
|
|
|
|
|
service = dao_fetch_service_by_inbound_number(inbound_number)
|
2017-06-20 17:13:40 +01:00
|
|
|
|
2017-08-23 13:03:52 +01:00
|
|
|
if not service:
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app.logger.warning(
|
|
|
|
|
'Inbound number "{}" from {} not associated with a service'.format(
|
|
|
|
|
inbound_number, provider_name
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-06-20 17:13:40 +01:00
|
|
|
return False
|
2017-06-29 15:33:44 +01:00
|
|
|
|
2017-08-23 13:03:52 +01:00
|
|
|
if not has_inbound_sms_permissions(service.permissions):
|
2017-06-29 15:33:44 +01:00
|
|
|
current_app.logger.error(
|
2023-08-29 14:54:30 -07:00
|
|
|
'Service "{}" does not allow inbound SMS'.format(service.id)
|
|
|
|
|
)
|
2017-06-29 15:33:44 +01:00
|
|
|
return False
|
|
|
|
|
|
2017-08-23 13:03:52 +01:00
|
|
|
return service
|
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]
|
2024-02-28 12:41:57 -05:00
|
|
|
return {ServicePermissionType.INBOUND_SMS, ServicePermissionType.SMS}.issubset(
|
|
|
|
|
set(str_permissions)
|
|
|
|
|
)
|