using System; using System.Drawing; using System.Speech.Recognition;//append the stuff between the quotes to References textbox: " | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Speech.dll" public static class VoiceBotScript { //--------------------------------------------------------------------------- /// /// long winded example of verbal confirmation macroscript, where the response /// must be "valid". To accept any response is much simpler, just remove the stuff /// between // and // /// then uncomment the commented line // for the simple voice prompt /// v2: cleaned up some for both clarity and updated coding style /// /// public static void Run(IntPtr windowHandle) { string str_valid_sites = "Google,Home,Eve-Gate,Discussions";//<<--some words may be harder to properly understand, so training may be needed to improve accuracy, or adding the false positives to the list of valid responses to mitigate them string str_urls = "https://www.google.com/advanced_search,http://www.WhereEverYouHomePageIs.net,https://gate.eveonline.com/,https://www.voicebot.net/Discussions/"; string str_url = ""; //<<--selected url based on user response string str_confirmation = "";//<<--verbal response converted to string bool bln_valid_response = true;//<<--validation flag bool bln_quit = false; //<<--failsafe flag bool bln_heard_correctly = true; //<<--flag to retry without using a retry int int_retries = 0;//<<--failsafe counter int int_index = 0; //<<--used to link valid response to the associated url //string str_confirmation = (string)Speech.SpeechRecognizer("Please select the site you would like to go to from the following {str_valid_sites}".Replace("{str_valid_sites}", str_valid_sites)); // bln_valid_response = false; BFS.Speech.TextToSpeech("Please select the site you would like to go to from the following"); BFS.Speech.TextToSpeech("{str_valid_sites}".Replace("{str_valid_sites}", str_valid_sites)); while( bln_valid_response == false || bln_quit == false ) { str_confirmation = (string)Speech.SpeechRecogizer(string.Empty);//<<--we've moved the voice prompt out side the loop so the system waits for a valid response without further "prompt" BFS.Speech.TextToSpeech("I heard: {str_confirmation}".Replace("{str_confirmation}", str_confirmation)); bln_heard_correctly = BFS.Dialog.GetUserConfirm("I heard: {str_confirmation}".Replace("{str_confirmation}", str_confirmation));//<<--so use can see the recognized speech, and update the valid list, or whatever... . if( !bln_heard_correctly ) { continue; } else {/*>>>>>do nothing*/; }//<<--skip processing, and try again, we were not heard correctly BFS.Speech.TextToSpeech("looking for a matching valid site"); int_index = 0; // foreach( string str_valid_site in str_valid_sites.Split(',') ) { BFS.Speech.TextToSpeech("site: {str_valid_site}".Replace("{str_valid_site}", str_valid_site)); if( str_confirmation.Trim().ToLower() == str_valid_site.Trim().ToLower() ) { BFS.Speech.TextToSpeech("match found."); str_url = str_urls.Split(',')[int_index]; bln_valid_response = true; break;//<<--breach the loop } else { BFS.Speech.TextToSpeech("wasn't a match, continuing."); }// int_index++; }// //>>>>>was the response valid? if( bln_valid_response == false ) { int_retries++;//<<--increment failsafe value, this will allow the prompt to fail after so many retries //>>>>>see if we want to provide "help" to the user... switch( int_retries ) { case 1: BFS.Speech.TextToSpeech("I require a site chosen from the provided list: {str_valid_sites}.".Replace("{str_valid_sites}", str_valid_sites)); break; case 3: BFS.Speech.TextToSpeech("If I may, the following is the list of valid responses: {str_valid_sites}.".Replace("{str_valid_sites}", str_valid_sites)); break; default: if( int_retries >= 5 ) { BFS.Speech.TextToSpeech("As, I am unable to understand your response, I will accept this as a failure to respond and abort. There may be a technical fault with the voice input system if you were indeed responding."); bln_quit = true; } else {/*>>>>>do nothing*/; } break; }// } else { break;//<<--breach the loop }// }// // if( bln_valid_response ) { if( BFS.Dialog.GetUserConfirm("You selected site \"{str_url}\" End of line.".Replace("{str_url}", str_url)) ) { BFS.Web.OpenUrl(str_url); } else {/*>>>>>do nothing*/; } // } else {/*>>>>>do nothing*/; } }// }/**/ //===================================================================== /// /// Author: Darkstrumn /// Function: Aux speech functions, such as voice prompting /// public static class Speech { //--------------------------------------------------------------------- /// /// SpeechRecogizer provide voice prompts, where the user can be prompted /// aurally to respond verbally and the response is converted to string and /// returned for further processing /// /// /// public static string SpeechRecogizer(string str_voice_prompt) { string str_return = ""; SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); Grammar dictationGrammar = new DictationGrammar(); recognizer.LoadGrammar(dictationGrammar); BFS.Speech.TextToSpeech(str_voice_prompt); // try { recognizer.SetInputToDefaultAudioDevice(); RecognitionResult result = recognizer.Recognize(); str_return = result.Text; } catch( InvalidOperationException exception ) { BFS.Dialog.ShowMessageError("Error detected during sound acquisition: {exception.Source} - {exception.Message}.".Replace("{exception.Source}", exception.Source).Replace("{exception.Message}", exception.Message)); } finally { recognizer.UnloadAllGrammars(); }// return (str_return); }// //------------------------------------------------------------------------ }/**/