Create a voice prompt with artyom.js

The newDictation method is a function that expects a single object as first argument with the following properties:

Property name Type This text will be
question String This text will be synthesized by artyom when the prompt is started
options Array of Strings These are the available answers for the given prompt
beforePrompt Function This action will be executed just before artyom ask
onStartPrompt Function This action will be executed in the moment that artyom starts talk
onEndPrompt Function This action will be executed in the moment that artyom stops talk
onMatch Function

This action will be executed in the moment that artyom recognizes a given option.

The given parameters are the same given as a command (index of the matched command in the array,wildcard).
A function MUST be returned in order to be executed finally, so declare a function in a variable and return it.

How does a voice prompt works

Artyom provides you with tools that allows you to trigger actions with your voice, sometimes that function will need to do more things inside that command, well, artyom.newPrompt creates a more interactive way to do these things.

The great Tony Stark said once:

Tony Stark "Jarvis make me a sandwich"

However, there was no bread, therefore artyom needed to ask something later. 

Important to know

  • When an artyom prompt is executed, none of the previously injected commands are available, only the given options will trigger an action.
  • The prompt must be answered, otherwise any of the previously available commands can't be executed. So, is recommendable to add the "Exit" or "Quit" or "Cancel" option, and everything will run ok.

Example

The following prompt will make a very simple question:

artyom.newPrompt({
    question:"We have no cheese, you want it without cheese anyway ?",
    options:["Yes","No","Buy some cheese"],
    beforePrompt: () => {
        console.log("Before ask");
    },
    onStartPrompt: () => {
        console.log("The prompt is being executed");
    },
    onEndPrompt: () => {
        console.log("The prompt has been executed succesfully");
    },
    onMatch: (i) => { // i returns the index of the given options
        var action;

        if(i == 0){ 
            action =  () => {
                artyom.say("Your sandwich will be ready in a minute");
            }
        }

        if(i == 1){ 
            action =  () => {
                artyom.say("You may consider to order some cheese later");
            }
        }

        if(i == 2){
            action = () => {
                artyom.say("I'll order some cheese via eBay");
            }
        }

        // A function needs to be returned in onMatch event
        // in order to accomplish what you want to execute
        return action; 
    }
});

Example smart

The voice prompt supports the "smart" command too:

artyom.newPrompt({
    question:"How many centimeters should I cut?",
    //We set the smart property to true to accept wildcards
    smart:true,
    options:["Cut * centimeters","Remove * centimeters"],
    beforePrompt: () => {
        console.log("Before ask");
    },
    onStartPrompt:  () => {
        console.log("The prompt is being executed");
    },
    onEndPrompt: () => {
        console.log("The prompt has been executed succesfully");
    },
    onMatch: (i,wildcard) => {// i returns the index of the given options
        var action;

        var totalCentimeters = parseInt(wildcard);

        action = () => {
            alert(wildcard + " centimeters will be removed of your sandwich!");
        };

        // A function needs to be returned in onMatch event
        // in order to accomplish what you want to execute
        return action;                       
    }
});