in SDKV4-Samples/dotnet_core/StateBot/StateBot.cs [53:106]
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
// Get the state properties from the turn context.
UserProfile userProfile =
await _accessors.UserProfileAccessor.GetAsync(turnContext, () => new UserProfile());
ConversationData conversationData =
await _accessors.ConversationDataAccessor.GetAsync(turnContext, () => new ConversationData());
if (string.IsNullOrEmpty(userProfile.Name))
{
// First time around this is set to false, so we will prompt user for name.
if (conversationData.PromptedUserForName)
{
// Set the name to what the user provided.
userProfile.Name = turnContext.Activity.Text?.Trim();
// Acknowledge that we got their name.
await turnContext.SendActivityAsync($"Thanks {userProfile.Name}.");
// Reset the flag to allow the bot to go though the cycle again.
conversationData.PromptedUserForName = false;
}
else
{
// Prompt the user for their name.
await turnContext.SendActivityAsync($"What is your name?");
// Set the flag to true, so we don't prompt in the next turn.
conversationData.PromptedUserForName = true;
}
// Save user state and save changes.
await _accessors.UserProfileAccessor.SetAsync(turnContext, userProfile);
await _accessors.UserState.SaveChangesAsync(turnContext);
}
else
{
// Add message details to the conversation data.
conversationData.Timestamp = turnContext.Activity.Timestamp.ToString();
conversationData.ChannelId = turnContext.Activity.ChannelId.ToString();
// Display state data.
await turnContext.SendActivityAsync($"{userProfile.Name} sent: {turnContext.Activity.Text}");
await turnContext.SendActivityAsync($"Message received at: {conversationData.Timestamp}");
await turnContext.SendActivityAsync($"Message received from: {conversationData.ChannelId}");
}
// Update conversation state and save changes.
await _accessors.ConversationDataAccessor.SetAsync(turnContext, conversationData);
await _accessors.ConversationState.SaveChangesAsync(turnContext);
}
}