Created
January 17, 2016 18:29
-
-
Save cssquirrel/999a34c8cabf2506a50c to your computer and use it in GitHub Desktop.
Alternate function for annyang.js that makes it easier to enter commands with dynamically generated text instead of only literal text strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Insert this into annyang.js to have alternate command functionality | |
/** | |
* @method addCommandsWithDynamicText | |
* @param {array of Object} commands | |
* @param {string} commands[i].phrase - The speech that annyang is listening for to trigger from. | |
* @param {function or Object} commands[i].callback - If a function, a callback to trigger if the phrase is heard. If an object, it has a callback parameter (which is the function) and a regexp parameter (which contains an optional regexp string to use for the phrase). | |
* @description An alternate way to give annyang commands that makes it easier to have variables in your code with dynamically generated strings to pass into the phrase, instead of only allowing text like addCommands() | |
*/ | |
addCommandsWithDynamicText: function(commands) { | |
if (commands && commands.length) { | |
commands.forEach(function(command) { | |
if (typeof command.callback === "function") { | |
registerCommand(commandToRegExp(command.phrase), command.callback, command.phrase); | |
} else if (typeof command.callback === "object" && command.callback.regexp instanceof RegExp) { | |
registerCommand(new RegExp(command.callback.regexp.source, 'i'), command.callback.callback, command.phrase); | |
} else { | |
root.console.log('Can not register command: %c' + command.phrase, debugStyle); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Geht leider nicht mehr.