Reputation: 994
I am creating an alexa app and for that i have permission for the user's location and customer's firstname. My first question is if customer's first name is what is user's first name or it is something different. But if it is asking for the user's first name then to get that. For location info, we use ConsentToken
, so is there any way to get the user name out of it?
I can ask for the user name and store it and then can greet the user. But i have to ask everytime user launches the app. I am using php.
Upvotes: 0
Views: 2353
Reputation: 412
In addition to Neryuk's answer pointing correctly the documentation, I give some code sample in NodeJS.
Assuming you already gave all the permissions needed from Alexa Skill Developer Portal, you can ask for user information within your intent handler in this way:
const PERMISSIONS = ['alexa::profile:name:read', 'alexa::profile:email:read', 'alexa::profile:mobile_number:read'];
const MyIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'MyIntent');
},
async handle(handlerInput) {
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput;
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const consentToken = handlerInput.requestEnvelope.context.System.apiAccessToken;
if (!consentToken) {
return responseBuilder
.speak('Missing permissions')
.withAskForPermissionsConsentCard(PERMISSIONS)
.getResponse();
}
const client = serviceClientFactory.getUpsServiceClient();
const userName = await client.getProfileName();
const userEmail = await client.getProfileEmail();
const userMobileNumber = await client.getProfileMobileNumber();
console.log('Name successfully retrieved '+ userName);
console.log('Email successfully retrieved '+ userEmail);
console.log('PhoneNumber successfully retrieved '+ JSON.stringify(userMobileNumber));
const speakOutput = 'The username is '+userName;
return handlerInput.responseBuilder
.speak(speakOutput)
.withSimpleCard('MyFavSkill')
.getResponse();
},
};
Note that you must declare your handlerInput code as async because you are invoking an async request you must wait within your method. Code is missing the "try-catch" block to simplify its reading, but you should manage unexpected errors. Hope this may help.
Upvotes: 0
Reputation: 178
First, the user has to link his account with your skill and accept the permission (you need to set it in your skill configuration)
once the user is loged in, You will just be able to use a access/refresh token to get the user name from Alexa output
Check this, could be clearest: https://developer.amazon.com/fr/docs/custom-skills/request-customer-contact-information-for-use-in-your-skill.html
Upvotes: 1