Skip to content

Commit

Permalink
Add Dtmf Tone string comparison (#34775)
Browse files Browse the repository at this point in the history
* Add Dtmf Tone string comparison

The Dtmf tone is casted to single char like "1", "#" etc. The collecton
of dtmf tones are translated to string like "1234#*".

* change tone from string to char

* use lambda instead of for loop

* update api
  • Loading branch information
wangrui-msft authored Mar 13, 2023
1 parent 46ebe00 commit f81272b
Show file tree
Hide file tree
Showing 16 changed files with 610 additions and 515 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ public partial class CollectTonesResult : Azure.Communication.CallAutomation.Rec
{
internal CollectTonesResult() { }
public System.Collections.Generic.IReadOnlyList<Azure.Communication.CallAutomation.DtmfTone> Tones { get { throw null; } }
public string ConvertToString() { throw null; }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public partial struct ContentTransferOptions : System.IEquatable<Azure.Communication.CallAutomation.ContentTransferOptions>
Expand Down Expand Up @@ -471,6 +472,7 @@ public CreateGroupCallOptions(System.Collections.Generic.IEnumerable<Azure.Commu
public static bool operator ==(Azure.Communication.CallAutomation.DtmfTone left, Azure.Communication.CallAutomation.DtmfTone right) { throw null; }
public static implicit operator Azure.Communication.CallAutomation.DtmfTone (string value) { throw null; }
public static bool operator !=(Azure.Communication.CallAutomation.DtmfTone left, Azure.Communication.CallAutomation.DtmfTone right) { throw null; }
public char ToChar() { throw null; }
public override string ToString() { throw null; }
}
public partial class EventProcessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,75 @@ namespace Azure.Communication.CallAutomation
[JsonConverter(typeof(EquatableEnumJsonConverter<DtmfTone>))]
public readonly partial struct DtmfTone
{
/// <summary>
/// The Dtmf Tone in character format.
/// </summary>
public char ToChar()
{
string originalValue = ToString();
return ConvertToChar(originalValue);
}

/// <summary>
/// Convert the tone from string to char digit like "1", "2", "#".
/// </summary>
private static char ConvertToChar(string value)
{
char charValue = ' ';
switch (value)
{
case "zero":
charValue = '0';
break;
case "one":
charValue = '1';
break;
case "two":
charValue = '2';
break;
case "three":
charValue = '3';
break;
case "four":
charValue = '4';
break;
case "five":
charValue = '5';
break;
case "six":
charValue = '6';
break;
case "seven":
charValue = '7';
break;
case "eight":
charValue = '8';
break;
case "nine":
charValue = '9';
break;
case "a":
charValue = 'a';
break;
case "b":
charValue = 'b';
break;
case "c":
charValue = 'c';
break;
case "d":
charValue = 'd';
break;
case "pound":
charValue = '#';
break;
case "asterisk":
charValue = '*';
break;
default:
break;
}
return charValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;
using Azure.Core;

namespace Azure.Communication.CallAutomation
Expand All @@ -14,5 +15,13 @@ public partial class CollectTonesResult : RecognizeResult
/// </summary>
[CodeGenMember("Tones")]
public IReadOnlyList<DtmfTone> Tones { get; }

/// <summary>
/// Convert the collection of tones to a string like "12345#".
/// </summary>
public string ConvertToString()
{
return string.Join("", Tones.Select(x => x.ToChar()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Azure.Communication.CallAutomation.Tests.Events
{
public class CallAutomationEventParserTests
{
private static string DTMF_RESULT_JSON = "{\"recognizeResult\":{},\"collectTonesResult\":{\"tones\":[\"five\"]},\"choiceResult\":{\"label\":null,\"recognizedPhrase\":null},\"recognitionType\":\"dtmf\",\"callConnectionId\":\"callConnectionId\",\"serverCallId\":\"serverCallId\",\"correlationId\":\"correlationId\",\"operationContext\":\"operationContext\",\"resultInformation\":{\"code\":200,\"subCode\":8531,\"message\":\"Action completed, max digits received\"}}";
private static string DTMF_RESULT_JSON = "{\"recognizeResult\":{},\"collectTonesResult\":{\"tones\":[\"five\", \"six\", \"pound\"]},\"choiceResult\":{\"label\":null,\"recognizedPhrase\":null},\"recognitionType\":\"dtmf\",\"callConnectionId\":\"callConnectionId\",\"serverCallId\":\"serverCallId\",\"correlationId\":\"correlationId\",\"operationContext\":\"operationContext\",\"resultInformation\":{\"code\":200,\"subCode\":8531,\"message\":\"Action completed, max digits received\"}}";
private static string CHIOCE_RESULT_JSON = "{\"recognizeResult\":{},\"collectTonesResult\":{\"tones\":[]},\"choiceResult\":{\"label\":\"testLabel\",\"recognizedPhrase\":\"testRecognizePhrase\"},\"recognitionType\":\"choices\",\"callConnectionId\":\"callConnectionId\",\"serverCallId\":\"serverCallId\",\"correlationId\":\"correlationId\",\"operationContext\":\"operationContext\",\"resultInformation\":{\"code\":200,\"subCode\":8531,\"message\":\"Action completed, max digits received\"}}";
[Test]
public void EventParserShouldParseEventWithEventDataAndType()
Expand Down Expand Up @@ -523,8 +523,10 @@ public void RecognizeCompletedWithDtmfEventParsed_Test()
Assert.AreEqual(200, recognizeCompleted.ResultInformation?.Code);
if (recognizeResult is CollectTonesResult collectToneRecognizedResult)
{
string toneResults = collectToneRecognizedResult.ConvertToString();
Assert.NotZero(collectToneRecognizedResult.Tones.Count());
Assert.AreEqual(DtmfTone.Five, collectToneRecognizedResult.Tones.First());
Assert.AreEqual(toneResults, "56#");
}
}
else
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit f81272b

Please sign in to comment.