Skip to content

Commit 03bc2ac

Browse files
committed
replace code include with inline code
1 parent 3fb2f33 commit 03bc2ac

File tree

1 file changed

+182
-8
lines changed

1 file changed

+182
-8
lines changed

articles/cognitive-services/LUIS/luis-csharp-tutorial-bf-v4.md

Lines changed: 182 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,196 @@ In order to develop the web app bot code, download the code and use on your loca
105105

106106
1. Open the **LuisHelper.cs** file. This is where the user utterance entered into the bot is sent to LUIS. The response from LUIS is returned from the method as a **BookDetails** object. When you create your own bot, you should also create your own object to return the details from LUIS.
107107

108-
[!code-csharp[Console app code that calls a LUIS endpoint](~/samples-luis/documentation-samples/quickstarts/analyze-text/csharp/Program.cs)]
109108

110-
[!code-csharp[Console app code that calls a LUIS endpoint](~/samples-luis/documentation-samples/tutorial-web-app-bot/v4/luis-csharp-bot-johnsmith-src/LuisHelper.cs)]
109+
```csharp
110+
// Copyright (c) Microsoft Corporation. All rights reserved.
111+
// Licensed under the MIT License.
112+
113+
using System;
114+
using System.Linq;
115+
using System.Threading;
116+
using System.Threading.Tasks;
117+
using Microsoft.Bot.Builder;
118+
using Microsoft.Bot.Builder.AI.Luis;
119+
using Microsoft.Extensions.Configuration;
120+
using Microsoft.Extensions.Logging;
121+
122+
namespace Microsoft.BotBuilderSamples
123+
{
124+
public static class LuisHelper
125+
{
126+
public static async Task<BookingDetails> ExecuteLuisQuery(IConfiguration configuration, ILogger logger, ITurnContext turnContext, CancellationToken cancellationToken)
127+
{
128+
var bookingDetails = new BookingDetails();
129+
130+
try
131+
{
132+
// Create the LUIS settings from configuration.
133+
var luisApplication = new LuisApplication(
134+
configuration["LuisAppId"],
135+
configuration["LuisAPIKey"],
136+
"https://" + configuration["LuisAPIHostName"]
137+
);
138+
139+
var recognizer = new LuisRecognizer(luisApplication);
140+
141+
// The actual call to LUIS
142+
var recognizerResult = await recognizer.RecognizeAsync(turnContext, cancellationToken);
143+
144+
var (intent, score) = recognizerResult.GetTopScoringIntent();
145+
if (intent == "Book_flight")
146+
{
147+
// We need to get the result from the LUIS JSON which at every level returns an array.
148+
bookingDetails.Destination = recognizerResult.Entities["To"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
149+
bookingDetails.Origin = recognizerResult.Entities["From"]?.FirstOrDefault()?["Airport"]?.FirstOrDefault()?.FirstOrDefault()?.ToString();
150+
151+
// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
152+
// TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
153+
bookingDetails.TravelDate = recognizerResult.Entities["datetime"]?.FirstOrDefault()?["timex"]?.FirstOrDefault()?.ToString().Split('T')[0];
154+
}
155+
}
156+
catch (Exception e)
157+
{
158+
logger.LogWarning($"LUIS Exception: {e.Message} Check your LUIS configuration.");
159+
}
160+
161+
return bookingDetails;
162+
}
163+
}
164+
}
165+
```
111166

112167
1. Open **BookingDetails.cs** to view how the object abstracts the LUIS information.
113168

114-
[!code-csharp[Console app code that calls a LUIS endpoint](~/samples-luis/documentation-samples/quickstarts/analyze-text/csharp/Program.cs)]
115-
116-
[!code-csharp[Open the BookingDetails.cs file to view how the object abstracts the LUIS information.](~/samples-luis/documentation-samples/tutorial-web-app-bot/v4/luis-csharp-bot-johnsmith-src/BookingDetails.cs "Open the BookingDetails.cs file to view how the object abstracts the LUIS information.")]
169+
```csharp
170+
// Copyright (c) Microsoft Corporation. All rights reserved.
171+
// Licensed under the MIT License.
172+
173+
namespace Microsoft.BotBuilderSamples
174+
{
175+
public class BookingDetails
176+
{
177+
public string Destination { get; set; }
178+
179+
public string Origin { get; set; }
180+
181+
public string TravelDate { get; set; }
182+
}
183+
}
117184
```
118185

119186
1. Open **Dialogs -> BookingDialog.cs** to understand how the BookingDetails object is used to manage the conversation flow. Travel details are asked in steps, then the entire booking is confirmed and finally repeated back to the user.
120187

