-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
244 lines (224 loc) · 8.38 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const Alexa = require('ask-sdk-core');
const {AddAudioPlayerHttpHeadersRequestInterceptor, SetDefaultAudioPlayerHttpHeadersRequestInterceptor} = require('@warren-bank/alexa-skill-library-audio-player-http-request-headers')
const LaunchRequestHandler = {
canHandle(handlerInput) {
return (Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest');
},
handle(handlerInput) {
const speakOutput = 'Welcome, you can say "play audio" to start listening to music. What would you like to do?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
/**
* Intent handler to start playing an audio file.
* By default, it will play a specific audio stream.
* */
const PlayAudioIntentHandler = {
canHandle(handlerInput) {
return (
(Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest') &&
(
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'PlayAudioIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ResumeIntent')
)
);
},
handle(handlerInput) {
const speakOutput = 'Playing the audio stream.';
const playBehavior = 'REPLACE_ALL';
// https://requestbin.com/
// https://requestbin.com/r
// https://requestbin.com/r/en0m33feca4osq
const audioStreamUrl = 'https://en0m33feca4osq.x.pipedream.net/audio_track.mp3';
return handlerInput.responseBuilder
.speak(speakOutput)
.addAudioPlayerPlayDirective(
playBehavior,
audioStreamUrl,
'mytoken',
0
)
// .removeAudioPlayerHttpHeaders()
.addAudioPlayerHttpHeaders([{
"name": "X-Alexa-Issue",
"value": "https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/issues/610#issuecomment-1483729767"
}])
.getResponse();
}
};
/**
* Intent handler to start playing an audio file.
* By default, it will play a specific audio stream.
* */
const PauseAudioIntentHandler = {
canHandle(handlerInput) {
return (
(Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest') &&
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PauseIntent')
);
},
handle(handlerInput) {
return handlerInput.responseBuilder
.addAudioPlayerStopDirective()
.getResponse();
}
};
/**
* Intent handler for built-in intents that aren't supported in this sample skill.
* As this is a sample skill for a single stream, these intents are irrelevant to this skill.
* Regardless, the skill needs to handle this gracefully, which is why this handler exists.
* */
const UnsupportedAudioIntentHandler = {
canHandle(handlerInput) {
return (
(Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest') &&
(
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.LoopOffIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.LoopOnIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NextIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PreviousIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.RepeatIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ShuffleOffIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ShuffleOnIntent') ||
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StartOverIntent')
)
);
},
handle(handlerInput) {
const speakOutput = 'Sorry, I can\'t support that yet.';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return (
(Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest') &&
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent')
);
},
handle(handlerInput) {
const speakOutput = 'You can say "play audio" to start playing music! How can I help?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
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 = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
};
/* *
* SystemExceptions can be triggered if there is a problem with the audio that is trying to be played.
* This handler will log the details of the exception and can help troubleshoot issues with audio playback.
* */
const SystemExceptionHandler = {
canHandle(handlerInput) {
return (handlerInput.requestEnvelope.request.type === 'System.ExceptionEncountered');
},
handle(handlerInput) {
console.log(`System exception encountered: ${JSON.stringify(handlerInput.requestEnvelope.request)}`);
},
};
/* *
* FallbackIntent triggers when a customer says something that doesn’t map to any intents in your skill
* It must also be defined in the language model (if the locale supports it)
* This handler can be safely added but will be ingnored in locales that do not support it yet
* */
const FallbackIntentHandler = {
canHandle(handlerInput) {
return (
(Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest') &&
(Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent')
);
},
handle(handlerInput) {
const speakOutput = 'Sorry, I don\'t know about that. Please try again.';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
/* *
* SessionEndedRequest notifies that a session was ended. This handler will be triggered when a currently open
* session is closed for one of the following reasons: 1) The user says "exit" or "quit". 2) The user does not
* respond or says something that does not match an intent defined in your voice model. 3) An error occurs
* */
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return (Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest');
},
handle(handlerInput) {
console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
// Any cleanup logic goes here.
return handlerInput.responseBuilder.getResponse(); // notice we send an empty response
}
};
/**
* Generic error handling to capture any syntax or routing errors. If you receive an error
* stating the request handler chain is not found, you have not implemented a handler for
* the intent being invoked or included it in the skill builder below
* */
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
const speakOutput = 'Sorry, I had trouble doing what you asked. Please try again.';
console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};
/**
* This handler acts as the entry point for your skill, routing all request and response
* payloads to the handlers above. Make sure any new handlers or interceptors you've
* defined are included below. The order matters - they're processed top to bottom
* */
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
PlayAudioIntentHandler,
PauseAudioIntentHandler,
UnsupportedAudioIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SystemExceptionHandler,
FallbackIntentHandler,
SessionEndedRequestHandler
)
.addRequestInterceptors(
AddAudioPlayerHttpHeadersRequestInterceptor,
SetDefaultAudioPlayerHttpHeadersRequestInterceptor([{
"name": "X-Alexa-Skill-Demo",
"value": "https://github.com/warren-bank/Alexa-skill-demos/tree/alexa-samples/skill-sample-nodejs-audio-player/e93ec39"
},{
"name": "X-Alexa-Skill-Library",
"value": "https://github.com/warren-bank/Alexa-skill-libraries/tree/master/audio-player-http-request-headers"
}])
)
.addErrorHandlers(
ErrorHandler
)
.lambda();