|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Bot.Builder; |
| 9 | +using Microsoft.Bot.Schema; |
| 10 | + |
| 11 | +namespace Microsoft.BotBuilderSamples |
| 12 | +{ |
| 13 | + public class StateManagementBot : ActivityHandler |
| 14 | + { |
| 15 | + private BotState _conversationState; |
| 16 | + private BotState _userState; |
| 17 | + |
| 18 | + public StateManagementBot(ConversationState conversationState, UserState userState) |
| 19 | + { |
| 20 | + _conversationState = conversationState; |
| 21 | + _userState = userState; |
| 22 | + } |
| 23 | + |
| 24 | + public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) |
| 25 | + { |
| 26 | + await base.OnTurnAsync(turnContext, cancellationToken); |
| 27 | + |
| 28 | + // Save any state changes that might have occured during the turn. |
| 29 | + await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 30 | + await _userState.SaveChangesAsync(turnContext, false, cancellationToken); |
| 31 | + } |
| 32 | + |
| 33 | + protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 34 | + { |
| 35 | + await turnContext.SendActivityAsync("Welcome to State Bot Sample. Type anything to get started."); |
| 36 | + } |
| 37 | + |
| 38 | + protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 39 | + { |
| 40 | + // Get the state properties from the turn context. |
| 41 | + |
| 42 | + var conversationStateAccessors = _conversationState.CreateProperty<ConversationData>(nameof(ConversationData)); |
| 43 | + var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData()); |
| 44 | + |
| 45 | + var userStateAccessors = _userState.CreateProperty<UserProfile>(nameof(UserProfile)); |
| 46 | + var userProfile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile()); |
| 47 | + |
| 48 | + if (string.IsNullOrEmpty(userProfile.Name)) |
| 49 | + { |
| 50 | + // First time around this is set to false, so we will prompt user for name. |
| 51 | + if (conversationData.PromptedUserForName) |
| 52 | + { |
| 53 | + // Set the name to what the user provided. |
| 54 | + userProfile.Name = turnContext.Activity.Text?.Trim(); |
| 55 | + |
| 56 | + // Acknowledge that we got their name. |
| 57 | + await turnContext.SendActivityAsync($"Thanks {userProfile.Name}. To see conversation data, type anything."); |
| 58 | + |
| 59 | + // Reset the flag to allow the bot to go though the cycle again. |
| 60 | + conversationData.PromptedUserForName = false; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + // Prompt the user for their name. |
| 65 | + await turnContext.SendActivityAsync($"What is your name?"); |
| 66 | + |
| 67 | + // Set the flag to true, so we don't prompt in the next turn. |
| 68 | + conversationData.PromptedUserForName = true; |
| 69 | + } |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + // Add message details to the conversation data. |
| 74 | + // Convert saved Timestamp to local DateTimeOffset, then to string for display. |
| 75 | + var messageTimeOffset = (DateTimeOffset) turnContext.Activity.Timestamp; |
| 76 | + var localMessageTime = messageTimeOffset.ToLocalTime(); |
| 77 | + conversationData.Timestamp = localMessageTime.ToString(); |
| 78 | + conversationData.ChannelId = turnContext.Activity.ChannelId.ToString(); |
| 79 | + |
| 80 | + // Display state data. |
| 81 | + await turnContext.SendActivityAsync($"{userProfile.Name} sent: {turnContext.Activity.Text}"); |
| 82 | + await turnContext.SendActivityAsync($"Message received at: {conversationData.Timestamp}"); |
| 83 | + await turnContext.SendActivityAsync($"Message received from: {conversationData.ChannelId}"); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
0 commit comments