From 823b28020ba009346cd7f6720998e8b139554198 Mon Sep 17 00:00:00 2001 From: Ezequiel Jadib Date: Fri, 6 Jan 2017 22:35:29 -0300 Subject: [PATCH] [CSharp-SpeechToText] Update to Microsoft.Bot.Builder v3.5.0 --- .../Controllers/MessagesController.cs | 117 +++++++----------- CSharp/intelligence-SpeechToText/README.md | 21 ++-- .../MicrosoftCognitiveSpeechService.cs | 11 +- .../SpeechToText.csproj | 24 ++-- .../SpeechToText.sln | 5 + CSharp/intelligence-SpeechToText/Web.config | 12 ++ .../intelligence-SpeechToText/packages.config | 12 +- 7 files changed, 102 insertions(+), 100 deletions(-) diff --git a/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs b/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs index 63f8b21307..2a36f15756 100644 --- a/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs +++ b/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs @@ -33,9 +33,9 @@ public async Task Post([FromBody]Activity activity) var audioAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType.Equals("audio/wav") || a.ContentType.Equals("application/octet-stream")); if (audioAttachment != null) { - var stream = await GetImageStream(connector, audioAttachment); + var stream = await GetAudioStream(connector, audioAttachment); var text = await this.speechService.GetTextFromAudioAsync(stream); - message = ProcessText(activity.Text, text); + message = ProcessText(text); } else { @@ -61,44 +61,60 @@ public async Task Post([FromBody]Activity activity) return response; } - private static string ProcessText(string input, string text) + private static string ProcessText(string text) { string message = "You said : " + text + "."; - input = input?.Trim(); - - if (!string.IsNullOrEmpty(input)) + if (!string.IsNullOrEmpty(text)) { - var normalizedInput = input.ToUpper(); + var wordCount = text.Split(' ').Count(x => !string.IsNullOrEmpty(x)); + message += "\n\nWord Count: " + wordCount; - if (normalizedInput.Equals("WORD")) - { - var wordCount = text.Split(' ').Count(x => !string.IsNullOrEmpty(x)); - message += " Word Count: " + wordCount; - } - else if (normalizedInput.Equals("CHARACTER")) - { - var characterCount = text.Count(c => c != ' '); - message += " Character Count: " + characterCount; - } - else if (normalizedInput.Equals("SPACE")) - { - var spaceCount = text.Count(c => c == ' '); - message += " Space Count: " + spaceCount; - } - else if (normalizedInput.Equals("VOWEL")) - { - var vowelCount = text.ToUpper().Count("AEIOU".Contains); - message += " Vowel Count: " + vowelCount; - } - else + var characterCount = text.Count(c => c != ' '); + message += "\n\nCharacter Count: " + characterCount; + + var spaceCount = text.Count(c => c == ' '); + message += "\n\nSpace Count: " + spaceCount; + + var vowelCount = text.ToUpper().Count("AEIOU".Contains); + message += "\n\nVowel Count: " + vowelCount; + } + + return message; + } + + private static async Task GetAudioStream(ConnectorClient connector, Attachment audioAttachment) + { + using (var httpClient = new HttpClient()) + { + // The Skype attachment URLs are secured by JwtToken, + // you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image. + // https://github.com/Microsoft/BotBuilder/issues/662 + var uri = new Uri(audioAttachment.ContentUrl); + if (uri.Host.EndsWith("skype.com") && uri.Scheme == "https") { - var keywordCount = text.ToUpper().Split(' ').Count(w => w == normalizedInput); - message += " Keyword " + input + " found " + keywordCount + " times."; + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetTokenAsync(connector)); + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream")); } + + return await httpClient.GetStreamAsync(uri); } + } - return message; + /// + /// Gets the JwT token of the bot. + /// + /// + /// JwT token of the bot + private static async Task GetTokenAsync(ConnectorClient connector) + { + var credentials = connector.Credentials as MicrosoftAppCredentials; + if (credentials != null) + { + return await credentials.GetTokenAsync(); + } + + return null; } /// @@ -139,44 +155,5 @@ private async Task HandleSystemMessage(Activity activity) return null; } - - private static async Task GetImageStream(ConnectorClient connector, Attachment imageAttachment) - { - using (var httpClient = new HttpClient()) - { - // The Skype attachment URLs are secured by JwtToken, - // you should set the JwtToken of your bot as the authorization header for the GET request your bot initiates to fetch the image. - // https://github.com/Microsoft/BotBuilder/issues/662 - var uri = new Uri(imageAttachment.ContentUrl); - if (uri.Host.EndsWith("skype.com") && uri.Scheme == "https") - { - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetTokenAsync(connector)); - httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream")); - } - else - { - httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(imageAttachment.ContentType)); - } - - return await httpClient.GetStreamAsync(uri); - } - } - - /// - /// Gets the JwT token of the bot. - /// - /// - /// JwT token of the bot - private static async Task GetTokenAsync(ConnectorClient connector) - { - var credentials = connector.Credentials as MicrosoftAppCredentials; - if (credentials != null) - { - return await credentials.GetTokenAsync(); - } - - return null; - } - } } \ No newline at end of file diff --git a/CSharp/intelligence-SpeechToText/README.md b/CSharp/intelligence-SpeechToText/README.md index 186cb4cfb8..be59330341 100644 --- a/CSharp/intelligence-SpeechToText/README.md +++ b/CSharp/intelligence-SpeechToText/README.md @@ -8,19 +8,13 @@ A sample bot that illustrates how to use the Microsoft Cognitive Services Bing S The minimum prerequisites to run this sample are: * The latest update of Visual Studio 2015. You can download the community version [here](http://www.visualstudio.com) for free. -* The Bot Framework Emulator. To install the Bot Framework Emulator, download it from [here](https://aka.ms/bf-bc-emulator). Please refer to [this documentation article](https://docs.botframework.com/en-us/csharp/builder/sdkreference/gettingstarted.html#emulator) to know more about the Bot Framework Emulator. +* The Bot Framework Emulator. To install the Bot Framework Emulator, download it from [here](https://emulator.botframework.com/). Please refer to [this documentation article](https://github.com/microsoft/botframework-emulator/wiki/Getting-Started) to know more about the Bot Framework Emulator. * **[Recommended]** Visual Studio Code for IntelliSense and debugging, download it from [here](https://code.visualstudio.com/) for free. * This sample currently uses a free trial Microsoft Cognitive service key with limited QPS. Please subscribe to Bing Speech Api services [here](https://www.microsoft.com/cognitive-services/en-us/subscriptions) and update the `MicrosoftSpeechApiKey` key in key in [Web.config](Web.config) file to try it out further. ### Usage -Attach an audio file (wav format) and send an optional command as text. -Supported Commands: -* `WORD` - Counts the number of words. -* `CHARACTER` - Counts the number of characters excluding spaces. -* `SPACE` - Counts the number of spaces. -* `VOWEL` - Counts the number of vowels. -* Any other word will count the occurrences of that word in the transcribed text +Attach an audio file (wav format). ### Code Highlights @@ -62,8 +56,15 @@ public async Task GetTextFromAudioAsync(Stream audiostream) var response = await client.PostAsync(requestUri, binaryContent); var responseString = await response.Content.ReadAsStringAsync(); - dynamic data = JsonConvert.DeserializeObject(responseString); - return data.header.name; + try + { + dynamic data = JsonConvert.DeserializeObject(responseString); + return data.header.name; + } + catch (JsonReaderException ex) + { + throw new Exception(responseString, ex); + } } } } diff --git a/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs b/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs index f88e9a7006..9fab8bea8f 100644 --- a/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs +++ b/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs @@ -28,8 +28,15 @@ public async Task GetTextFromAudioAsync(Stream audiostream) var response = await client.PostAsync(requestUri, binaryContent); var responseString = await response.Content.ReadAsStringAsync(); - dynamic data = JsonConvert.DeserializeObject(responseString); - return data.header.name; + try + { + dynamic data = JsonConvert.DeserializeObject(responseString); + return data.header.name; + } + catch (JsonReaderException ex) + { + throw new Exception(responseString, ex); + } } } } diff --git a/CSharp/intelligence-SpeechToText/SpeechToText.csproj b/CSharp/intelligence-SpeechToText/SpeechToText.csproj index 7b7d2d658f..58a8c679a8 100644 --- a/CSharp/intelligence-SpeechToText/SpeechToText.csproj +++ b/CSharp/intelligence-SpeechToText/SpeechToText.csproj @@ -42,33 +42,33 @@ 4 - - packages\Autofac.3.5.2\lib\net40\Autofac.dll + + packages\Autofac.4.2.1\lib\net45\Autofac.dll True packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll True - - packages\Microsoft.Bot.Builder.3.2.1\lib\net46\Microsoft.Bot.Builder.dll + + packages\Microsoft.Bot.Builder.3.5.0\lib\net46\Microsoft.Bot.Builder.dll True - - packages\Microsoft.Bot.Builder.3.2.1\lib\net46\Microsoft.Bot.Connector.dll + + packages\Microsoft.Bot.Builder.3.5.0\lib\net46\Microsoft.Bot.Connector.dll True - - packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.2.206221351\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll + + packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.3.308261200\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll True - packages\Microsoft.Rest.ClientRuntime.2.3.2\lib\net45\Microsoft.Rest.ClientRuntime.dll + packages\Microsoft.Rest.ClientRuntime.2.3.4\lib\net45\Microsoft.Rest.ClientRuntime.dll True - packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.1\lib\net40\Microsoft.WindowsAzure.Configuration.dll + packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll True @@ -76,8 +76,8 @@ True - - packages\System.IdentityModel.Tokens.Jwt.4.0.2.206221351\lib\net45\System.IdentityModel.Tokens.Jwt.dll + + packages\System.IdentityModel.Tokens.Jwt.4.0.3.308261200\lib\net45\System.IdentityModel.Tokens.Jwt.dll True diff --git a/CSharp/intelligence-SpeechToText/SpeechToText.sln b/CSharp/intelligence-SpeechToText/SpeechToText.sln index e52440a7a5..9e3df9fb6c 100644 --- a/CSharp/intelligence-SpeechToText/SpeechToText.sln +++ b/CSharp/intelligence-SpeechToText/SpeechToText.sln @@ -5,6 +5,11 @@ VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpeechToText", "SpeechToText.csproj", "{A8BA1066-5695-4D71-ABB4-65E5A5E0C3D4}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "README", "README", "{D52A8E68-530F-4437-9CE8-F089EA054A6D}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/CSharp/intelligence-SpeechToText/Web.config b/CSharp/intelligence-SpeechToText/Web.config index 55052fd1cb..449d459d1c 100644 --- a/CSharp/intelligence-SpeechToText/Web.config +++ b/CSharp/intelligence-SpeechToText/Web.config @@ -70,6 +70,18 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CSharp/intelligence-SpeechToText/packages.config b/CSharp/intelligence-SpeechToText/packages.config index 818a7bc7a2..88cb35634e 100644 --- a/CSharp/intelligence-SpeechToText/packages.config +++ b/CSharp/intelligence-SpeechToText/packages.config @@ -1,15 +1,15 @@  - + - - - - + + + + - + \ No newline at end of file