· 6 years ago · Feb 06, 2019, 04:04 PM
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System;
5using System.Linq;
6using Microsoft.AspNetCore.Builder;
7using Microsoft.AspNetCore.Hosting;
8using Microsoft.Bot.Builder.Integration.AspNet.Core;
9using Microsoft.Bot.Configuration;
10using Microsoft.Bot.Connector.Authentication;
11using Microsoft.Extensions.Configuration;
12using Microsoft.Extensions.DependencyInjection;
13using Microsoft.Bot.Builder;
14using Microsoft.Bot.Builder.Dialogs;
15
16namespace ChatBotTraining_Session1_Waterfall
17{
18 /// <summary>
19 /// The Startup class configures services and the request pipeline.
20 /// </summary>
21 public class Startup
22 {
23 public Startup(IHostingEnvironment env)
24 {
25 var builder = new ConfigurationBuilder()
26 .SetBasePath(env.ContentRootPath)
27 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
28 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
29 .AddEnvironmentVariables();
30
31 Configuration = builder.Build();
32 }
33
34 public IConfiguration Configuration { get; }
35
36 public void ConfigureServices(IServiceCollection services)
37 {
38 //added
39 IStorage storage = new MemoryStorage();
40 ConversationState conversationState = new ConversationState(storage);
41 //^
42
43 services.AddBot<ChatBotTraining_Session1_WaterfallBot>(options =>
44 {
45 var secretKey = Configuration.GetSection("botFileSecret")?.Value;
46
47 // Loads .bot configuration file and adds a singleton that your Bot can access through dependency injection.
48 var botConfig = BotConfiguration.Load(@".\ChatBotTraining_Session1_Waterfall.bot", secretKey);
49 services.AddSingleton(sp => botConfig);
50
51 // Retrieve current endpoint.
52 var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.Name == "development").FirstOrDefault();
53 if (!(service is EndpointService endpointService))
54 {
55 throw new InvalidOperationException($"The .bot file does not contain a development endpoint.");
56 }
57
58 options.CredentialProvider = new SimpleCredentialProvider(endpointService.AppId, endpointService.AppPassword);
59
60 // Catches any errors that occur during a conversation turn and logs them.
61 options.OnTurnError = async (context, exception) =>
62 {
63 await context.SendActivityAsync("Sorry, it looks like something went wrong.");
64 };
65 });
66
67 //added
68 services.AddSingleton<ChatBotAccessor>(sp =>
69 {
70 return new ChatBotAccessor(conversationState)
71 {
72 ConversationDialogStateAccessor = conversationState.CreateProperty<DialogState>(ChatBotAccessor.ChatBotAccessorName)
73 };
74 });
75 }
76
77 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
78 {
79 app.UseDefaultFiles()
80 .UseStaticFiles()
81 .UseBotFramework();
82 }
83 }
84}