This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 529
/
SwitchSkillDialog.cs
83 lines (70 loc) · 3.67 KB
/
SwitchSkillDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
namespace Microsoft.Bot.Solutions.Skills.Dialogs
{
public class SwitchSkillDialog : ComponentDialog
{
private static string _confirmPromptId = "ConfirmSkillSwitch";
private IStatePropertyAccessor<string> _skillIdAccessor;
private IStatePropertyAccessor<Activity> _lastActivityAccessor;
public SwitchSkillDialog(ConversationState conversationState)
: base(nameof(SwitchSkillDialog))
{
_skillIdAccessor = conversationState.CreateProperty<string>(Properties.SkillId);
_lastActivityAccessor = conversationState.CreateProperty<Activity>(Properties.LastActivity);
var intentSwitch = new WaterfallStep[]
{
PromptToSwitchAsync,
EndAsync,
};
AddDialog(new WaterfallDialog(nameof(SwitchSkillDialog), intentSwitch));
AddDialog(new ConfirmPrompt(_confirmPromptId));
}
// Runs when this dialog ends. Handles result of prompt to switch skills or resume waiting dialog.
protected override async Task<DialogTurnResult> EndComponentAsync(DialogContext outerDc, object result, CancellationToken cancellationToken)
{
var skillId = await _skillIdAccessor.GetAsync(outerDc.Context, () => null).ConfigureAwait(false);
var lastActivity = await _lastActivityAccessor.GetAsync(outerDc.Context, () => null).ConfigureAwait(false);
outerDc.Context.Activity.Text = lastActivity.Text;
// Ends this dialog.
await outerDc.EndDialogAsync().ConfigureAwait(false);
if ((bool)result)
{
// If user decided to switch, replace current skill dialog with new skill dialog.
var skillDialogArgs = new SkillDialogArgs { SkillId = skillId };
// Start the skill dialog.
return await outerDc.ReplaceDialogAsync(skillId, skillDialogArgs).ConfigureAwait(false);
}
else
{
// Otherwise, continue the waiting skill dialog with the user's previous utterance.
return await outerDc.ContinueDialogAsync().ConfigureAwait(false);
}
}
// Prompts user to switch to a new skill.
private async Task<DialogTurnResult> PromptToSwitchAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var options = stepContext.Options as SwitchSkillDialogOptions ?? throw new ArgumentException($"You must provide options of type {typeof(SwitchSkillDialogOptions).FullName}.");
await _skillIdAccessor.SetAsync(stepContext.Context, options.Skill.Id).ConfigureAwait(false);
await _lastActivityAccessor.SetAsync(stepContext.Context, stepContext.Context.Activity).ConfigureAwait(false);
return await stepContext.PromptAsync(_confirmPromptId, options).ConfigureAwait(false);
}
// Ends this dialog, returning the prompt result.
private async Task<DialogTurnResult> EndAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
bool result = (bool)stepContext.Result;
return await stepContext.EndDialogAsync(result: result).ConfigureAwait(false);
}
private class Properties
{
public const string SkillId = "skillSwitchValue";
public const string LastActivity = "skillSwitchActivity";
}
}
}