如何在Alexa Skill中获取用户设置里的“工作”地址?
Hey there! I’ve tackled this exact issue before, so let me break down how to pull that work address from Alexa’s "My Location" settings into your custom skill. Unlike device addresses, this is part of the user’s profile address data, so we’ll need to handle permissions and use Alexa’s User Profile API to fetch it.
Step 1: Enable Required Permissions in Your Skill
First, you need to tell Alexa your skill needs access to the user’s work address:
- Head to the Alexa Developer Console, open your skill, and navigate to the Permissions tab.
- Under the Profile section, find and check the box for Read User's Work Address (this is the official name, even if your translation of the setting was slightly different).
- Save your changes—this lets Alexa know your skill requests this access.
Step 2: Request User Consent (If Not Already Granted)
Before you can fetch the address, the user needs to grant permission. Add a handler to check for consent and prompt the user if it’s missing:
// Example for Node.js using ask-sdk-core const AskForWorkAddressPermissionHandler = { canHandle(handlerInput) { const { request, context } = handlerInput.requestEnvelope; const hasPermission = context.System.user.permissions?.consentStatus.includes('READ::ALEXA:PROFILE:WORK_ADDRESS'); return request.type === 'IntentRequest' && request.intent.name === 'GetWorkAddressIntent' && !hasPermission; }, handle(handlerInput) { const requiredPermissions = ['read::alexa:profile:work_address']; return handlerInput.responseBuilder .addAskForPermissionsConsentCard(requiredPermissions) .speak("I need permission to access your work address. You can grant this by opening the Alexa app and going to your skill's settings.") .reprompt("Could you please grant permission in the Alexa app so I can get your work address?") .getResponse(); } };
Step 3: Fetch the Work Address Once Consent is Granted
Once the user has given permission, you can call Alexa’s User Profile Service (UPS) to retrieve the work address. Here’s how to do it in code:
const GetWorkAddressHandler = { canHandle(handlerInput) { const { request } = handlerInput.requestEnvelope; return request.type === 'IntentRequest' && request.intent.name === 'GetWorkAddressIntent'; }, async handle(handlerInput) { const { serviceClientFactory, requestEnvelope } = handlerInput; const { userId, apiAccessToken } = requestEnvelope.context.System.user; try { // Initialize the UPS client const upsClient = serviceClientFactory.getUpsServiceClient(); // Fetch the work address const workAddress = await upsClient.getWorkAddress(userId, apiAccessToken); // Format the address for response (adjust based on your needs) const formattedAddress = [ workAddress.addressLine1, workAddress.addressLine2, `${workAddress.city}, ${workAddress.stateOrRegion} ${workAddress.postalCode}`, workAddress.countryCode ].filter(Boolean).join(', '); return handlerInput.responseBuilder .speak(`Got it! Your work address is ${formattedAddress}`) .getResponse(); } catch (error) { // Handle permission errors or API issues if (error.name === 'ServiceError' && error.statusCode === 403) { return handlerInput.responseBuilder .speak("I still don't have permission to access your work address. Please check your Alexa app settings for this skill.") .getResponse(); } return handlerInput.responseBuilder .speak("Sorry, I ran into an issue getting your work address. Could you try again later?") .getResponse(); } } };
Key Notes to Keep in Mind
- Field Variability: Address fields might differ by region (e.g., some locations won’t have
addressLine2). Always filter out null/empty values to avoid awkward responses. - Testing: When testing, make sure you’ve granted permission to your skill in the Alexa app (go to Skills & Games > Your Skills > [Your Skill] > Settings > Permissions).
- Permission Scopes: The exact scope string is
read::alexa:profile:work_address—don’t misspell this, or the permission request will fail.
内容的提问来源于stack exchange,提问作者chocolate cake

