Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df1bd30

Browse files
author
Catalin Hatmanu
committed
Added Stemenegement bot for testing purposes
1 parent 8387c04 commit df1bd30

15 files changed

+779
-1
lines changed

‎BinaryFog.Bot.Builder.LiteDb.sln‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ VisualStudioVersion = 16.0.29009.5
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BinaryFog.Bot.Builder.LiteDb", "BinaryFog.Bot.Builder.LiteDb\BinaryFog.Bot.Builder.LiteDb.csproj", "{14E032E9-4708-464D-8F86-4E671FB78A7F}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinaryFog.Bot.Builder.LiteDb.Tests", "BinaryFog.Bot.Builder.LiteDb.Tests\BinaryFog.Bot.Builder.LiteDb.Tests.csproj", "{B2586F6E-A36C-45BA-AF7D-9EF29B60CFC1}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BinaryFog.Bot.Builder.LiteDb.Tests", "BinaryFog.Bot.Builder.LiteDb.Tests\BinaryFog.Bot.Builder.LiteDb.Tests.csproj", "{B2586F6E-A36C-45BA-AF7D-9EF29B60CFC1}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StateMangementBot", "StateManagementBot\StateMangementBot.csproj", "{D28CDA83-4764-4C3B-9D40-0F13F6989FB5}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{B2586F6E-A36C-45BA-AF7D-9EF29B60CFC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{B2586F6E-A36C-45BA-AF7D-9EF29B60CFC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{B2586F6E-A36C-45BA-AF7D-9EF29B60CFC1}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{D28CDA83-4764-4C3B-9D40-0F13F6989FB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{D28CDA83-4764-4C3B-9D40-0F13F6989FB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{D28CDA83-4764-4C3B-9D40-0F13F6989FB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{D28CDA83-4764-4C3B-9D40-0F13F6989FB5}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
5+
using Microsoft.Bot.Connector.Authentication;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Microsoft.BotBuilderSamples
9+
{
10+
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
11+
{
12+
public AdapterWithErrorHandler(ICredentialProvider credentialProvider, ILogger<BotFrameworkHttpAdapter> logger)
13+
: base(credentialProvider)
14+
{
15+
// Enable logging at the adapter level using OnTurnError.
16+
OnTurnError = async (turnContext, exception) =>
17+
{
18+
logger.LogError($"Exception caught : {exception}");
19+
await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");
20+
21+
// Depending on the application and how the state is being used you may also decide to
22+
// delete either the ConversationState or UserState or both when you get a leaked global exception.
23+
// This can avoid a bot getting "stuck" in an infinite loop prompting the user.
24+
};
25+
}
26+
}
27+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Bot.Connector.Authentication;
5+
using Microsoft.Extensions.Configuration;
6+
7+
namespace Microsoft.BotBuilderSamples
8+
{
9+
public class ConfigurationCredentialProvider : SimpleCredentialProvider
10+
{
11+
public ConfigurationCredentialProvider(IConfiguration configuration)
12+
: base(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"])
13+
{
14+
}
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Bot.Builder;
7+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
8+
9+
namespace Microsoft.BotBuilderSamples
10+
{
11+
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
12+
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
13+
// achieved by specifying a more specific type for the bot constructor argument.
14+
[Route("api/messages")]
15+
[ApiController]
16+
public class BotController : ControllerBase
17+
{
18+
private readonly IBotFrameworkHttpAdapter _adapter;
19+
private readonly IBot _bot;
20+
21+
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
22+
{
23+
_adapter = adapter;
24+
_bot = bot;
25+
}
26+
27+
[HttpPost]
28+
public async Task PostAsync()
29+
{
30+
// Delegate the processing of the HTTP POST to the adapter.
31+
// The adapter will invoke the bot.
32+
await _adapter.ProcessAsync(Request, Response, _bot);
33+
}
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.BotBuilderSamples
5+
{
6+
// Defines a state property used to track conversation data.
7+
public class ConversationData
8+
{
9+
// The time-stamp of the most recent incoming message.
10+
public string Timestamp { get; set; }
11+
12+
// The ID of the user's channel.
13+
public string ChannelId { get; set; }
14+
15+
// Track whether we have already asked the user's name
16+
public bool PromptedUserForName { get; set; } = false;
17+
}
18+
}

‎StateManagementBot/Program.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.AspNetCore;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace Microsoft.BotBuilderSamples
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
CreateWebHostBuilder(args).Build().Run();
15+
}
16+
17+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
18+
WebHost.CreateDefaultBuilder(args)
19+
.ConfigureLogging((logging) =>
20+
{
21+
logging.AddDebug();
22+
logging.AddConsole();
23+
})
24+
.UseStartup<Startup>();
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:3978/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"StateBot": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:3978/"
25+
}
26+
}
27+
}

‎StateManagementBot/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Save user and conversation data
2+
3+
This sample demonstrates how to save user and conversation data in an ASP.Net Core 2 bot.
4+
The bot maintains conversation state to track and direct the conversation and ask the user questions.
5+
The bot maintains user state to track the user's answers.
6+
7+
## Further reading
8+
9+
- [Azure Bot Service](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
10+
- [Bot Storage](https://docs.microsoft.com/azure/bot-service/dotnet/bot-builder-dotnet-state?view=azure-bot-service-3.0&viewFallbackFrom=azure-bot-service-4.0)

‎StateManagementBot/Startup.cs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Bot.Builder;
8+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
9+
using Microsoft.Bot.Connector.Authentication;
10+
using Microsoft.Extensions.DependencyInjection;
11+
12+
namespace Microsoft.BotBuilderSamples
13+
{
14+
public class Startup
15+
{
16+
// This method gets called by the runtime. Use this method to add services to the container.
17+
public void ConfigureServices(IServiceCollection services)
18+
{
19+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
20+
21+
// Create the credential provider to be used with the Bot Framework Adapter.
22+
services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
23+
24+
// Create the Bot Framework Adapter with error handling enabled.
25+
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
26+
27+
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
28+
services.AddSingleton<IStorage, MemoryStorage>();
29+
30+
// Create the User state.
31+
services.AddSingleton<UserState>();
32+
33+
// Create the Conversation state.
34+
services.AddSingleton<ConversationState>();
35+
36+
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
37+
services.AddTransient<IBot, StateManagementBot>();
38+
}
39+
40+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
41+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
42+
{
43+
if (env.IsDevelopment())
44+
{
45+
app.UseDeveloperExceptionPage();
46+
}
47+
else
48+
{
49+
app.UseHsts();
50+
}
51+
52+
app.UseDefaultFiles();
53+
app.UseStaticFiles();
54+
55+
//app.UseHttpsRedirection();
56+
app.UseMvc();
57+
}
58+
}
59+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /