Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 152d8fe

Browse files
darrenjryanisgrig
andauthored
Exchanging data with skills (#3422)
* exchanging data with skills * Update exchanging-data-with-skills.md Co-authored-by: Ryan Lengel Isgrig <[email protected]>
1 parent 20a5ad7 commit 152d8fe

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
category: Virtual Assistant
3+
subcategory: Handbook
4+
title: Exchanging data with Skills
5+
description: Exchange data to and from Skills using the SkillDialog
6+
order: 12
7+
toc: true
8+
---
9+
10+
# {{ page.title }}
11+
{:.no_toc}
12+
{{ page.description }}
13+
14+
There are situations when it's helpful to pass data between Skills. Information can be provided to a Skill to perform slot-filling therefore limiting the interactions the user has to perform (e.g. share the current location). Additionally, a Skill can interact with the user through responses as usual but also return data back to the underlying caller which can be used for other purposes. For example, a Virtual Assistant could invoke the Calendar, ToDo and Weather Skill to retrieve information and generate a "Your Day Today" card experience bringing together disparate information. These `action` interactions could be silent to the end user with data being returned from each interaction or be interactive depending on your scenario.
15+
16+
Bot Framework Skills provides the capability to pass data to a Skill through the `Value` property on the Activity sent to the Skill through the SkillDialog. Conversely, when a Skill ends a dialog using `EndDialogAsync` an object can be returned which is marshalled back to the caller for use. You can set this Value property in any-way you desire but an example end to end flow is shown below to guide next steps.
17+
18+
> Action invocation is supported by Bot Framework based Bots including Virtual Assistant along with Power Virtual Agents.
19+
20+
## Pre and Post Processing
21+
22+
In order to pass data to a Skill and process data returned from a Skill, one technique is to create a `Pre` and `Post` waterfall step for each Skill you wish to invoke, an example of this is shown below.
23+
24+
```csharp
25+
var skillSteps = new WaterfallStep[]
26+
{
27+
PreSkillStepAsync,
28+
PostSkillStepAsync,
29+
};
30+
31+
AddDialog(new WaterfallDialog("WeatherActionInvoke", skillSteps));
32+
```
33+
34+
You can then invoke this Skill by starting the Waterfall dialog:
35+
36+
```csharp
37+
return await innerDc.BeginDialogAsync("WeatherActionInvoke");
38+
```
39+
40+
## Sending data to a Skill
41+
42+
In the `Pre` processing step you can pass data to the Skill by populating the `Value` property on the Activity with the object you wish to serialize and pass to the Skill. The example below, shows an `Action` within the Skill called `WeatherForecast` being invoked and location information being passed. This activity is then passed to the SkillDialog which will process and send across to the skill.
43+
44+
```csharp
45+
private async Task<DialogTurnResult> PreSkillStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
46+
{
47+
var evt = stepContext.Context.Activity.AsEventActivity();
48+
if (evt != null)
49+
{
50+
LocationInfo location = new LocationInfo();
51+
location.Location = "51.4644018,-2.1177246,14";
52+
53+
var activity = (Activity)Activity.CreateEventActivity();
54+
activity.Name = "WeatherForecast";
55+
activity.Value = location;
56+
57+
// Create the BeginSkillDialogOptions
58+
var skillDialogArgs = new BeginSkillDialogOptions { Activity = activity };
59+
60+
// Start the skillDialog instance with the arguments.
61+
return await stepContext.BeginDialogAsync("WeatherSkill", skillDialogArgs, cancellationToken);
62+
}
63+
64+
return await stepContext.NextAsync();
65+
}
66+
```
67+
68+
## Retrieving data after a Skill interaction
69+
70+
The `Post` processing step will be invoked once the Skill processing has been completed. If data has been returned you will find this within the `stepContext.Result` property.
71+
72+
```csharp
73+
private async Task<DialogTurnResult> PostSkillStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
74+
{
75+
// stepContext.Result has any returning data from a Skill
76+
if (stepContext.Result != null)
77+
{
78+
var returnObject = JsonConvert.SerializeObject(stepContext.Result);
79+
// Perform your processing here
80+
}
81+
82+
return await stepContext.NextAsync();
83+
}
84+
```
85+
86+
## Retrieving data within a Skill
87+
88+
Within your Skill, you then need to handle the Event triggered by the previous steps to retrieve the data and start dialog processing as usual. With the Virtual Assistant and Skill Template this would be within your `RouteStepAsync` method. The example below shows handling the `WeatherForecast` event used above and retrieving data from the `Value` property of an activity. You could then populate the state object with information used by downstream dialogs. An example Action is provided with the Skill Template and you can review the implementation [here](https://github.com/microsoft/botframework-solutions/blob/master/samples/csharp/skill/SkillSample/Dialogs/MainDialog.cs#L245.)
89+
90+
```csharp
91+
case "WeatherForecast":
92+
{
93+
LocationInfo locationData = null;
94+
if (ev.Value is JObject location)
95+
{
96+
locationData = location.ToObject<LocationInfo>();
97+
// Process data here
98+
}
99+
100+
// Start a dialog to process..
101+
return await stepContext.BeginDialogAsync(YOUR_DIALOG.id, options);
102+
```
103+
104+
## Returning data back to the caller from a Skill
105+
106+
Finally, once a Skill has finished processing it can optionally decide to return supporting data to the caller through the `result` parameter on `EndDialogAsync`. You have complete control over the structure of the returned object. In this example the forecast data is returned to the caller which can make use of it as required.
107+
108+
```csharp
109+
return await sc.EndDialogAsync(new WeatherForecastInformation { Forecast = forecast });
110+
```
111+
112+
## Summary
113+
114+
Exchanging data to and from Skills is an optional, but powerful way to build proactive and reactive experiences including those that aggregate data from a variety of Skills to create a more unified experience.

0 commit comments

Comments
 (0)