-
Notifications
You must be signed in to change notification settings - Fork 0
/
modernDictionaryAlexaSkill.js
104 lines (92 loc) · 3.58 KB
/
modernDictionaryAlexaSkill.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
95
96
97
98
99
100
101
102
103
104
var https = require('https');
var swearjar = require('swearjar');
var appID = process.env['app_ID'];
exports.handler = (event, context) => {
try {
if (event.session.application.applicationId != appID) {
throw "Not a valid App ID";
}
switch (event.request.type) {
case 'LaunchRequest':
launchText = 'Modern Dictionary is a skill to learn new funny phrases and words by giving you the definition. Please ask for a random definition now or say stop to exit.';
returnResponse(launchText, false);
break;
case 'IntentRequest':
switch(event.request.intent.name) {
case 'GetRandomDefinition':
getRandomDefinition();
break;
case 'AMAZON.CancelIntent':
case 'AMAZON.StopIntent':
returnResponse("Goodbye", true);
break;
case 'AMAZON.HelpIntent':
helpText = "Modern Dictionary gives modern day definitions to words and phrases. To hear one, please ask Modern Dictionary for a random word now.";
returnResponse(helpText, false);
break;
}
break;
case 'SessionEndedRequest':
break;
default:
context.fail('INVALID REQUEST TYPE: $(event.request.type)');
}
}
catch(error) { context.fail('Exception: $(error)'); }
function getRandomDefinition() {
var endpoint = 'https://api.urbandictionary.com/v0/random';
var body = '';
https.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk; });
response.on('end', () => {
var data = JSON.parse(body);
var foundCleanWord = false
for (var i = 0; i < data.list.length; i++) {
var word = data.list[i].word;
// Check to see if the word contains profanities
if (!swearjar.profane(word)) {
var definition = data.list[i].definition;
// Censor the definition of profanities
cleanDefinition = swearjar.censor(definition);
var result = 'The random word is ' + word + '. The definition is ' + cleanDefinition;
foundCleanWord = true;
returnResponse(result, true);
}
else {
// Useful for debugging retries
console.log('Profane word was ' + word + ", retrying...");
continue;
}
}
if (!foundCleanWord) {
returnResponse("Sorry I was not able to find a clean defintion, please try again later", true);
}
});
});
}
function returnResponse(text, shouldEndSession) {
context.succeed(
generateResponse(
buildSpeechletResponse(text, shouldEndSession),
{}
)
);
}
};
// Helpers
buildSpeechletResponse = (outputText, shouldEndSession) => {
return {
outputSpeech: {
type: 'PlainText',
text: outputText
},
shouldEndSession: shouldEndSession
};
};
generateResponse = (speechletResponse, sessionAttributes) => {
return {
version: '1.0',
sessionAttributes: sessionAttributes,
response: speechletResponse
};
};