问题
So, I'm working a project with the Amazon Echo. My goal is to record when I did a specific action and to record it into a DB. My issue is timezones, and I avoid this by using epoch time. However, from what I can tell of custom slots for an intent, my choices are formatted date strings with no time, or formatted time strings with no dates, and on top of all of that, I have no way of grabbing the client's timezone without specifically asking for it based on some forum posts I found with my google fu.
Does the echo just hate dates? This seems like something that should be really easy, but I'm struggling to figure out how to go about it without being really awkward and asking where they live so I can do a lookup of their timezone. I already had to make a pivot from telling them the specific time they did something to how long ago they did something because I can't pass dates back to the Echo and expect it to translate it. Is this another silly pivot I have to make?
回答1:
Correct, you cannot get the client's TZ or location from Alexa. It's a privacy issue. The only way is to ask for it yourself.
There is a custom slot for "five digit number" which is perfect for ZIP code. That is the simplest way to get a user's approximate location.
If you want to get more inventive, since the Echo is (pretty much) only used in America, you only have to narrow it down to one of four time zones. (At the expense of Alaska, Hawaii and Arizona for half the year.) You could just ask which timezone they are in, which is less intrusive.
Or, if you want to get creative, make a joke out of it.
- A: "Pop quiz: Are you in Eastern Standard time?"
- U: "No"
- A: "Drat. Let me guess again. You seem like a Mountain Time person."
...
Yes. It is an irritating limitation. But if you can find a novel way to work around it your skill will be more endearing.
回答2:
It looks like you cannot (yet) detect the user's timezone, but you should still be able to achieve your goals. A few thoughts:
- You could just make the system ignorant of timezones altogether. Each user may have a different timezone, but all times for a particular user will be in the same timezone so any duration calculations will work fine.
- There is no DATETIME slot type, so you need to handle each slot separately as AMAZON.DATE and AMAZON.TIME. e.g. "Mark completion on {DATE} at {TIME}"
- Also, if you are real-world marking completion datetime, can you just generate the timestamp inside your code, rather than making them say it?
- Requesting Zip could be a quick way to set timezone, if necessary.
- Hacky: If you use Account Linking you may be able to detect their timezone from the client-side on your login page.
Converting between server timetamp and spoken times in the user's timezone will be problematic, but perhaps you can work around it until amazon includes timezone data.
回答3:
There is a way to find the user's timezone from country code and postal code which Amazon provides. You just need to enable the permission in the developer's console.
After that, whenever you receive a request from Alexa voice service, you will get a consent token
. Which indicates that the user has given the permission.
You can then get the timezone of the user by querying Google API. The python code for the same is mentioned below.
# to get the country code and postal code of user
res = requests.get('{0}/v1/devices/{1}/settings/address/countryAndPostalCode'.format(context.System.apiEndpoint, context.System.device.deviceId),
headers = {'Authorization':'Bearer {}'.format(context.System.apiAccessToken)}).json()
postCode = res['postalCode']
countryCode = res['countryCode']
# key specific to the user
apiAccessKey = 'your-api-key'
# Google Geocoding API: provides latitude and longitude from country code and postal code
res = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address={0},{1}&key={2}'
.format(countryCode, postCode, apiAccessKey)).json()
lat = res['results'][0]['geometry']['location']['lat']
lng = res['results'][0]['geometry']['location']['lng']
# Google's Time Zone API: provides timezone from latitude and longitude.
res = requests.get('https://maps.googleapis.com/maps/api/timezone/json?location={0},{1}×tamp={2}&key={3}'
.format(lat, lng, datetime.datetime.timestamp(datetime.datetime.now()),apiAccessKey)).json()
timezone = res['timeZoneId']
来源:https://stackoverflow.com/questions/38352271/alexa-skills-custom-slot-for-date-times