-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (58 loc) · 1.52 KB
/
index.js
File metadata and controls
64 lines (58 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const AWS = require('aws-sdk')
AWS.config.update({region: 'us-east-1'})
const uuidV4 = require('uuid/v4')
const translate = new AWS.Translate()
const polly = new AWS.Polly()
const s3 = new AWS.S3({
params: {
Bucket: 'translatebucket',
}
})
exports.handler = (event, context, callback) => {
// Step 1 translate the text
let message = ''
const translateParams = {
SourceLanguageCode: 'en',
TargetLanguageCode: event.code,
Text: event.sentence
}
translate.translateText(translateParams, function (err, data) {
if (err) callback(err)
message = data.TranslatedText
const voices = {
'es': 'Penelope',
'pt': 'Vitoria',
'de': 'Vicki',
'en': 'Joanna',
'fr': 'Celine'
}
const voice = voices[event.code]
const pollyParams = {
OutputFormat: "mp3",
SampleRate: "8000",
Text: message,
TextType: "text",
VoiceId: voice
};
// step 2: synthesize the translation into speech
polly.synthesizeSpeech(pollyParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
let key = uuidV4()
const params2 = {
Key: key,
ContentType: 'audio/mpeg',
Body: data.AudioStream,
ACL: 'public-read'
};
s3.putObject(params2, function(err, data) {
if (err) {
callback('error putting item: ', err)
} else {
callback(null, { sentence: key })
}
});
}
});
});
};