From 07f8674adac8650cf264d563c5c363e6ca394507 Mon Sep 17 00:00:00 2001 From: Jeff Anderson <75326061+troutkilroy@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:20:40 -0700 Subject: [PATCH] Added support for call control gather function (#77) * Added support for call control gather function * Added JSON attribute * Cleanup --------- Co-authored-by: jeff anderson --- .../CallControl/Gather/CallGatherResponse.cs | 31 ++++++++ .../Calls/CallControl/CallControlService.cs | 12 +++ .../Gather/CallControlGatherOptions.cs | 73 +++++++++++++++++++ .../Gather/CallControlGatherService.cs | 44 +++++++++++ .../Gather/CallControlGatherTest.cs | 54 ++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 src/Telnyx.net/Entities/Calls/CallControl/Gather/CallGatherResponse.cs create mode 100644 src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherOptions.cs create mode 100644 src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherService.cs create mode 100644 src/TelnyxTests/Services/Calls/CallControl/Gather/CallControlGatherTest.cs diff --git a/src/Telnyx.net/Entities/Calls/CallControl/Gather/CallGatherResponse.cs b/src/Telnyx.net/Entities/Calls/CallControl/Gather/CallGatherResponse.cs new file mode 100644 index 0000000..fe55c96 --- /dev/null +++ b/src/Telnyx.net/Entities/Calls/CallControl/Gather/CallGatherResponse.cs @@ -0,0 +1,31 @@ +namespace Telnyx +{ + using System.Runtime.Serialization; + using Newtonsoft.Json; + + /// + /// Gather Response. + /// + public class CallGatherResponse : TelnyxEntity + { + /// + /// The status of the Call. + /// + /// Status of the Call. + [JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] + public enum ResultEnum + { + /// + /// Enum PendingEnum for answered + /// + [EnumMember(Value = "ok")] + Success = 0, + } + + /// + /// Gets or sets destination number or SIP URI of the call. + /// + [JsonProperty("result")] + public ResultEnum? Result { get; set; } + } +} diff --git a/src/Telnyx.net/Services/Calls/CallControl/CallControlService.cs b/src/Telnyx.net/Services/Calls/CallControl/CallControlService.cs index 73c2caf..9341279 100644 --- a/src/Telnyx.net/Services/Calls/CallControl/CallControlService.cs +++ b/src/Telnyx.net/Services/Calls/CallControl/CallControlService.cs @@ -18,6 +18,7 @@ public class CallControlService private readonly CallControlBridgeService callControlBridgeService; private readonly CallControlForkStartService callControlForkStartService; private readonly CallControlForkStopService callControlForkStopService; + private readonly CallControlGatherService callControlGatherService; private readonly CallControlGatherUsingAudioService callControlGatherUsingAudioService; private readonly CallControlGatherUsingSpeakService callControlGatherUsingSpeakService; private readonly CallControlHangupService callControlHangupService; @@ -40,6 +41,7 @@ public CallControlService() this.callControlBridgeService = new CallControlBridgeService(); this.callControlForkStartService = new CallControlForkStartService(); this.callControlForkStopService = new CallControlForkStopService(); + this.callControlGatherService = new CallControlGatherService(); this.callControlGatherUsingAudioService = new CallControlGatherUsingAudioService(); this.callControlGatherUsingSpeakService = new CallControlGatherUsingSpeakService(); this.callControlHangupService = new CallControlHangupService(); @@ -129,6 +131,16 @@ public virtual async Task ForkStopAsync(CallControlForkSto return await this.callControlForkStopService.CreateAsync(this.CallControlId, options, postFix, requestOptions, ct); } + public virtual CallGatherResponse Gather(CallControlGatherOptions options, string postFix = "actions/gather", RequestOptions requestOptions = null) + { + return this.callControlGatherService.Create(this.CallControlId, options, postFix, requestOptions); + } + + public virtual async Task GatherAsync(CallControlGatherOptions options, string postFix = "actions/gather", RequestOptions requestOptions = null, CancellationToken ct = default) + { + return await this.callControlGatherService.CreateAsync(this.CallControlId, options, postFix, requestOptions, ct); + } + public virtual CallGatherUsingAudioResponse GatherUsingAudio(CallControlGatherUsingAudioOptions options, string postFix = "actions/gather_using_audio", RequestOptions requestOptions = null) { return this.callControlGatherUsingAudioService.Create(this.CallControlId, options, postFix, requestOptions); diff --git a/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherOptions.cs b/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherOptions.cs new file mode 100644 index 0000000..78c95c8 --- /dev/null +++ b/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherOptions.cs @@ -0,0 +1,73 @@ +namespace Telnyx +{ + using System; + using Newtonsoft.Json; + + /// + /// CallControlGatherUsingSpeakCreateOptions. + /// + public class CallControlGatherOptions : BaseOptions + { + /// + /// Gets or sets the minimum number of digits to fetch. This parameter has a minimum value of 1. + /// Default: 1. + /// + [JsonProperty("minimum_digits")] + public int MinimumDigits { get; set; } = 1; + + /// + /// Gets or sets the maximum number of digits to fetch. This parameter has a maximum value of 128. + /// Default: 128. + /// + [JsonProperty("maximum_digits")] + public int MaximumDigits { get; set; } = 128; + + /// + /// Gets or sets the number of milliseconds to wait to complete the request. + /// Default: 60000. + /// + [JsonProperty("timeout_millis")] + public int TimeoutMillis { get; set; } = 60000; + + /// + /// Gets or sets the digit used to terminate input if fewer than maximum_digits digits have been gathered. + /// Default: "#". + /// + [JsonProperty("terminating_digit")] + public string TerminatingDigit { get; set; } + + /// + /// Gets or sets a list of all digits accepted as valid. + /// Default: "0123456789#*". + /// + [JsonProperty("valid_digits")] + public string ValidDigits { get; set; } + + /// + /// Gets or sets the number of milliseconds to wait for input between digits. + /// Default: 5000. + /// + [JsonProperty("inter_digit_timeout_millis")] + public int InterDigitTimeoutMillis { get; set; } = 5000; + + /// + /// Gets or sets the number of milliseconds to wait for the first DTMF. + /// Default: 5000. + /// + /// + [JsonProperty("initial_timeout_millis")] + public int InitialTimeoutMillis { get; set; } = 5000; + + /// + /// Gets or sets use this field to add state to every subsequent webhook. It must be a valid Base-64 encoded string. + /// + [JsonProperty("client_state")] + public string ClientState { get; set; } + + /// + /// Gets or sets use this field to avoid duplicate commands. Telnyx will ignore commands with the same. + /// + [JsonProperty("command_id")] + public Guid? CommandId { get; set; } + } +} diff --git a/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherService.cs b/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherService.cs new file mode 100644 index 0000000..1a48dfc --- /dev/null +++ b/src/Telnyx.net/Services/Calls/CallControl/Gather/CallControlGatherService.cs @@ -0,0 +1,44 @@ +namespace Telnyx +{ + using System.Threading; + using System.Threading.Tasks; + + /// + /// CallControlGatherService. + /// + public class CallControlGatherService : Service, + INestedCreatableWithIdInMid + { + /// + /// Initializes a new instance of the class. + /// + public CallControlGatherService() + : base(null) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// api key. + public CallControlGatherService(string apiKey) + : base(apiKey) + { + } + + /// + public override string BasePath => "/calls"; + + /// + public virtual CallGatherResponse Create(string id, CallControlGatherOptions options, string postFix = "actions/gather", RequestOptions requestOptions = null) + { + return this.CreateEntity(id, postFix, options, requestOptions, string.Empty); + } + + /// + public async Task CreateAsync(string parentId, CallControlGatherOptions createOptions, string postFix = "actions/gather", RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken)) + { + return await this.CreateEntityAsync(parentId, postFix, createOptions, requestOptions, string.Empty, cancellationToken); + } + } +} \ No newline at end of file diff --git a/src/TelnyxTests/Services/Calls/CallControl/Gather/CallControlGatherTest.cs b/src/TelnyxTests/Services/Calls/CallControl/Gather/CallControlGatherTest.cs new file mode 100644 index 0000000..2cbeded --- /dev/null +++ b/src/TelnyxTests/Services/Calls/CallControl/Gather/CallControlGatherTest.cs @@ -0,0 +1,54 @@ +// +// Copyright (c) Telnyx. All rights reserved. +// + +namespace TelnyxTests.Services.Calls.CallCommands +{ + using System.Threading.Tasks; + using Telnyx; + using Xunit; + + public class CallControlGatherTest : BaseTelnyxTest + { + private const string CallControllId = "call_123"; + + private readonly CallControlGatherService service; + private readonly CallControlGatherOptions createOptions; + + public CallControlGatherTest(MockHttpClientFixture mockHttpClientFixture) + : base(mockHttpClientFixture) + { + this.service = new CallControlGatherService(); + + this.createOptions = new CallControlGatherOptions() + { + ClientState = "aGF2ZSBhIG5pY2UgZGF5ID1d", + CommandId = new System.Guid("891510ac-f3e4-11e8-af5b-de00688a4901"), + InterDigitTimeoutMillis = 5000, + TimeoutMillis = 60000, + ValidDigits = "123", + TerminatingDigit = "#", + MaximumDigits = 10, + MinimumDigits = 1, + }; + } + + [Fact] + public void Create() + { + var message = this.service.Create(CallControllId, this.createOptions); + //this.AssertRequest(HttpMethod.Post, $"/v2/calls/{CallControllId}/actions/gather"); + Assert.NotNull(message); + Assert.Equal(typeof(CallGatherUsingAudioResponse), message.GetType()); + } + + [Fact] + public async Task CreateAsync() + { + var message = await this.service.CreateAsync(CallControllId, this.createOptions); + //this.AssertRequest(HttpMethod.Post, $"/v2/calls/{CallControllId}/actions/gather"); + Assert.NotNull(message); + Assert.Equal(typeof(CallGatherUsingAudioResponse), message.GetType()); + } + } +}