This function is used to handle the recognized text by artyom is artyom is enabled in remote mode. It will be executed everytime the user speaks and some text is recognized. The main goal of this method is that you can customize the way in which the commands are processed, so you can use the recognized text and process them in the way you want.
Enable remote mode
The remoteProcessorService will be ignored if you don't provide the remote mode at the initialization of artyom. Be sure to initialize artyom in remote mode:
window.onload = function(){
artyom.initialize({
lang:"en-GB",
debug:true,
continuous:true,
listen:true,
// Important to use the custom processor
mode:"remote"
}).then(function(){
console.log("Artyom has been correctly initialized");
console.log("The following array shouldn't be empty" , artyom.getVoices());
}).catch(function(){
console.error("An error occurred during the initialization");
});
};
Once artyom is initialized, the custom processor will be used (if set).
Custom server processor
Create a custom service that processes commands stored in a database from your server (is up to you the configuration in the server side):
artyom.remoteProcessorService(function(data){
// An object with information about the recognized text
// by the speech recognition API
console.info(data);
if(data.isFinal){
// execute an ajax to a server
// which process the command
// with something like SQL etc..
$.ajax({
url:"command-processor",
dataType:"JSON",
data:{
recognized: data.text
},
success:function(dataFromServer){
// The response should be a json object like
//{
// "name": "hello",
// "action":"alert('Hi, you said hello !');"
//}
console.log(dataFromServer);
// Will alert
eval(dataFromServer.action);
}
});
}
});
Custom local processor
The custom function doesn't need to be remote, so you can handle the recognized text in the way you want:
artyom.remoteProcessorService(function(data){
var customDatabaseCommands = [
{
command_word: "Hello",
execute: function(){
alert("Hello");
}
}
];
if(data.isFinal){
customDatabaseCommands.forEach(function(item){
// If the command_word property of my command is the same
// that what you said then execute the command
if((item.command_word).indexOf(data.text) != -1){
// Execute the command, in this if you say hello, it will trigger
item.execute();
}
});
}
});