Created
January 20, 2019 13:41
-
-
Save sebsto/0bde5eb2d79f5f880e802e03172e9b95 to your computer and use it in GitHub Desktop.
call LWA profile from alexa skill
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
const HelloWorldIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent'; | |
}, | |
async handle(handlerInput) { | |
const { | |
accessToken | |
} = handlerInput.requestEnvelope.context.System.user; | |
let speechText = ''; | |
if (!accessToken) { | |
speechText = 'You must authenticate with your Amazon Account to use this skill. I sent instructions in how to do this in your Alexa App'; | |
return handlerInput.responseBuilder.speak(speechText).withLinkAccountCard().getResponse(); | |
} else { | |
const amznProfileUrl = `https://api.amazon.com/user/profile?access_token=${accessToken}`; | |
try { | |
const response = await axios.get(amznProfileUrl); | |
speechText = `Hello, ${response.data.name.split(" ")[0]}!`; | |
} catch (error) { | |
console.error(error); | |
speechText = 'Hello!'; | |
} | |
return handlerInput.responseBuilder.speak(speechText).getResponse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment