question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add contexts, provide another layer of routing for intents

See original GitHub issue

I’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:open
  • Created 8 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
h0lmiecommented, Aug 9, 2016

I’ve taken a slightly different approach to this problem.

With a very simple change that adds routes to the schema object and app.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.

app.route('mainMenu', function(request, response){
  response.session('__STATE__', 'mainMenu');
  response.say('Do you like rock music?');
});

app.route('artistType', function(request, response){
  response.session('__STATE__', 'artistType');
  response.say('Do you like The Clash?');
});

app.route('albumName', function(request, response){
  response.session('__STATE__', 'albumName');
  response.say('Do you like London Calling?');
});

app.launch(function(request, response) {
  app.route.mainMenu(request, response);
});

app.intent('YesIntent', {
  'routes': {
    'mainMenu': 'artistType',
    'artistType': 'albumName'
  },
  'utterances':[
    'yes',
  ]},
  function(request, response) {
    var state = request.session('__STATE__'); //e.g. 'artistType'
    var func = this.schema.routes[state];
    app.route[func](request, response); //e.g. call app.route.albumName
  }
);

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?

0reactions
rossthedevignercommented, Nov 28, 2017

@h0lmie do you have a gist of how you accomplished this? i’m looking at trying todo the same thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intents and Intent Filters - Android Developers
An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to...
Read more >
Define topic context intent configurations
Type of condition that determines how the topic intent configuration is to be applied. Simple: Specify a condition using the condition builder.
Read more >
CLI Book 1: Cisco ASA Series General Operations CLI ...
Placing a context directly in front of another context is called cascading contexts; the outside interface of one context is the same interface ......
Read more >
Tune Routing Behavior - Digital Assistant - Oracle Help Center
Context -aware routing: If a user is already engaged with a skill, that skill is given more weight during intent resolution than intents...
Read more >
How to send an object from one Android Activity to another ...
If your Object Class implements Parcelable and Serializable then make sure you ... new ClassName(); Intent i = new Intent(context, EditActivity.class); i.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found