Skip to content

Instantly share code, notes, and snippets.

@sebsto
Created January 20, 2019 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebsto/0bde5eb2d79f5f880e802e03172e9b95 to your computer and use it in GitHub Desktop.
Save sebsto/0bde5eb2d79f5f880e802e03172e9b95 to your computer and use it in GitHub Desktop.
call LWA profile from alexa skill
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