Get location from Alexa Skills Kit (ASK)
Asked Answered
P

5

21

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?

Partner answered 2/5, 2016 at 15:30 Comment(0)
F
6

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.

Flashover answered 2/5, 2016 at 23:46 Comment(1)
This answer is now obsolete, watch out! See other answers belowCorniculate
R
15

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.

Reisfield answered 5/4, 2017 at 14:22 Comment(2)
The Google Maps API is not really an option due to their very restrictive usage policy: "The Geocoding API may only be used in conjunction with displaying results on a Google map. It is prohibited to use Geocoding API data without displaying a Google map."Trackless
@Trackless I believe having the option to display the map on a device (e.g. an Echo Show) could/would satisfy this requirement.Palsy
F
6

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.

Flashover answered 2/5, 2016 at 23:46 Comment(1)
This answer is now obsolete, watch out! See other answers belowCorniculate
G
2

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.

Goldston answered 15/4, 2017 at 21:39 Comment(3)
how do you add the flask_ask library though? I've tried the above code but it now makes my lambda function fail when it attempts to import the flask_ask modules at the beginningPeregrine
pip install flask-ask should be enough. You should check the documentation: flask-ask.readthedocs.ioGoldston
Does anyone know a way to do this without using flask ask.Brumbaugh
T
2

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
    }       
}
Trackless answered 10/1, 2019 at 18:54 Comment(0)
S
0

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

Stiffler answered 23/8, 2018 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.