in Solutions/AWSSCV-VoicemailExpress/Code/awsscv_vmx_transcriber/awsscv_vmx_transcriber.py [0:0]
def lambda_handler(event, context):
logger.debug(event)
# Establish an empty response and loop counter
loop_counter = 0
# Process the incoming S3 event
for recording in event['Records']:
# Increment loop
loop_counter = loop_counter+1
# Grab incoming data elements from the S3 event
try:
recording_key = recording['s3']['object']['key']
recording_name = recording_key.replace('voicemail_recordings/','')
contact_id = recording_name.replace('.wav','')
recording_bucket = recording['s3']['bucket']['name']
except Exception as e:
logger.error(e)
logger.debug('Record {0} Result: Failed to extract data from event'.format(loop_counter))
continue
# Establish the S3 client and get the object tags
try:
s3_client = boto3.client('s3')
object_data = s3_client.get_object_tagging(
Bucket=recording_bucket,
Key=recording_key
)
object_tags = object_data['TagSet']
loaded_tags = {}
for i in object_tags:
loaded_tags.update({i['Key']:i['Value']})
except Exception as e:
logger.error(e)
logger.debug('Record {0} Result: Failed to extract tags from object'.format(loop_counter))
continue
# Build the Recording URL
try:
recording_url = 'https://{0}.s3-{1}.amazonaws.com/{2}'.format(recording_bucket, recording['awsRegion'], recording_key)
except Exception as e:
logger.error(e)
logger.debug('Record {0} Result: Failed to generate recording URL'.format(loop_counter))
continue
# Do the transcription
try:
# Esteablish the client
transcribe_client = boto3.client('transcribe')
# Submit the transcription job
transcribe_response = transcribe_client.start_transcription_job(
TranscriptionJobName=contact_id,
LanguageCode=loaded_tags['vm_lang'],
MediaFormat='wav',
Media={
'MediaFileUri': recording_url
},
OutputBucketName=os.environ['s3_transcripts_bucket']
)
except Exception as e:
logger.error(e)
logger.debug('Record {0} Result: Transcription job failed'.format(loop_counter))
continue
logger.debug('Record {0} Result: Success!'.format(loop_counter))
return {
'status': 'complete',
'result': '{0} records processed'.format(loop_counter)
}