Get location from Alexa Skills Kit (ASK)

末鹿安然 提交于 2019-12-17 18:37:03

问题


I'm looking for a way to get the user's location, ideally longitude/latitude but address would work too, from the Alexa Skill Kit request to my custom skill. Also, I don't want to have to have the user link to an account on my app's side.

Is this possible? If so, how?


回答1:


As per this thread on the Amazon Developer forums, there is not currently (as of May 2016) a way to get user location via the publicly available APIs. The only skills able to do so, such as Uber or Domino's, are utilizing APIs that are not available through the Alexa Skills Kit. However, there's hope that it may be added, as "Jamie@Amazon" left this reply in that discussion:

Hey there,

Thanks for posting.

This has now been added to the roadmap. Thanks for the feedback.

Jamie

However, at the time of writing, no further update has been provided regarding the implementation of such a feature.




回答2:


Amazon has now (2017-04-05) added this capability. See their blog post about their new Device Address API.

Using this new API you can get the address (either postal code or full address) of the device, as specified in the customer’s device settings.

From that you could use a geocoding API (such as is part of the Google Maps API) to translate the address into location coordinates.




回答3:


As @Tom has pointed out, it is now possible to get the device address in your custom skill. If you are using Python to create your skill, it's pretty easy to implement the new API. I've written a detailed blog post about it here. It also describes how to get the corresponding coordinates for the retrieved address, so it might be useful for you. In short, here is a Python function to get you started. I use the very handy Flask-Ask library for my skill. Thus, it is easy to get the value of the deviceId and consentToken objects. These are included by Alexa in the JSON request sent to your skill. They are needed for constructing the request to the Amazon address API endpoint:

import requests
from flask_ask import Ask, context

def get_alexa_location():
    URL =  "https://api.amazonalexa.com/v1/devices/{}/settings" \
           "/address".format(context.System.device.deviceId)
    TOKEN =  context.System.user.permissions.consentToken
    HEADER = {'Accept': 'application/json',
             'Authorization': 'Bearer {}'.format(TOKEN)}
    r = requests.get(URL, headers=HEADER)
    if r.status_code == 200:
        return(r.json())

The function will return something like this on a successful call:

{u'city': u'Berlin', u'countryCode': u'DE',
u'addressLine1': u'42 Unter den Linden',
u'addressLine2': None, u'stateOrRegion': u'Berlin',
u'districtOrCounty': u'Berlin', u'postalCode': u'10117',
u'addressLine3': None}

You can use geopy to convert this address to coordinates.




回答4:


It is now possible to get the user's real-time geolocation (longitude/latitude) using the Alexa Location Services, which avoids having to integrate with a separate geocoding API as suggested by other answers. See the related blogpost for official information about this feature's release.

Provided that the device is compatible (context.System.device.supportedInterfaces.Geolocation exists), the location services are running and the alexa::devices:all:geolocation:read permission has been granted to your skill , you can retrieve a Geolocation object through the request's context, which will be equivalent to the following JSON payload:

"Geolocation":{ 
    "locationServices": { 
        "access": "ENABLED",
        "status": "RUNNING",   
    },
    "timestamp": "2018-03-25T00:00:00Z+00:00",
    "coordinate": {
        "latitudeInDegrees": 38.2,
        "longitudeInDegrees": 28.3,
        "accuracyInMeters": 12.1 
    },
    "altitude": {
        "altitudeInMeters": 120.1,
        "accuracyInMeters": 30.1
    },
    "heading": { 
        "directionInDegrees": 180.0,
        "accuracyInDegrees": 5.0  
    },
    "speed": { 
        "speedInMetersPerSecond": 10.0,
        "accuracyInMetresPerSecond": 1.1
    }       
}



回答5:


Please follow the below URL to Get location from Alexa Skills Kit (ASK)

URL: https://api.eu.amazonalexa.com/v1/devices/{devicesId}/settings/address/countryAndPostalCode

Your Header would be something like below :

Host:api.eu.amazonalexa.com[keep your host you can get is from developer account during testing from json]
Accept:application/json
Authorization:Bearer [KEEP YOUR apiAccessToken here]

if request went success your will get response as below:

{
    "countryCode": "IN",
    "postalCode": "560102"
}

Make sure you have enabled permission in the Alexa app, and grants the permission of the respective skill please refer the bellow URL for more details of permission configuration

https://developer.amazon.com/blogs/alexa/post/0c975fc7-17dd-4f5c-8343-a37024b66c99/alexa-skill-recipe-using-the-device-address-api-to-request-information



来源:https://stackoverflow.com/questions/36986139/get-location-from-alexa-skills-kit-ask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!