Skip to content

Commit

Permalink
Search root dialog instead of hard code to main.dialog (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
boydc2014 authored Mar 24, 2020
1 parent 8d594d5 commit 1906ccf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions BotProject/Templates/CSharp/ComposerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public class ComposerBot : ActivityHandler
private readonly IStatePropertyAccessor<DialogState> dialogState;
private readonly string rootDialogFile;

public ComposerBot(ConversationState conversationState, UserState userState, ResourceExplorer resourceExplorer, BotFrameworkClient skillClient, SkillConversationIdFactoryBase conversationIdFactory)
public ComposerBot(ConversationState conversationState, UserState userState, ResourceExplorer resourceExplorer, BotFrameworkClient skillClient, SkillConversationIdFactoryBase conversationIdFactory, string rootDialog)
{
HostContext.Current.Set(skillClient);
HostContext.Current.Set(conversationIdFactory);
this.conversationState = conversationState;
this.userState = userState;
this.dialogState = conversationState.CreateProperty<DialogState>("DialogState");
this.resourceExplorer = resourceExplorer;
this.rootDialogFile = "Main.dialog";
this.rootDialogFile = rootDialog;
LoadRootDialogAsync();
}

Expand Down
29 changes: 26 additions & 3 deletions BotProject/Templates/CSharp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.IO;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -77,10 +78,11 @@ public void ConfigureServices(IServiceCollection services)
var userState = new UserState(storage);
var conversationState = new ConversationState(storage);

var botFile = Configuration.GetSection("bot").Get<string>();
var botDir = Configuration.GetSection("bot").Get<string>();

// manage all bot resources
var resourceExplorer = new ResourceExplorer().AddFolder(botFile);
var resourceExplorer = new ResourceExplorer().AddFolder(botDir);
var rootDialog = GetRootDialog(botDir);

services.AddSingleton(userState);
services.AddSingleton(conversationState);
Expand Down Expand Up @@ -113,7 +115,14 @@ public void ConfigureServices(IServiceCollection services)
return adapter;
});

services.AddSingleton<IBot, ComposerBot>();
services.AddSingleton<IBot>(s =>
new ComposerBot(
s.GetService<ConversationState>(),
s.GetService<UserState>(),
s.GetService<ResourceExplorer>(),
s.GetService<BotFrameworkClient>(),
s.GetService<SkillConversationIdFactoryBase>(),
rootDialog));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -129,5 +138,19 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
endpoints.MapControllers();
});
}

private string GetRootDialog(string folderPath)
{
var dir = new DirectoryInfo(folderPath);
foreach (var f in dir.GetFiles())
{
if (f.Extension == ".dialog")
{
return f.Name;
}
}

throw new Exception($"Can't locate root dialog in {dir.FullName}");
}
}
}

0 comments on commit 1906ccf

Please sign in to comment.