in SDKV4-Samples/dotnet_core/ComplexDialogBot/ComplexDialogBot.cs [100:155]
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
// Handle Message activity type, which is the main activity type for shown within a conversational interface
// Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
// see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
if (turnContext.Activity.Type == ActivityTypes.Message)
{
// Run the DialogSet - let the framework identify the current state of the dialog from
// the dialog stack and figure out what (if any) is the active dialog.
DialogContext dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
DialogTurnResult results = await dialogContext.ContinueDialogAsync(cancellationToken);
switch (results.Status)
{
case DialogTurnStatus.Cancelled:
case DialogTurnStatus.Empty:
// If there is no active dialog, we should clear the user info and start a new dialog.
await _accessors.UserProfileAccessor.SetAsync(turnContext, new UserProfile(), cancellationToken);
await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
await dialogContext.BeginDialogAsync(TopLevelDialog, null, cancellationToken);
break;
case DialogTurnStatus.Complete:
// If we just finished the dialog, capture and display the results.
UserProfile userInfo = results.Result as UserProfile;
string status = "You are signed up to review "
+ (userInfo.CompaniesToReview.Count is 0 ? "no companies" : string.Join(" and ", userInfo.CompaniesToReview))
+ ".";
await turnContext.SendActivityAsync(status);
await _accessors.UserProfileAccessor.SetAsync(turnContext, userInfo, cancellationToken);
await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
break;
case DialogTurnStatus.Waiting:
// If there is an active dialog, we don't need to do anything here.
break;
}
await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
}
// Processes ConversationUpdate Activities to welcome the user.
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
{
if (turnContext.Activity.MembersAdded != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
}
else
{
await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken: cancellationToken);
}
}