Skip to content

Commit dc05852

Browse files
committed
update
1 parent 1c20171 commit dc05852

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

articles/cognitive-services/Speech-Service/how-to-custom-speech-commands-fulfill-sdk.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.date: 03/12/2020
1313
ms.author: donkim
1414
---
1515

16-
# Fulfill commands from a client with the Speech SDK (Preview)
16+
# Fulfill commands from a client with the Speech SDK
1717

1818
To complete tasks using a Custom Commands application you can send custom payloads to a connected client device.
1919

@@ -28,17 +28,17 @@ In this article, you'll:
2828
- An Azure subscription key for Speech service
2929
- [Get one for free](get-started.md) or create it on the [Azure portal](https://portal.azure.com)
3030
- A previously created Custom Commands app
31-
- [Quickstart: Create a Custom Command with Parameters (Preview)](./quickstart-custom-speech-commands-create-parameters.md)
31+
- [Quickstart: Create a Custom Command with Parameters](./quickstart-custom-speech-commands-create-parameters.md)
3232
- A Speech SDK enabled client application
33-
- [Quickstart: Connect to a Custom Command application with the Speech SDK (Preview)](./quickstart-custom-speech-commands-speech-sdk.md)
33+
- [Quickstart: Connect to a Custom Command application with the Speech SDK](./quickstart-custom-speech-commands-speech-sdk.md)
3434

3535
## Optional: Get started fast
3636

3737
This article describes, step by step, how to make a client application to talk to your Custom Commands application. If you prefer to dive right in, the complete, ready-to-compile source code used in this article is available in the [Speech SDK Samples](https://aka.ms/csspeech/samples).
3838

3939
## Fulfill with JSON payload
4040

41-
1. Open your previously created Custom Commands application from the [Speech Studio](https://speech.microsoft.com/)
41+
1. Open the Custom Commands application you previously created from [Quickstarts: Create a custom command with parameters](./quickstart-custom-speech-commands-create-parameters.md)
4242
1. Check the **Completion Rules** section to make sure you have the previously created rule that responds back to the user
4343
1. To send a payload directly to the client, create a new rule with a Send Activity action
4444

@@ -51,9 +51,7 @@ This article describes, step by step, how to make a client application to talk t
5151
| Conditions | Required Parameter - `OnOff` and `SubjectDevice` | Conditions that determine when the rule can run |
5252
| Actions | `SendActivity` (see below) | The action to take when the rule condition is true |
5353

54-
> [!div class="mx-imgBorder"]
55-
> ![Send Activity payload](media/custom-speech-commands/fulfill-sdk-send-activity-action.png)
56-
54+
1. Copy the JSON below to **Activity content**
5755
```json
5856
{
5957
"type": "event",
@@ -62,12 +60,14 @@ This article describes, step by step, how to make a client application to talk t
6260
"device": "{SubjectDevice}"
6361
}
6462
```
63+
> [!div class="mx-imgBorder"]
64+
> ![Send Activity payload](media/custom-speech-commands/fulfill-sdk-send-activity-action.png)
6565
6666
## Create visuals for device on or off state
6767

68-
In [Quickstart: Connect to a Custom Command application with the Speech SDK (Preview)](./quickstart-custom-speech-commands-speech-sdk.md) you created a Speech SDK client application that handled commands such as `turn on the tv`, `turn off the fan`. Now add some visuals so you can see the result of those commands.
68+
In [Quickstart: Connect to a Custom Command application with the Speech SDK](./quickstart-custom-speech-commands-speech-sdk.md), you created a Speech SDK client application that handled commands such as `turn on the tv`, `turn off the fan`. With some visuals added, you can see the result of those commands.
6969

70-
Add labeled boxes with text indicating **On** or **Off** using the following XML added to `MainPage.xaml.cs`
70+
Add labeled boxes with text indicating **On** or **Off** using the following XML added to `MainPage.xaml`
7171

7272
```xml
7373
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="20">
@@ -87,36 +87,57 @@ Add labeled boxes with text indicating **On** or **Off** using the following XML
8787
```
8888

8989
## Handle customizable payload
90+
### Add reference libraries
9091

91-
Now that you've created a JSON payload, you can add a reference to the [JSON.NET](https://www.newtonsoft.com/json) library to handle deserialization.
92+
Since you've created a JSON payload, you need to add a reference to the [JSON.NET](https://www.newtonsoft.com/json) library to handle deserialization.
93+
- Right-client your solution.
94+
- Choose **Manage NuGet Packages for Solution**, Select **Install**
95+
- Search for **Newtonsoft.json** in the update list, Update **Microsoft.NETCore.UniversalWindowsPlatform** to newest version
9296

9397
> [!div class="mx-imgBorder"]
9498
> ![Send Activity payload](media/custom-speech-commands/fulfill-sdk-json-nuget.png)
9599
96-
In `InitializeDialogServiceConnector` add the following to your `ActivityReceived` event handler. The additional code will extract the payload from the activity and change the visual state of the tv or fan accordingly.
100+
In `MainPage.xaml.cs', add
101+
- `using Newtonsoft.Json;`
102+
- `using Windows.ApplicationModel.Core;`
103+
104+
### Handle received payload
105+
106+
In `InitializeDialogServiceConnector`, replace the `ActivityReceived` event handler with following code. The modified `ActivityReceived` event handler will extract the payload from the activity and change the visual state of the tv or fan accordingly.
97107

98108
```C#
99109
connector.ActivityReceived += async (sender, activityReceivedEventArgs) =>
100110
{
101111
NotifyUser($"Activity received, hasAudio={activityReceivedEventArgs.HasAudio} activity={activityReceivedEventArgs.Activity}");
102112

103113
dynamic activity = JsonConvert.DeserializeObject(activityReceivedEventArgs.Activity);
114+
var name = activity?.name != null ? activity.name.ToString() : string.Empty;
104115

105-
if(activity?.name == "SetDeviceState")
116+
if (name.Equals("UpdateDeviceState"))
106117
{
107-
var state = activity?.state;
108-
var device = activity?.device;
109-
switch(device)
118+
Debug.WriteLine("Here");
119+
var state = activity?.device != null ? activity.state.ToString() : string.Empty;
120+
var device = activity?.device != null ? activity.device.ToString() : string.Empty;
121+
122+
if (state.Equals("on") || state.Equals("off"))
110123
{
111-
case "tv":
112-
State_TV.Text = state;
113-
break;
114-
case "fan":
115-
State_Fan.Text = state;
116-
break;
117-
default:
118-
NotifyUser($"Received request to set unsupported device {device} to {state}");
119-
break;
124+
switch (device)
125+
{
126+
case "tv":
127+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
128+
CoreDispatcherPriority.Normal, () => { State_TV.Text = state; });
129+
break;
130+
case "fan":
131+
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
132+
CoreDispatcherPriority.Normal, () => { State_Fan.Text = state; });
133+
break;
134+
default:
135+
NotifyUser($"Received request to set unsupported device {device} to {state}");
136+
break;
137+
}
138+
}
139+
else {
140+
NotifyUser($"Received request to set unsupported state {state}");
120141
}
121142
}
122143

@@ -134,6 +155,8 @@ connector.ActivityReceived += async (sender, activityReceivedEventArgs) =>
134155
1. Select the Talk button
135156
1. Say `turn on the tv`
136157
1. The visual state of the tv should change to "On"
158+
> [!div class="mx-imgBorder"]
159+
> ![Send Activity payload](media/custom-speech-commands/fulfill-sdk-uwp-turnontv.png)
137160
138161
## Next steps
139162

3.18 KB
Loading
-7.67 KB
Loading
33.6 KB
Loading

0 commit comments

Comments
 (0)