-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (154 loc) · 5.45 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const Alexa = require('ask-sdk-core');
const request = require('request-promise-native');
/*
------------------------------
- Custom task handlers -
------------------------------
*/
// General levels
const GeneralLevelsIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'NivelesGeneralesIntent';
},
async handle(handlerInput) {
const response = await request(prepareOptions("http://www.zaragoza.es/sede/servicio/informacion-polen"));
const json = JSON.parse(response.body);
var talkResult = "";
const resultElements = json.result;
resultElements.forEach(element => {
talkResult += element.title + ":" + element.observation[0].value + ",";
});
return handlerInput.responseBuilder
.speak(talkResult + '. ¿Puedo ayudarte en algo más?')
.reprompt('¿Puedo ayudarte en algo más?')
.getResponse();
}
};
// By level
const NivelConcretoIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'NivelConcretoIntent';
},
async handle(handlerInput) {
const response = await request(prepareOptions("http://www.zaragoza.es/sede/servicio/informacion-polen"));
const json = JSON.parse(response.body);
const level = handlerInput.requestEnvelope.request.intent.slots.level.value;
var talkResultLevel = "";
const resultElementsLevel = json.result;
resultElementsLevel.forEach(element => {
if(element.observation[0].value === level)
talkResultLevel += element.title + ",";
});
if(talkResultLevel === "")
talkResultLevel = "No hay medidas de nivel '" + level + "'";
return handlerInput.responseBuilder
.speak(talkResultLevel + '. ¿Puedo ayudarte en algo más?')
.reprompt('¿Puedo ayudarte en algo más?')
.getResponse();
}
};
/*
------------------------------
- Common task handlers -
------------------------------
*/
// Welcome
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'Bienvenido! ¿en qué puedo ayudarte?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// Help
const HelpIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'Puedes solicitar los niveles generales o especificar un nivel. ¿En que puedo ayudarte?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
// Cancel and stop
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Gracias por usar este servicio. Hasta luego!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
// Error
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`~~~~ Error handled: ${error.stack}`);
const speakOutput = `Lo siento. Se ha producido un error. <lang xml:lang="en-GB">` + error.message + `</lang>`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
/*
------------------------------
- Utility functions -
------------------------------
*/
// Prepare request params
function prepareOptions(url) {
return {
url: url,
headers: {
Accept: "application/json"
},
resolveWithFullResponse: true
};
}
// Session end
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
},
handle(handlerInput) {
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse();
}
};
/*
------------------------------------------
- Skill builder - Order matters -
------------------------------------------
*/
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
GeneralLevelsIntentHandler,
NivelConcretoIntentHandler,
LaunchRequestHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(
ErrorHandler,
)
.lambda();