121-
[!code-csharp[Console app code that calls a LUIS endpoint](~/samples-luis/documentation-samples/quickstarts/analyze-text/csharp/Program.cs)]
122-
123-
[!code-csharp[Open the Dialogs folder, then the BookingDialog.cs file to understand how the BookingDetails object is used to manage the conversation flow.](~/samples-luis/documentation-samples/tutorial-web-app-bot/v4/luis-csharp-bot-johnsmith-src/Dialogs/BookingDialog.cs "Open the Dialogs folder, then the BookingDialog.cs file to understand how the BookingDetails object is used to manage the conversation flow.")]
188+
```csharp
189+
// Copyright (c) Microsoft Corporation. All rights reserved.
190+
// Licensed under the MIT License.
191+
192+
using System.Threading;
193+
using System.Threading.Tasks;
194+
using Microsoft.Bot.Builder;
195+
using Microsoft.Bot.Builder.Dialogs;
196+
using Microsoft.Recognizers.Text.DataTypes.TimexExpression;
197+
198+
namespace Microsoft.BotBuilderSamples.Dialogs
199+
{
200+
public class BookingDialog : CancelAndHelpDialog
201+
{
202+
public BookingDialog()
203+
: base(nameof(BookingDialog))
204+
{
205+
AddDialog(new TextPrompt(nameof(TextPrompt)));
206+
AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
207+
AddDialog(new DateResolverDialog());
208+
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
209+
{
210+
DestinationStepAsync,
211+
OriginStepAsync,
212+
TravelDateStepAsync,
213+
ConfirmStepAsync,
214+
FinalStepAsync,
215+
}));
216+
217+
// The initial child Dialog to run.
218+
InitialDialogId = nameof(WaterfallDialog);
219+
}
220+
221+
private async Task<DialogTurnResult> DestinationStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
222+
{
223+
var bookingDetails = (BookingDetails)stepContext.Options;
224+
225+
if (bookingDetails.Destination == null)
226+
{
227+
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Where would you like to travel to?") }, cancellationToken);
228+
}
229+
else
230+
{
231+
return await stepContext.NextAsync(bookingDetails.Destination, cancellationToken);
232+
}
233+
}
234+
235+
private async Task<DialogTurnResult> OriginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
236+
{
237+
var bookingDetails = (BookingDetails)stepContext.Options;
238+
239+
bookingDetails.Destination = (string)stepContext.Result;
240+
241+
if (bookingDetails.Origin == null)
242+
{
243+
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Where are you traveling from?") }, cancellationToken);
244+
}
245+
else
246+
{
247+
return await stepContext.NextAsync(bookingDetails.Origin, cancellationToken);
248+
}
249+
}
250+
private async Task<DialogTurnResult> TravelDateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
251+
{
252+
var bookingDetails = (BookingDetails)stepContext.Options;
253+
254+
bookingDetails.Origin = (string)stepContext.Result;
255+
256+
if (bookingDetails.TravelDate == null || IsAmbiguous(bookingDetails.TravelDate))
257+
{
258+
return await stepContext.BeginDialogAsync(nameof(DateResolverDialog), bookingDetails.TravelDate, cancellationToken);
259+
}
260+
else
261+
{
262+
return await stepContext.NextAsync(bookingDetails.TravelDate, cancellationToken);
263+
}
264+
}
265+
266+
private async Task<DialogTurnResult> ConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
267+
{
268+
var bookingDetails = (BookingDetails)stepContext.Options;
269+
270+
bookingDetails.TravelDate = (string)stepContext.Result;
271+
272+
var msg = $"Please confirm, I have you traveling to: {bookingDetails.Destination} from: {bookingDetails.Origin} on: {bookingDetails.TravelDate}";
273+
274+
return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text(msg) }, cancellationToken);
275+
}
276+
277+
private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
278+
{
279+
if ((bool)stepContext.Result)
280+
{
281+
var bookingDetails = (BookingDetails)stepContext.Options;
282+
283+
return await stepContext.EndDialogAsync(bookingDetails, cancellationToken);
284+
}
285+
else
286+
{
287+
return await stepContext.EndDialogAsync(null, cancellationToken);
288+
}
289+
}
290+
291+
private static bool IsAmbiguous(string timex)
292+
{
293+
var timexProperty = new TimexProperty(timex);
294+
return !timexProperty.Types.Contains(Constants.TimexTypes.Definite);
295+
}
296+
}
297+
}
124298
```
125299

126300

0 commit comments

Comments
 (0)