-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (63 loc) · 2.25 KB
/
index.js
File metadata and controls
73 lines (63 loc) · 2.25 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
65
66
67
68
69
70
71
72
73
'use strict';
const REPEAT_PREFIX = [
"Sorry, I said: ",
"Let me repeat that. ",
"Sure, I said: ",
"I said: "
];
class VoiceRepeater {
constructor(app) {
const originalAsk = app.ask.bind(app);
this.app = app;
let lastPromptWithPrefix = this.lastPromptWithPrefix.bind(this);
let lastPrompt = this.lastPrompt.bind(this);
app.ask = function (response) {
let textToSpeech = "";
if (response.hasOwnProperty("items")) {
for (let item of response.items) {
if (item.hasOwnProperty("simpleResponse")) {
let simpleResponse = item.simpleResponse;
if (simpleResponse.hasOwnProperty("textToSpeech")) {
textToSpeech = simpleResponse.textToSpeech;
}
}
}
} else if (response.hasOwnProperty("speech")) {
textToSpeech = response.speech;
} else if (typeof response !== 'object') {
textToSpeech = response;
}
// Check to see if last prompt was a repeat prompt with prefix.
if (textToSpeech === lastPromptWithPrefix()) {
textToSpeech = lastPrompt();
}
const ssmlOpenTag = "<SSML>";
let repeatPrefix = getRandomeElementInArray(REPEAT_PREFIX);
const lastStatement = textToSpeech;
if (lastStatement.startsWith(ssmlOpenTag)) {
lastStatement = lastStatement.slice(ssmlOpenTag.length);
repeatPrefix = ssmlOpenTag + repeatPrefix;
}
app.setContext("last_prompt", 100,
{
"last_prompt": textToSpeech,
"prefixed_last_prompt": repeatPrefix + lastStatement,
});
originalAsk(response);
}
}
lastPromptWithPrefix() {
return this.app.getContext("last_prompt") !== null
? this.app.getContextArgument("last_prompt", "prefixed_last_prompt").value
: "um....I don't remember what I said!";
}
lastPrompt() {
return this.app.getContext("last_prompt") !== null
? this.app.getContextArgument("last_prompt", "last_prompt").value
: "um....I don't remember what I said!";
}
}
function getRandomeElementInArray(someArray) {
return someArray[Math.floor(Math.random()*someArray.length)]
}
exports.VoiceRepeater = VoiceRepeater;