Skip to content

Instantly share code, notes, and snippets.

@cssquirrel
Created January 17, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cssquirrel/999a34c8cabf2506a50c to your computer and use it in GitHub Desktop.
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.
// 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);
}
});
}
}
@Matzefication
Copy link

Geht leider nicht mehr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment