Add contexts, provide another layer of routing for intents
See original GitHub issueI’ve got a feature idea that I am going to implement in to my own fork of this. I’d be happy to submit it as a Pull Request if this is something you’d like!
The feature introduces the idea of “Contexts” which provide another layer of routing for intents. Basically, registered Intents can have one or more “Contexts” associated with them. When a request comes in indicating that it is in a particular context (by way of a private sessionAttribute managed by the module), the actual Intent event that fires will be the one that is for that context.
This sort of thing will be very useful in situations where many different multi-step “conversational” responses are expected. For example, there might be 5 different situations in which Alexa asks the user a yes or no question, and so that same “YesIntent” would need to behave very differently in each. Currently the way to deal with this is:
app.intent("YesIntent", function(req, res) {
if (req.session('question' === "AreYouHappy") {
// User answered "Yes" to "Are you happy?"
} else if (req.session('question' === "IsItRaining") {
// User answered "Yes" to "Is it raining?"
} else if (req.session('question' === "HaveYouEaten") {
// User answered "Yes" to "Have you eaten?"
}
}
But with my proposed implementation, it would be this, which is much easier to understand at a glance what is going on. Not to mention more modular!
app.intent({intent: 'YesIntent', context: 'AreYouHappy'}, function(req, res) {
// User answered "Yes" to "Are you happy?"
}
app.intent({intent: 'YesIntent', context: 'IsItRaining'}, function(req, res) {
// User answered "Yes" to "Is it raining?"
}
app.intent({intent: 'YesIntent', context: 'HaveYouEaten'}, function(req, res) {
// User answered "Yes" to "Have you eaten?"
}
If this were implemented to have fallbacks to handle Intents not found in the specific context as well as allow lists of contexts to be specified for a single event, this could end up being very powerful and easy to use for the developer.
Anyway, I’m starting work on it, but let me know if this is something you’d like PRd here!
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (2 by maintainers)
Top GitHub Comments
I’ve taken a slightly different approach to this problem.
With a very simple change that adds
routes
to the schema object andapp.route
as a top level app object (merely to keep the style consistent), I can route intents to different functions, based on a__STATE__
session variable.This leaves intents responsible only for routing, and routes responsible for the output and user flow. A route can also be re-used in different intents.
Now, when YesIntent is invoked, it calls the correct route function based on where the user came from i.e. the last
__STATE__
.To make it cleaner, setting the
__STATE__
session and calling the correct route function could be handled automatically by alexa-app in the routes and intents – the idea being to keep the default behaviour the same and fallback to the normal intent function call if no routes are defined.It feels a little more straight-forward than nested contexts, and this demo functionality only required a couple of one line changes to
alexa.app
.What do people think of this type of approach?
@h0lmie do you have a gist of how you accomplished this? i’m looking at trying todo the same thing.