Last active
April 17, 2018 21:18
-
-
Save jawa-the-hutt/6dfcb872aee0a9fa41632cd9ca3e9c42 to your computer and use it in GitHub Desktop.
handler-sample1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let deviceId = this.event.context.System.device.deviceId; | |
this.context.log('deviceId = ', deviceId); // will help you find out what the device ID is so you can add it into the global devices object | |
// get the mapping to the intent object | |
let intent = this.event.request.intent; | |
this.context.log('intent - ', intent); | |
// get the authority that is returning from alexa service. filter it based on your alexa app ID and the Control_List name you gave it | |
let authority = intent.slots.Control.resolutions.resolutionsPerAuthority.filter(auth => auth.authority.includes(GetEnvironmentVariable('ALEXA_APP_ID') + ".Control_List")); | |
// get the actual word being sent back from alexa service so that we can match it later | |
let control = authority[0].values[0].value.id; | |
this.context.log('control - ', control); | |
// Setup default Alexa response. This response will change based on the match made in the switch statement that determines | |
// which Control we are trying to use. | |
let alexaResponse = 'ok'; | |
// did we detect a Control value? If not, then Alexa will respond with a message that she doesn't understand what you're trying to do. | |
// If there is a value, then it will attempt to map it into an action | |
if (control) { | |
// Obtain User Intent | |
switch(control) { | |
// based on the intent, this will map the action that simulates a button push on a remote control | |
case "PLAY": | |
path = '/remote/processKey?key=play&hold=keyPress'; | |
alexaResponse = "sure...playing now"; | |
break; | |
case "PAUSE": | |
path = '/remote/processKey?key=pause&hold=keyPress'; | |
alexaResponse = "sure...pausing now"; | |
break; | |
case "ESPN": | |
path = '/tv/tune?major=206'; | |
alexaResponse = "sure...changing to ESPN"; | |
break; | |
case "ESPN2": | |
path = '/tv/tune?major=209'; | |
alexaResponse = "sure...changing to ESPN2"; | |
break; | |
// default check to see if a number was passed in and if so, it tries to change the STB to that channel. otherwise it gives up | |
default: | |
if (! isNaN(intent.slots.Channel.value) ) { | |
path = '/tv/tune?major=' + intent.slots.Channel.value; | |
} else { | |
response.tell("I don't understand what you need. Please try again"); | |
} | |
break; | |
}; | |
} else { | |
this.emit(':tell', "hmm... I don't know how to do that with Direct TV"); | |
} | |
// get the room the directv unit is in. If the user says the room at the end of their statement, then we will specifically take action on the STB mapped | |
// to that room in the global rooms object. If the user does not add a room at the end of their statement, then we fall back to the trying to take action | |
// on the STB that is in the same room as the Alexa device the user is speaking to. If all else fails, we set the clientAddress to null and | |
// this will effectively cause Alexa to not be able to do anything. | |
if (rooms[intent.slots.Room.value]) { | |
clientAddress = '&clientAddr=' + rooms[intent.slots.Room.value]; | |
} else if (deviceRoom) { | |
clientAddress = '&clientAddr=' + rooms[deviceRoom]; | |
} else { | |
clientAddress = null; | |
} | |
this.context.log('clientAddress = ', clientAddress); | |
if(clientAddress) { | |
let url = baseUrl + path + clientAddress; | |
this.context.log('url - ', url); | |
return fetch(url).then((res) => { | |
if(res.ok) { | |
this.emit(':tell', alexaResponse); | |
return; | |
} else { | |
this.context.log('error response - ', res.status); | |
switch(res.status){ | |
case 403: | |
this.emit(':tell', "that channel is currently unavailable"); | |
break; | |
default: | |
this.emit(':tell', "hmm... that didn't work"); | |
break; | |
}; | |
return; | |
} | |
}).catch(error => { | |
throw error; | |
}); | |
} else { | |
this.emit(':tell', "hmm...I don't recognize that room"); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment