-
Notifications
You must be signed in to change notification settings - Fork 5
/
action.js
94 lines (80 loc) · 3.17 KB
/
action.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var watson = require('watson-developer-cloud');
// To create your Watson Tone Analyzer keys, head over to Bluemix and create the Tone Analyzer service
var tone_analyzer = watson.tone_analyzer({
username: '<username>',
password: '<password>',
version: 'v3',
version_date: '2016-05-19'
});
// array of quotes
var quotes = [
"Life isn’t about getting and having, it’s about giving and being.",
"Whatever the mind of man can conceive and believe, it can achieve.",
"Strive not to be a success, but rather to be of value.", "Albert Einstein",
"Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.",
"I attribute my success to this: I never gave or took any excuse.",
"You miss 100% of the shots you don’t take.",
"The most difficult thing is the decision to act, the rest is merely tenacity.",
"happy wife means happy life"
];
// Function to return random value
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Return random quote
function randomQuote() {
return quotes[getRandomInt(0, quotes.length-1)];
};
//OpenWhisk action, expects to return something
function main(args) {
var intent = args.request.intent;
var text = '';
// Return to Alexa random quote
if(intent.name === 'randomQuote') {
text += randomQuote();
var response = {
"version": "1.0",
"response" :{
"shouldEndSession": true,
"outputSpeech": {
"type": "PlainText",
"text": "Here is a random Quote: " + text
}
}
};
return response;
}
// Return to Alexa a happy/Joy quote
if(intent.name === 'happyQuote') {
return new Promise( (resolve, reject) => {
// loop over all the quotes
quotes.forEach(function(quote){
tone_analyzer.tone({text:quote}, (err, tone) => {
if(err){
return reject(err);
}else {
tone.document_tone.tone_categories[0].tones.forEach(function(entry) {
// check to find joy/happy quotes
if(entry.tone_name == "Joy" && entry.score >= 0.5){
text = "Here is a happy Quote that I found for you: " + quote;
console.log(tone);
var response = {
"version": "1.0",
"response" :{
"shouldEndSession": true,
"outputSpeech": {
"type": "PlainText",
"text": text
}
}
};
return resolve(response);
}
});
}
});
});
});
}
}
exports.main = main;