Skip to content

Instantly share code, notes, and snippets.

@sebsto sebsto/lwa_blog_2.js
Created Jan 20, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.