Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 3d1bd9f

Browse files
committed
Added minor changes to check for timeout
1 parent f555360 commit 3d1bd9f

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

packages/Telephony/Actions/BatchFixedLengthInput.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class BatchFixedLengthInput : BatchRegexInput
2222
[JsonProperty("$kind")]
2323
public new const string Kind = "Microsoft.Telephony.BatchFixedLengthInput";
2424

25+
private const string _dtmfCharacterRegex = @"^[\d#\*]+$";
2526
private const string _interruptionMaskRegex = @"^[\d]+$";
2627
private int _batchLength;
2728

@@ -65,9 +66,18 @@ public int BatchLength
6566
}
6667

6768
/// <inheritdoc/>
68-
public override Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
69+
public async override Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
6970
{
70-
return base.ContinueDialogAsync(dc, cancellationToken);
71+
if ((dc.Context.Activity.Type == ActivityTypes.Message) &&
72+
(Regex.Match(dc.Context.Activity.Text, _dtmfCharacterRegex).Success || dc.State.GetValue(TurnPath.Interrupted, () => false)))
73+
{
74+
return await base.ContinueDialogAsync(dc, cancellationToken).ConfigureAwait(false);
75+
}
76+
else
77+
{
78+
await CheckForTimeoutAsync(dc, cancellationToken).ConfigureAwait(false);
79+
return new DialogTurnResult(DialogTurnStatus.Waiting);
80+
}
7181
}
7282
}
7383
}

packages/Telephony/Actions/BatchRegexInput.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext d
120120
return await PromptUserAsync(dc, cancellationToken).ConfigureAwait(false);
121121
}
122122

123-
//Handle case where we timed out
124-
var activity = dc.Context.Activity;
125-
if (!wasInterrupted && activity.Type != ActivityTypes.Message && activity.Name == ActivityEventNames.ContinueConversation)
126-
{
127-
await SendActivityAsync(dc, cancellationToken).ConfigureAwait(false);
128-
return await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
129-
}
130-
131123
//Get value of termination string from expression
132124
string regexPattern = this.TerminationConditionRegexPattern?.GetValue(dc.State);
133125

@@ -198,6 +190,19 @@ protected override async Task<bool> OnPreBubbleEventAsync(DialogContext dc, Dial
198190
return false;
199191
}
200192

193+
protected async Task CheckForTimeoutAsync(DialogContext dc, CancellationToken cancellationToken)
194+
{
195+
//Handle case where we timed out
196+
var interrupted = dc.State.GetValue<bool>(TurnPath.Interrupted, () => false);
197+
var activity = dc.Context.Activity;
198+
if (!interrupted && activity.Type != ActivityTypes.Message && activity.Name == ActivityEventNames.ContinueConversation)
199+
{
200+
//Send the default message to the user and end the dialog
201+
await SendActivityAsync(dc, cancellationToken).ConfigureAwait(false);
202+
await dc.EndDialogAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
203+
}
204+
}
205+
201206
private async Task<DialogTurnResult> PromptUserAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
202207
{
203208
//Do we already have a value stored? This would happen in the interruption case, a case in which we are looping over ourselves, or maybe we had a fatal error and had to restart the dialog tree
@@ -256,15 +261,11 @@ protected override async Task<bool> OnPreBubbleEventAsync(DialogContext dc, Dial
256261
return new DialogTurnResult(DialogTurnStatus.Complete);
257262
}
258263

259-
private void CreateTimerForConversation(DialogContext dc, string timerId, CancellationToken cancellationToken)
264+
private void CreateTimerForConversation(DialogContext dc, int timeout, string timerId, CancellationToken cancellationToken)
260265
{
261266
BotAdapter adapter = dc.Context.Adapter;
262-
var identity = dc.Context.TurnState.Get<ClaimsIdentity>("BotIdentity");
263-
264267
ConversationReference conversationReference = dc.Context.Activity.GetConversationReference();
265-
var timeoutExpression = this.TimeOutInMilliseconds ?? throw new InvalidOperationException($"Value for TimeOutInMilliseconds property is not defined.");
266-
int timeout = timeoutExpression.GetValue(dc.State);
267-
268+
var identity = dc.Context.TurnState.Get<ClaimsIdentity>("BotIdentity");
268269
var audience = dc.Context.TurnState.Get<string>(BotAdapter.OAuthScopeKey);
269270

270271
//Question remaining to be answered: Will this task get garbage collected? If so, we need to maintain a handle for it.
@@ -287,10 +288,15 @@ await adapter.ContinueConversationAsync(
287288

288289
private async Task InitTimeoutTimerAsync(DialogContext dc, CancellationToken cancellationToken)
289290
{
290-
var timerId = Guid.NewGuid().ToString();
291-
CreateTimerForConversation(dc, timerId, cancellationToken);
292-
await stateMatrix.StartAsync(timerId).ConfigureAwait(false);
293-
dc.State.SetValue("this.TimerId", timerId);
291+
var timeout = this.TimeOutInMilliseconds?.GetValue(dc.State) ?? 0;
292+
293+
if (timeout > 0)
294+
{
295+
var timerId = Guid.NewGuid().ToString();
296+
CreateTimerForConversation(dc, timeout, timerId, cancellationToken);
297+
await stateMatrix.StartAsync(timerId).ConfigureAwait(false);
298+
dc.State.SetValue("this.TimerId", timerId);
299+
}
294300
}
295301
}
296302
}

packages/Telephony/Actions/BatchTerminationCharacterInput.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class BatchTerminationCharacterInput : BatchRegexInput
2222
[JsonProperty("$kind")]
2323
public new const string Kind = "Microsoft.Telephony.BatchTerminationCharacterInput";
2424

25+
private const string _dtmfCharacterRegex = @"^[\d#\*]+$";
2526
private const string _interruptionMaskRegex = @"^[\d]+$";
2627
private string _terminationCharacter;
2728

@@ -59,9 +60,18 @@ public string TerminationCharacter
5960
}
6061

6162
/// <inheritdoc/>
62-
public override Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
63+
public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
6364
{
64-
return base.ContinueDialogAsync(dc, cancellationToken);
65+
if ((dc.Context.Activity.Type == ActivityTypes.Message) &&
66+
(Regex.Match(dc.Context.Activity.Text, _dtmfCharacterRegex).Success || dc.State.GetValue(TurnPath.Interrupted, () => false)))
67+
{
68+
return await base.ContinueDialogAsync(dc, cancellationToken).ConfigureAwait(false);
69+
}
70+
else
71+
{
72+
await CheckForTimeoutAsync(dc, cancellationToken).ConfigureAwait(false);
73+
return new DialogTurnResult(DialogTurnStatus.Waiting);
74+
}
6575
}
6676
}
6777
}

0 commit comments

Comments
 (0)