@@ -17,25 +17,26 @@ var app = new alexa.app('connect');
17
17
app . express ( { expressApp : express_app } ) ;
18
18
19
19
const successSound = "<audio src='soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_neutral_response_02'/>" ;
20
- const connectDeviceCard = ( req ) => ( {
20
+ const connectDeviceCard = ( res ) => ( {
21
21
type : "Simple" ,
22
- title : req . __ ( "Connecting to a device using Spotify Connect" ) ,
23
- content : req . __ ( "To add a device to Spotify Connect,"
22
+ title : res . __ ( "Connecting to a device using Spotify Connect" ) ,
23
+ content : res . __ ( "To add a device to Spotify Connect,"
24
24
+ " log in to your Spotify account on a supported device"
25
25
+ " such as an Echo, phone, or computer"
26
26
+ "\nhttps://support.spotify.com/uk/article/spotify-connect/" )
27
27
} ) ;
28
28
const applicationId = require ( './package.json' ) . alexa . applicationId ;
29
29
30
+ i18n . configure ( {
31
+ directory : __dirname + '/locales' ,
32
+ defaultLocale : 'en-GB' ,
33
+ register : req
34
+ } ) ;
35
+
30
36
// Run every time the skill is accessed
31
37
app . pre = function ( req , res , _type ) {
32
- i18n . configure ( {
33
- directory : __dirname + '/locales' ,
34
- defaultLocale : 'en-GB' ,
35
- register : req
36
- } ) ;
37
38
if ( req . data . request . locale ) {
38
- i18n . setLocale ( req , req . data . request . locale ) ;
39
+ i18n . setLocale ( res , req . data . request . locale ) ;
39
40
}
40
41
// Error if the application ID of the request is not for this skill
41
42
if ( req . applicationId != applicationId &&
@@ -44,22 +45,22 @@ app.pre = function (req, res, _type) {
44
45
}
45
46
// Check that the user has an access token, if they have linked their account
46
47
if ( ! ( req . context . System . user . accessToken || req . getSession ( ) . details . user . accessToken ) ) {
47
- res . say ( req . __ ( "You have not linked your Spotify account, check your Alexa app to link the account" ) ) ;
48
+ res . say ( res . __ ( "You have not linked your Spotify account, check your Alexa app to link the account" ) ) ;
48
49
res . linkAccount ( ) ;
49
50
}
50
51
} ;
51
52
52
53
// Run after every request
53
54
app . post = function ( req , res , _type , exception ) {
54
55
if ( exception ) {
55
- return res . clear ( ) . say ( req . __ ( "An error occured: " ) + exception ) . send ( ) ;
56
+ return res . clear ( ) . say ( res . __ ( "An error occured: " ) + exception ) . send ( ) ;
56
57
}
57
58
} ;
58
59
59
60
// Function for when skill is invoked without intent
60
61
app . launch ( function ( req , res ) {
61
- res . say ( req . __ ( "I can control your Spotify Connect devices, to start, ask me to list your devices" ) )
62
- . reprompt ( req . __ ( "To start, ask me to list your devices" ) ) ;
62
+ res . say ( res . __ ( "I can control your Spotify Connect devices, to start, ask me to list your devices" ) )
63
+ . reprompt ( res . __ ( "To start, ask me to list your devices" ) ) ;
63
64
// Keep session open
64
65
res . shouldEndSession ( false ) ;
65
66
} ) ;
@@ -70,9 +71,9 @@ app.intent("AMAZON.HelpIntent", {
70
71
"slots" : { } ,
71
72
"utterances" : [ ]
72
73
} , function ( req , res ) {
73
- res . say ( req . __ ( "You can ask me to list your connect devices and then control them. " ) )
74
- . say ( req . __ ( "For example, tell me to play on a device after listing devices" ) )
75
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
74
+ res . say ( res . __ ( "You can ask me to list your connect devices and then control them. " ) )
75
+ . say ( res . __ ( "For example, tell me to play on a device after listing devices" ) )
76
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
76
77
// Keep session open
77
78
res . shouldEndSession ( false ) ;
78
79
} ) ;
@@ -111,10 +112,10 @@ app.intent('PlayIntent', {
111
112
req . getSession ( ) . set ( "statusCode" , r . statusCode ) ;
112
113
res . say ( successSound ) ;
113
114
} ) . catch ( ( err ) => {
114
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
115
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
115
116
if ( err . statusCode === 404 ) {
116
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
117
- res . card ( connectDeviceCard ( req ) ) ;
117
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
118
+ res . card ( connectDeviceCard ( res ) ) ;
118
119
}
119
120
} ) ;
120
121
}
@@ -134,10 +135,10 @@ app.intent('PauseIntent', {
134
135
req . getSession ( ) . set ( "statusCode" , r . statusCode ) ;
135
136
res . say ( successSound ) ;
136
137
} ) . catch ( ( err ) => {
137
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
138
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
138
139
if ( err . statusCode === 404 ) {
139
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
140
- res . card ( connectDeviceCard ( req ) ) ;
140
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
141
+ res . card ( connectDeviceCard ( res ) ) ;
141
142
}
142
143
} ) ;
143
144
}
@@ -159,10 +160,10 @@ app.intent('SkipNextIntent', {
159
160
req . getSession ( ) . set ( "statusCode" , r . statusCode ) ;
160
161
res . say ( successSound ) ;
161
162
} ) . catch ( ( err ) => {
162
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
163
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
163
164
if ( err . statusCode === 404 ) {
164
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
165
- res . card ( connectDeviceCard ( req ) ) ;
165
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
166
+ res . card ( connectDeviceCard ( res ) ) ;
166
167
}
167
168
} ) ;
168
169
}
@@ -185,10 +186,10 @@ app.intent('SkipPreviousIntent', {
185
186
req . getSession ( ) . set ( "statusCode" , r . statusCode ) ;
186
187
res . say ( successSound ) ;
187
188
} ) . catch ( ( err ) => {
188
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
189
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
189
190
if ( err . statusCode === 404 ) {
190
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
191
- res . card ( connectDeviceCard ( req ) ) ;
191
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
192
+ res . card ( connectDeviceCard ( res ) ) ;
192
193
}
193
194
} ) ;
194
195
}
@@ -206,10 +207,10 @@ const setVolume = (volumePercent, req, res) => {
206
207
// Handle sending as JSON
207
208
json : true
208
209
} ) . catch ( ( err ) => {
209
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
210
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
210
211
if ( err . statusCode === 404 ) {
211
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
212
- res . card ( connectDeviceCard ( req ) ) ;
212
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
213
+ res . card ( connectDeviceCard ( res ) ) ;
213
214
}
214
215
} ) ;
215
216
} ;
@@ -227,7 +228,7 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
227
228
}
228
229
else {
229
230
// If not valid volume
230
- res . say ( req . __ ( isPercentIntent
231
+ res . say ( res . __ ( isPercentIntent
231
232
? "You can only set the volume percent between 0 and 100"
232
233
: "You can only set the volume between 0 and 10" ) ) ;
233
234
// Keep session open
@@ -237,22 +238,22 @@ const getAndValidateVolumePercentFromSlot = (req, res, isPercentIntent) => {
237
238
}
238
239
else {
239
240
// Not a number
240
- res . say ( req . __ ( isPercentIntent
241
+ res . say ( res . __ ( isPercentIntent
241
242
? "Try setting a volume percent between 0 and 100"
242
243
: "Try setting a volume between 0 and 10" ) )
243
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
244
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
244
245
// Keep session open
245
246
res . shouldEndSession ( false ) ;
246
247
return null ;
247
248
}
248
249
}
249
250
else {
250
251
// No slot value
251
- res . say ( req . __ ( "I couldn't work out the volume to use." ) )
252
- . say ( req . __ ( isPercentIntent
252
+ res . say ( res . __ ( "I couldn't work out the volume to use." ) )
253
+ . say ( res . __ ( isPercentIntent
253
254
? "Try setting a volume percent between 0 and 100"
254
255
: "Try setting a volume between 0 and 10" ) )
255
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
256
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
256
257
// Keep session open
257
258
res . shouldEndSession ( false ) ;
258
259
return null ;
@@ -319,16 +320,16 @@ app.intent('GetDevicesIntent', {
319
320
// Check if user has devices
320
321
if ( devices . length > 0 ) {
321
322
// Comma separated list of device names
322
- res . say ( req . __ ( "I found these connect devices: " ) )
323
- . say ( [ deviceNames . slice ( 0 , - 1 ) . join ( ', ' ) , deviceNames . slice ( - 1 ) [ 0 ] ] . join ( deviceNames . length < 2 ? '' : ',' + req . __ ( ' and ' ) ) + ". " )
324
- . say ( req . __ ( "What would you like to do with these devices?" ) ) . reprompt ( req . __ ( "What would you like to do?" ) ) ;
323
+ res . say ( res . __ ( "I found these connect devices: " ) )
324
+ . say ( [ deviceNames . slice ( 0 , - 1 ) . join ( ', ' ) , deviceNames . slice ( - 1 ) [ 0 ] ] . join ( deviceNames . length < 2 ? '' : ',' + res . __ ( ' and ' ) ) + ". " )
325
+ . say ( res . __ ( "What would you like to do with these devices?" ) ) . reprompt ( res . __ ( "What would you like to do?" ) ) ;
325
326
// Keep session open
326
327
res . shouldEndSession ( false ) ;
327
328
}
328
329
else {
329
330
// No devices found
330
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
331
- res . card ( connectDeviceCard ( req ) ) ;
331
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
332
+ res . card ( connectDeviceCard ( res ) ) ;
332
333
}
333
334
} )
334
335
// Handle errors
@@ -375,29 +376,29 @@ app.intent('DevicePlayIntent', {
375
376
// Handle sending as JSON
376
377
json : true
377
378
} ) . then ( ( _r ) => {
378
- res . say ( req . __ ( "Playing on device {{deviceName}}" , { deviceName : device . name } ) ) ;
379
+ res . say ( res . __ ( "Playing on device {{deviceName}}" , { deviceName : device . name } ) ) ;
379
380
} ) . catch ( ( err ) => {
380
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
381
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
381
382
if ( err . statusCode === 404 ) {
382
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
383
- res . card ( connectDeviceCard ( req ) ) ;
383
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
384
+ res . card ( connectDeviceCard ( res ) ) ;
384
385
}
385
386
} ) ;
386
387
}
387
388
else {
388
389
// If device not found
389
- res . say ( req . __ ( "I couldn't find a device named {{DEVICE}}." , { DEVICE } ) )
390
- . say ( req . __ ( "Try asking me to list devices first" ) ) ;
390
+ res . say ( res . __ ( "I couldn't find a device named {{DEVICE}}." , { DEVICE } ) )
391
+ . say ( res . __ ( "Try asking me to list devices first" ) ) ;
391
392
// Keep session open
392
393
res . shouldEndSession ( false ) ;
393
394
}
394
395
} ) ;
395
396
}
396
397
else {
397
398
// No slot value
398
- res . say ( req . __ ( "I couldn't work out which device to play on." ) )
399
- . say ( req . __ ( "Try asking me to list devices first" ) )
400
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
399
+ res . say ( res . __ ( "I couldn't work out which device to play on." ) )
400
+ . say ( res . __ ( "Try asking me to list devices first" ) )
401
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
401
402
// Keep session open
402
403
res . shouldEndSession ( false ) ;
403
404
}
@@ -428,8 +429,8 @@ app.intent(
428
429
if ( ! req . slot ( "TRACKNAME" ) ) {
429
430
// No slot value
430
431
res
431
- . say ( req . __ ( "I couldn't work out which song you want to queue." ) )
432
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
432
+ . say ( res . __ ( "I couldn't work out which song you want to queue." ) )
433
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
433
434
// Keep session open
434
435
res . shouldEndSession ( false ) ;
435
436
}
@@ -472,21 +473,21 @@ app.intent(
472
473
} )
473
474
. then ( ( response ) => {
474
475
res . say (
475
- req . __ ( "Queued track {{trackName}}" , {
476
+ res . __ ( "Queued track {{trackName}}" , {
476
477
trackName,
477
478
} )
478
479
) ;
479
480
} )
480
481
. catch ( ( err ) => {
481
482
res
482
- . say ( req . __ ( "Sorry, I couldn't queue that song." ) )
483
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
483
+ . say ( res . __ ( "Sorry, I couldn't queue that song." ) )
484
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
484
485
} ) ;
485
486
} )
486
487
. catch ( ( err ) => {
487
488
res
488
- . say ( req . __ ( "Sorry, I couldn't queue that song." ) )
489
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
489
+ . say ( res . __ ( "Sorry, I couldn't queue that song." ) )
490
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
490
491
} ) ;
491
492
}
492
493
) ;
@@ -526,29 +527,29 @@ app.intent('DeviceTransferIntent', {
526
527
// Handle sending as JSON
527
528
json : true
528
529
} ) . then ( ( _r ) => {
529
- res . say ( req . __ ( "Transferring to {{deviceName}}" , { deviceName : device . name } ) ) ;
530
+ res . say ( res . __ ( "Transferring to {{deviceName}}" , { deviceName : device . name } ) ) ;
530
531
} ) . catch ( ( err ) => {
531
- if ( err . statusCode === 403 ) res . say ( req . __ ( "Make sure your Spotify account is premium" ) ) ;
532
+ if ( err . statusCode === 403 ) res . say ( res . __ ( "Make sure your Spotify account is premium" ) ) ;
532
533
if ( err . statusCode === 404 ) {
533
- res . say ( req . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
534
- res . card ( connectDeviceCard ( req ) ) ;
534
+ res . say ( res . __ ( "I couldn't find any connect devices, check your Alexa app for information on connecting a device" ) ) ;
535
+ res . card ( connectDeviceCard ( res ) ) ;
535
536
}
536
537
} ) ;
537
538
}
538
539
else {
539
540
// If device not found
540
- res . say ( req . __ ( "I couldn't find a device named {{DEVICE}}." , { DEVICE } ) )
541
- . say ( req . __ ( "Try asking me to list devices first" ) ) ;
541
+ res . say ( res . __ ( "I couldn't find a device named {{DEVICE}}." , { DEVICE } ) )
542
+ . say ( res . __ ( "Try asking me to list devices first" ) ) ;
542
543
// Keep session open
543
544
res . shouldEndSession ( false ) ;
544
545
}
545
546
} ) ;
546
547
}
547
548
else {
548
549
// No slot value
549
- res . say ( req . __ ( "I couldn't work out which device to transfer to." ) )
550
- . say ( req . __ ( "Try asking me to list devices first" ) )
551
- . reprompt ( req . __ ( "What would you like to do?" ) ) ;
550
+ res . say ( res . __ ( "I couldn't work out which device to transfer to." ) )
551
+ . say ( res . __ ( "Try asking me to list devices first" ) )
552
+ . reprompt ( res . __ ( "What would you like to do?" ) ) ;
552
553
// Keep session open
553
554
res . shouldEndSession ( false ) ;
554
555
}
@@ -577,16 +578,16 @@ app.intent('GetTrackIntent', {
577
578
} )
578
579
. then ( function ( body ) {
579
580
if ( body . is_playing ) {
580
- res . say ( req . __ ( "This is {{name}} by {{artist}}" , { name : body . item . name , artist : body . item . artists [ 0 ] . name } ) ) ;
581
+ res . say ( res . __ ( "This is {{name}} by {{artist}}" , { name : body . item . name , artist : body . item . artists [ 0 ] . name } ) ) ;
581
582
}
582
583
else {
583
584
if ( body . item . name ) {
584
585
// If not playing but last track known
585
- res . say ( req . __ ( "That was {{name}} by {{artist}}" , { name : body . item . name , artist : body . item . artists [ 0 ] . name } ) ) ;
586
+ res . say ( res . __ ( "That was {{name}} by {{artist}}" , { name : body . item . name , artist : body . item . artists [ 0 ] . name } ) ) ;
586
587
}
587
588
else {
588
589
// If unknown
589
- res . say ( req . __ ( "Nothing is playing" ) ) ;
590
+ res . say ( res . __ ( "Nothing is playing" ) ) ;
590
591
}
591
592
}
592
593
} )
0 commit comments