(event, context)
| 6 | translate_c = boto3.client('translate') |
| 7 | |
| 8 | def handler(event, context): |
| 9 | try: |
| 10 | voice = event["queryStringParameters"]["voice"] |
| 11 | except KeyError: |
| 12 | voice = 'Matthew' |
| 13 | |
| 14 | try: |
| 15 | translate_from = event["queryStringParameters"]["translateFrom"] |
| 16 | except KeyError: |
| 17 | translate_from = 'en' |
| 18 | |
| 19 | try: |
| 20 | translate_to = event["queryStringParameters"]["translateTo"] |
| 21 | except KeyError: |
| 22 | translate_to = 'en' |
| 23 | |
| 24 | try: |
| 25 | text = event['body'] |
| 26 | except KeyError: |
| 27 | text = 'To hear your own script, you need to include text in the message body of your restful request to the API Gateway' |
| 28 | |
| 29 | # Only perform a translation if the languages are different |
| 30 | if translate_to != translate_from: |
| 31 | text = translate_text(text, translate_from, translate_to) |
| 32 | |
| 33 | speech = convert_text_to_speech(voice, text) |
| 34 | |
| 35 | return { |
| 36 | 'statusCode': 200, |
| 37 | 'headers': { 'Content-Type': 'audio/mpeg' }, |
| 38 | 'body': base64.b64encode(speech), |
| 39 | 'isBase64Encoded': True |
| 40 | } |
| 41 | |
| 42 | def translate_text(text, translate_from, translate_to): |
| 43 | response = translate_c.translate_text( |
nothing calls this directly
no test coverage detected