从 Alexa Skills Kit (ASK) 获取位置

Get location from Alexa Skills Kit (ASK)

我正在寻找一种获取用户位置的方法,理想情况下 longitude/latitude 但地址也可以,从 Alexa Skill Kit 请求到我的自定义技能。另外,我不想让用户 link 到我的应用端的帐户。

这可能吗?如果是,怎么做?

根据 this thread on the Amazon Developer forums,目前(截至 2016 年 5 月)无法通过公开可用的 API 获取用户位置。唯一能够做到这一点的技能,例如 Uber 或 Domino,正在利用无法通过 Alexa Skills Kit 获得的 API。但是,希望可以添加它,因为 "Jamie@Amazon" 在该讨论中留下了这个回复:

Hey there,

Thanks for posting.

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

Jamie

但是,在撰写本文时,尚未提供有关此类功能实施的进一步更新。

亚马逊现在 (2017-04-05) 添加了此功能。参见 their blog post about their new Device Address API

使用这个新的 API,您可以获得设备的地址(邮政编码或完整地址),如客户设备设置中指定的那样。

由此您可以使用地理编码 API(例如 Google 地图 API 的一部分)将地址转换为位置坐标。

正如@Tom 所指出的,现在可以在您的自定义技能中获取设备地址。如果您使用 Python 来创建您的技能,那么实施新的 API 非常容易。我已经写了一篇关于它的详细博客 post here。它还描述了如何为检索到的地址获取相应的坐标,因此它可能对您有用。简而言之,这是一个让您入门的 Python 函数。我使用非常方便的 Flask-Ask 库来提高我的技能。因此,很容易获得 deviceId 和 consentToken 对象的值。这些由 Alexa 包含在发送给您的技能的 JSON 请求中。构建对亚马逊地址 API 端点的请求需要它们:

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())

该函数将 return 在成功调用时像这样:

{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}

您可以使用geopy将此地址转换为坐标。

请按照以下 URL 从 Alexa Skills Kit (ASK) 获取位置

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

您的页眉如下所示:

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]

如果请求成功,您将得到如下响应:

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

确保您已在Alexa应用中启用权限,并授予相应技能的权限请参阅以下内容URL以了解更多权限配置

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

现在可以使用 Alexa Location Services, which avoids having to integrate with a separate geocoding API as suggested by other answers. See the related blogpost 获取用户的实时地理位置信息 (longitude/latitude),以获取有关此功能发布的官方信息。

只要设备兼容(context.System.device.supportedInterfaces.Geolocation存在),定位服务是运行并且你的技能已经获得alexa::devices:all:geolocation:read权限,你可以检索一个Geolocation 对象通过请求的上下文,这将等效于以下 JSON 有效负载:

"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
    }       
}