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 528
/
SkillDialog.cs
171 lines (143 loc) · 8.13 KB
/
SkillDialog.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Integration.AspNet.Core.Skills;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Solutions.Skills.Models;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Bot.Solutions.Skills
{
/// <summary>
/// A sample dialog that can wrap remote calls to a skill.
/// </summary>
/// <remarks>
/// The options parameter in <see cref="BeginDialogAsync"/> must be a <see cref="SkillDialogArgs"/> instance
/// with the initial parameters for the dialog.
/// </remarks>
public class SkillDialog : Dialog
{
private readonly string _botId;
private readonly ConversationState _conversationState;
private readonly SkillHttpClient _skillClient;
private readonly EnhancedBotFrameworkSkill _skill;
private readonly Uri _skillHostEndpoint;
public SkillDialog(ConversationState conversationState, SkillHttpClient skillClient, EnhancedBotFrameworkSkill skill, IConfiguration configuration, Uri skillHostEndpoint)
: base(skill?.Id)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
_botId = configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value;
if (string.IsNullOrWhiteSpace(_botId))
{
throw new ArgumentException($"{MicrosoftAppCredentials.MicrosoftAppIdKey} is not in configuration");
}
_skillHostEndpoint = skillHostEndpoint;
_skillClient = skillClient ?? throw new ArgumentNullException(nameof(skillClient));
_skill = skill ?? throw new ArgumentNullException(nameof(skill));
_conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
}
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default)
{
if (!(options is SkillDialogArgs dialogArgs))
{
throw new ArgumentNullException(nameof(options), $"Unable to cast {nameof(options)} to {nameof(SkillDialogArgs)}");
}
var skillId = dialogArgs.SkillId;
await dc.Context.TraceActivityAsync($"{GetType().Name}.BeginDialogAsync()", label: $"Using activity of type: {dialogArgs.ActivityType}", cancellationToken: cancellationToken).ConfigureAwait(false);
Activity skillActivity;
switch (dialogArgs.ActivityType)
{
case ActivityTypes.Event:
var eventActivity = Activity.CreateEventActivity();
eventActivity.Name = dialogArgs.Name;
eventActivity.ApplyConversationReference(dc.Context.Activity.GetConversationReference(), true);
skillActivity = (Activity)eventActivity;
break;
case ActivityTypes.Message:
var messageActivity = Activity.CreateMessageActivity();
messageActivity.Text = dc.Context.Activity.Text;
skillActivity = (Activity)messageActivity;
break;
default:
throw new ArgumentException($"Invalid activity type in {dialogArgs.ActivityType} in {nameof(SkillDialogArgs)}");
}
ApplyParentActivityProperties(dc.Context, skillActivity, dialogArgs);
return await this.SendToSkillAsync(dc, skillActivity, cancellationToken).ConfigureAwait(false);
}
public override async Task<DialogTurnResult> ContinueDialogAsync(DialogContext dc, CancellationToken cancellationToken = default)
{
await dc.Context.TraceActivityAsync($"{GetType().Name}.ContinueDialogAsync()", label: $"ActivityType: {dc.Context.Activity.Type}", cancellationToken: cancellationToken).ConfigureAwait(false);
if (dc.Context.Activity.Type == ActivityTypes.EndOfConversation)
{
await dc.Context.TraceActivityAsync($"{GetType().Name}.ContinueDialogAsync()", label: $"Got EndOfConversation", cancellationToken: cancellationToken).ConfigureAwait(false);
return await dc.EndDialogAsync(dc.Context.Activity.Value, cancellationToken).ConfigureAwait(false);
}
// Just forward to the remote skill
return await SendToSkillAsync(dc, dc.Context.Activity, cancellationToken).ConfigureAwait(false);
}
public override Task<DialogTurnResult> ResumeDialogAsync(DialogContext dc, DialogReason reason, object result = null, CancellationToken cancellationToken = default)
{
return Task.FromResult(EndOfTurn);
}
public override async Task EndDialogAsync(ITurnContext turnContext, DialogInstance instance, DialogReason reason, CancellationToken cancellationToken = default)
{
if (reason == DialogReason.CancelCalled || reason == DialogReason.ReplaceCalled)
{
await turnContext.TraceActivityAsync($"{GetType().Name}.EndDialogAsync()", label: $"ActivityType: {turnContext.Activity.Type}", cancellationToken: cancellationToken).ConfigureAwait(false);
var activity = (Activity)Activity.CreateEndOfConversationActivity();
ApplyParentActivityProperties(turnContext, activity, null);
await SendToSkillAsync(null, (Activity)activity, cancellationToken).ConfigureAwait(false);
}
await base.EndDialogAsync(turnContext, instance, reason, cancellationToken).ConfigureAwait(false);
}
private static void ApplyParentActivityProperties(ITurnContext turnContext, Activity skillActivity, SkillDialogArgs dialogArgs)
{
// Apply conversation reference and common properties from incoming activity before sending.
skillActivity.ApplyConversationReference(turnContext.Activity.GetConversationReference(), true);
skillActivity.ChannelData = turnContext.Activity.ChannelData;
skillActivity.Properties = turnContext.Activity.Properties;
if (dialogArgs != null)
{
skillActivity.Value = dialogArgs?.Value;
}
}
private async Task<DialogTurnResult> SendToSkillAsync(DialogContext dc, Activity activity, CancellationToken cancellationToken)
{
// add checking on skill configuration before sending over to skill
// because we can't perform the check in constructor which will fail startup of the app
if (string.IsNullOrWhiteSpace(_skill.AppId))
{
throw new ArgumentNullException(nameof(_skill.AppId));
}
if (string.IsNullOrWhiteSpace(_skill.SkillEndpoint.ToString()))
{
throw new ArgumentNullException(nameof(_skill.SkillEndpoint));
}
if (string.IsNullOrWhiteSpace(_skillHostEndpoint.ToString()))
{
throw new ArgumentNullException(nameof(_skillHostEndpoint));
}
if (dc != null)
{
// Always save state before forwarding
// (the dialog stack won't get updated with the skillDialog and things won't work if you don't)
await _conversationState.SaveChangesAsync(dc.Context, true, cancellationToken).ConfigureAwait(false);
}
var response = await _skillClient.PostActivityAsync(_botId, _skill, _skillHostEndpoint, activity, cancellationToken).ConfigureAwait(false);
if (!(response.Status >= 200 && response.Status <= 299))
{
throw new HttpRequestException($"Error invoking the skill id: \"{_skill.Id}\" at \"{_skill.SkillEndpoint}\" (status is {response.Status}). \r\n {response.Body}");
}
return EndOfTurn;
}
}
}