Class: ConversationEngine

conversationEngine~ConversationEngine()

The primary class for scripts to use to define how they respond to users (intents) and how goals behave behave when triggered by the script.

This class is helped by the ScriptParser to build a model of the users script, the InputMgr to process data from Alexa and call intents in Scripts, and the OutputMgr to take calls from Scripts via the Response class and build an output for sending back to Alexa.

Constructor

new ConversationEngine()

Constructed and returned by Violet when a Voice Script initializes
Source:

Methods

addInputTypes(keyTypes)

Used to define parameters that can be expected from the user
Parameters:
Name Type Description
keyTypes Object key:value pairs representing varName:typeName. typeName can alternatively be an object for customSlots (with a values property) or for literals (with a sampleValues property)
Source:
Examples

basic usage

violet.addInputTypes({
  'name': 'AMAZON.US_FIRST_NAME',
});

violet.respondTo(['My name is [[name]]'],
 (response) => {
   response.say('I like the name [[name]]')
 });

defining custom types

violet.addInputTypes({
  'timeUnit': {
    type: 'timeUnitType',
    values: ['days', 'hours', 'minutes']
  }
});

defining literals (while literals are open ended, it is recommended to use custom types for better recognition)

violet.addInputTypes({
  'itemName': {
    type: 'AMAZON.LITERAL',
    sampleValues: ['Call Chris', 'Book an appointment']
  }
});

addPhraseEquivalents()

Gives a set of equivalent phrases
Source:
Example
violet.addPhraseEquivalents([
  ['My name is', 'I call myself'],
]);
violet.respondTo(['My name is [[name]]'],
  (response) => {
    response.say('I like the name [[name]]')
  });

defineGoal(goalDef)

Defines what should happen when a goal is triggered (by calling the addGoal method). Goals allow for the grouping of application and user responses.
Parameters:
Name Type Description
goalDef Object goal definition
Properties
Name Type Description
goal string name of the goal
resolve resolveCallback callback when the goal is triggered (required unless prompt or ask have been provided)
prompt string string to prompt the user (usually for information) when the goal has been triggered
ask string similar to prompt, except that user can be prompted multiple items, but asked only one question at a time
respondTo Array.<Object> array of intents than can be triggered only after this goal has been triggered - for details see the respondTo method (required if prompt or ask have been provided)
Source:
Examples

setting up goals to be triggered from a regular intent

violet.respondTo('What time does the [[airline]] flight arrive',
  (response) => {
    response.addGoal('flightArrivalTime');
});
violet.respondTo('What time does the flight arrive from [[city]]',
  (response) => {
    response.addGoal('flightArrivalTime');
});

when the user is asking for a flight arrival time and we want to check if dependencies have been provided

violet.defineGoal({
  goal: 'flightArrivalTime',
  resolve: (response) => {
    if (!response.ensureGoalFilled('airline')
        || !response.ensureGoalFilled('city')
        || !response.ensureGoalFilled('flightDay') ) {
          return false; // dependent goals not met
        }
    var airline = response.get('airline');
    var city = response.get('city');
    var flightDay = response.get('flightDay');
    flightArrivalTimeSvc.query(airline, city, flightDay, (arrivalTime)=>{
      response.say('Flight ' + airline + ' from ' + city + ' is expected to arrive ' + flightDay + ' at ' + arrivalTime);
    });
    return true;
  }
});

when the user is asking for a flight arrival time and has not provided the airline name

violet.defineGoal({
  goal: 'airline',
  prompt: ['What airline', 'What airlines are you looking for the arrival time?'],
  respondTo: [{
    expecting: '[[airline]]',
    resolve: (response) => {
      response.set('airline', response.get('airline') );
  }}]
});

when the user is asking for a flight arrival time and has not provided the originating city

violet.defineGoal({
  goal: 'city',
  prompt: ['What city do you want the flight to be arriving from'],
  respondTo: [{
    expecting: '[[city]]',
    resolve: (response) => {
      response.set('city', response.get('city') );
  }}]
});

respondTo(intentDef)

Declare how you are going to be responding to users
Parameters:
Name Type Description
intentDef Object intent definition
Properties
Name Type Description
expecting Array.<string> array of strings that a user could say
resolve resolveCallback callback when one of the `expecting` items is said
goal string (optional) when provided above is applied only during given goal
name Array.<string> (optional) when provided is used as the intent name (otherwise one is generated)
Source:
Examples

a basic example

violet.respondTo('Hello',
 (response) => {
   response.say('Hi');
 });

using most of the parameters

violet.respondTo({
   expecting: ['I live in [[city]]', 'My house is in [[city]]', 'We are renting in [[city]]'],
   resolve: (response) => {
    response.say('I like the city [[city]]')
}});

setCloseRequests(phrases)

Override the default phrases to close the session.
Parameters:
Name Type Description
phrases Array.<string> response or array of potential responses
Source:

setLaunchPhrases(phrases)

Override the default launch phrases. Parameter is used directly with the response.say() function when Alexa launches this app
Parameters:
Name Type Description
phrases Array.<string> response or array of potential responses
Source:
Documentation generated by JSDoc 3.5.5 on Mon Jul 23 2018 23:46:45 GMT-0400 (EDT)