"""Util functions related to detecting a message type from a sqs.Message.""" import json from notifications_delivery.constants.notifications import ( MESSAGE_TYPE_BOUNCE, MESSAGE_TYPE_COMPLAINT, MESSAGE_TYPE_NOTIFICATION, SES_BOUNCE_NOTIFICATION_TYPE, SES_COMPLAINT_NOTIFICATION_TYPE, ) def get_message_type(message): """Parse message and return the message type. Args: message (sqs.Message): message pulled from SQS-message. Returns: str: the message type. """ if 'Message' in message: body = json.loads(message['Message']) notification_type = body.get('notificationType') if notification_type == SES_BOUNCE_NOTIFICATION_TYPE: message_type = MESSAGE_TYPE_BOUNCE elif notification_type == SES_COMPLAINT_NOTIFICATION_TYPE: message_type = MESSAGE_TYPE_COMPLAINT else: message_type = None elif 'type' in message: return message['type'] else: message_type = MESSAGE_TYPE_NOTIFICATION return message_type