Alexa Skills - get custom slot names using Flask-Ask
Amazon encourages developers to use Flask-Ask - the handy Python library for working with Alexa. Sadly, the project has been abandoned. They no longer take pull requests, you can't raise bugs against it, and the documentation is incomplete.
So this is how I solved an annoying problem - how to get the name of a custom slot.
Here's the code, with a fuller explanation afterwards.
Python 3from flask import Flask, render_template, request
from flask_ask import Ask, statement, question, session
app = Flask(__name__)
ask = Ask(app, '/')
@ask.intent("YourIntentName")
def your_intent_name():
content = request.get_json()
name = content['request']['intent']['slots']['YOUR_SLOT_NAME']['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value']['name']
Yeuch! What's going on?
Alexa lets us define custom slot names - these can be associated with any spoken text. For example, I might want the slot name "car" to be sent whether the user says "car" or "automobile" or "vehicle" or any other synonym.
In my case, I want to send my API the ID Code of a hospital.
If the user says "John Radcliff" or "Oxford" or "John Radcliff Hospital" - then my API should receive the ID RTH08
. It can then use that ID in a separate API call.
Here's the JSON that Alexa sends our API (I've truncated it for ease of reading).
JSON{
"request": {
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.1234",
"timestamp": "2019-06-17T06:54:52Z",
"locale": "en-GB",
"intent": {
"name": "CarPark",
"confirmationStatus": "NONE",
"slots": {
"hospital": {
"name": "hospital",
"value": "John radcliff",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.1234.hospitals",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "RTH08",
"id": "abc123"
}
}
]
}
]
},
}
}
}
}
}
A bit verbose, but easy enough to parse.
I've moaned before about Alexa skill development - but it is getting worse. As you can see from the above screenshot, the development website's contrast isn't great - which makes building a skill physically painful.
Add to that the outdated tutorials, the weird terminology, the multiple sites to use, broken links, and abandoned libraries... It's hard to feel enthusiastic about building more skills.
Amazon have gone down the classic route of paying developers to build for their platform. But I don't think that's enough.
The Alexa team need to work on the developer experience. A GUI like NODE-RED could be used to help build skills in one place. Why is it so complicated to deploy and test skills? Where are the official libraries which "just work"?
I honestly believe that one of the things holding back voice assistants from their full potential is the poor developer experience.
jotarun says:
Thx, this saves the day!