Skip to content

Commit

Permalink
[CSharp-SpeechToText] Update to Microsoft.Bot.Builder v3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ejadib authored Jan 7, 2017
1 parent 34d4433 commit 823b280
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 100 deletions.
117 changes: 47 additions & 70 deletions CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public async Task<HttpResponseMessage> 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
{
Expand All @@ -61,44 +61,60 @@ public async Task<HttpResponseMessage> 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<Stream> 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;
/// <summary>
/// Gets the JwT token of the bot.
/// </summary>
/// <param name="connector"></param>
/// <returns>JwT token of the bot</returns>
private static async Task<string> GetTokenAsync(ConnectorClient connector)
{
var credentials = connector.Credentials as MicrosoftAppCredentials;
if (credentials != null)
{
return await credentials.GetTokenAsync();
}

return null;
}

/// <summary>
Expand Down Expand Up @@ -139,44 +155,5 @@ private async Task<Activity> HandleSystemMessage(Activity activity)

return null;
}

private static async Task<Stream> 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);
}
}

/// <summary>
/// Gets the JwT token of the bot.
/// </summary>
/// <param name="connector"></param>
/// <returns>JwT token of the bot</returns>
private static async Task<string> GetTokenAsync(ConnectorClient connector)
{
var credentials = connector.Credentials as MicrosoftAppCredentials;
if (credentials != null)
{
return await credentials.GetTokenAsync();
}

return null;
}

}
}
21 changes: 11 additions & 10 deletions CSharp/intelligence-SpeechToText/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,8 +56,15 @@ public async Task<string> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ public async Task<string> 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);
}
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions CSharp/intelligence-SpeechToText/SpeechToText.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,42 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
<Reference Include="Autofac, Version=4.2.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>packages\Autofac.4.2.1\lib\net45\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Chronic, Version=0.3.2.0, Culture=neutral, PublicKeyToken=3bd1f1ef638b0d3c, processorArchitecture=MSIL">
<HintPath>packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Bot.Builder, Version=3.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.2.1\lib\net46\Microsoft.Bot.Builder.dll</HintPath>
<Reference Include="Microsoft.Bot.Builder, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.5.0\lib\net46\Microsoft.Bot.Builder.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Bot.Connector, Version=3.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.2.1\lib\net46\Microsoft.Bot.Connector.dll</HintPath>
<Reference Include="Microsoft.Bot.Connector, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.5.0\lib\net46\Microsoft.Bot.Connector.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.2.33, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.2.206221351\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.3.42, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.3.308261200\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Rest.ClientRuntime.2.3.2\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<HintPath>packages\Microsoft.Rest.ClientRuntime.2.3.4\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.1\lib\net40\Microsoft.WindowsAzure.Configuration.dll</HintPath>
<HintPath>packages\Microsoft.WindowsAzure.ConfigurationManager.3.2.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.20622.1351, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\System.IdentityModel.Tokens.Jwt.4.0.2.206221351\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.30826.1200, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\System.IdentityModel.Tokens.Jwt.4.0.3.308261200\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net" />
Expand Down
5 changes: 5 additions & 0 deletions CSharp/intelligence-SpeechToText/SpeechToText.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions CSharp/intelligence-SpeechToText/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.30826.1200" newVersion="4.0.30826.1200" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Protocol.Extensions" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.3.42" newVersion="1.0.3.42" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
12 changes: 6 additions & 6 deletions CSharp/intelligence-SpeechToText/packages.config
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="3.5.2" targetFramework="net46" />
<package id="Autofac" version="4.2.1" targetFramework="net46" />
<package id="Chronic.Signed" version="0.3.2" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.Bot.Builder" version="3.2.1" targetFramework="net46" />
<package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.2.206221351" targetFramework="net46" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net46" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net46" />
<package id="Microsoft.Bot.Builder" version="3.5.0" targetFramework="net46" />
<package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.3.308261200" targetFramework="net46" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.4" targetFramework="net46" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.3" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.2.206221351" targetFramework="net46" />
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.3.308261200" targetFramework="net46" />
</packages>

0 comments on commit 823b280

Please sign in to comment.