Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TestBot] Configure an endpoint for each bot #1946

Merged
merged 4 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FunctionalTests/ExportedTemplate/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"resourcesLocation": "[parameters('newAppServicePlanLocation')]",
"webAppName": "[if(empty(parameters('newWebAppName')), parameters('botId'), parameters('newWebAppName'))]",
"siteHost": "[concat(variables('webAppName'), '.azurewebsites.net')]",
"botEndpoint": "[concat('https://', variables('siteHost'), '/api/messages')]"
"botEndpoint": "[concat('https://', variables('siteHost'), '/api/mybot')]"
},
"resources": [
{
Expand Down
15 changes: 10 additions & 5 deletions tests/Microsoft.Bot.Builder.TestBot/Controllers/BotController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
Expand All @@ -11,24 +12,28 @@ namespace Microsoft.BotBuilderSamples
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
// achieved by specifying a more specific type for the bot constructor argument.
[Route("api/messages")]
[Route("api")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;
private IBot _bot;
private readonly Func<string, IBot> _service;

public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
public BotController(IBotFrameworkHttpAdapter adapter, Func<string, IBot> service)
{
_adapter = adapter;
_bot = bot;
_service = service;
}

[Route("{*botname}")]
[HttpPost]
public async Task PostAsync()
public async Task PostAsync(string botname)
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
_bot = _service(botname) ?? throw new Exception($"The endpoint '{botname}' is not associated with a bot.");

await _adapter.ProcessAsync(Request, Response, _bot);
}
}
Expand Down
51 changes: 23 additions & 28 deletions tests/Microsoft.Bot.Builder.TestBot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class Startup
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
GetEnvironmentVariables();

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

// Create the credential provider to be used with the Bot Framework Adapter.
Expand Down Expand Up @@ -51,24 +49,28 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<MainDialog>();

// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
switch (chosenBot)
{
case "EchoBot":
services.AddTransient<IBot, MyBot>();
break;

case "DialogBot":
services.AddTransient<IBot, DialogBot<MainDialog>>();
break;

default:
services.AddTransient<IBot, DialogAndWelcomeBot<MainDialog>>();
break;
}
services.AddScoped<MyBot>();
services.AddScoped<DialogBot<MainDialog>>();
services.AddScoped<DialogAndWelcomeBot<MainDialog>>();

// We can also run the inspection at a different endpoint. Just uncomment these lines.
// services.AddSingleton<DebugAdapter>();
// services.AddTransient<DebugBot>();
services.AddTransient<Func<string, IBot>>(serviceProvider => key =>
{
switch (key)
{
case "mybot":
return serviceProvider.GetService<MyBot>();
case "dialogbot":
return serviceProvider.GetService<DialogBot<MainDialog>>();
case "messages":
case "dialogandwelcomebot":
return serviceProvider.GetService<DialogAndWelcomeBot<MainDialog>>();
default:
return null;
}
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -87,19 +89,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseStaticFiles();

// app.UseHttpsRedirection();
app.UseMvc();
}

public void GetEnvironmentVariables()
{
if (string.IsNullOrWhiteSpace(chosenBot))
app.UseMvc(routes =>
{
chosenBot = Environment.GetEnvironmentVariable("CHOSENBOT");
if (string.IsNullOrWhiteSpace(chosenBot))
{
chosenBot = "DialogAndWelcomeBot";
}
}
routes.MapRoute(
name: "default",
template: "api/{controller}");
});
}
}
}