def lambda_handler()

in sam-app/lambda_functions/sfProcessContactLens.py [0:0]


def lambda_handler(event, context):
    # Check if Contact Lens is enabled at application level
    if os.environ["CONTACT_LENS_IMPORT_ENABLED"].lower() != 'true':
        logger.warning('Contact Lens Import is disabled')
        return {"Done": False}

    try:
        logger.info('Received event: %s' % json.dumps(event))

        event_record = event['Records'][0]
        bucket = event_record['s3']['bucket']['name']
        logger.info("ContactLens file bucket: %s" % bucket)

        key = urllib.parse.unquote(event_record['s3']['object']['key'])
        logger.info("ContactLens file key: %s" % key)

        logger.info('Retrieving ContactLens file: %s', key)
        contactLensObj = getS3FileJSONObject(bucket, key)
        logger.info('Retrieved ContactLens file: %s', key)

        contactId = contactLensObj['CustomerMetadata']['ContactId']

        # Check contact attributes
        contactAttributes = getContactAttributes(contactLensObj)
        if ("contactLensImportEnabled" not in contactAttributes or "contactLensImportEnabled" in contactAttributes and contactAttributes["contactLensImportEnabled"] != 'true'):
            logger.warning("Contact Lens import not enabled!")
            return {"Done": False}

        # Check if Connect instanceId in contact lens object matches env variable
        if not isValidContactLensData(contactLensObj):
            logger.warning('Wrong Contact Lens data for Amazon Connect instance %s', os.environ["AMAZON_CONNECT_INSTANCE_ID"])
            return {"Done": False}

        logger.info('Getting lock file metadata: %s ' % contactId)
        oMetadata = getS3FileMetadata(os.environ['TRANSCRIPTS_DESTINATION'], contactId)

        mACContactChannelAnalyticsId = None
        if 'ACContactChannelAnalyticsId'.lower() in oMetadata:
            mACContactChannelAnalyticsId = oMetadata['ACContactChannelAnalyticsId'.lower()]

        logger.info('Processing ContactLens transcript')
        participants = contactLensObj['Participants']
        ContactLensTranscripts = processContactLensTranscript(contactLensObj['Transcript'], participants)
        
        # customerTranscripts = ','.join(str(transcript) for transcript in ContactLensTranscripts['customerTranscripts'])
        # agentTranscripts = ','.join(str(transcript) for transcript in ContactLensTranscripts['agentTranscripts'])
        contactLensTranscripts = ContactLensTranscripts['finalTranscripts']
        
        logger.info('Processing Conversation Characteristics')
        contactLensConversationCharacteristics = processContactLensConversationCharacteristics(contactLensObj, bucket, contactLensTranscripts)

        createSalesforceObject(contactId, contactLensTranscripts, contactLensConversationCharacteristics, mACContactChannelAnalyticsId)

        logger.info('Updating s3 metadata')
        updateLock(os.environ['TRANSCRIPTS_DESTINATION'], contactId, oMetadata)

        logger.info('Done')
        return {"Done": True}
    except Exception as e:
        raise e