USER <--> BOT
USER -- Azure Bot Service -- BOT
USER -- ABS -- Adapter -- BOT
USER -- ABS -- Adapter(TurnContext) +- Middlewares -- BOT
+-- Storage
Activity -- Adapter -- TurnContext(Activity) -- Middleware -- BOT
|
Storage
Activity -- Adapter -- TurnContext(Activity) -- Middleware -- BOT
|
statePropertyAccessors -- BotState
/ \
UserState ConversationState
\ /
Storage (CosmosDB)
/// MessageTrigger.cs
public static class MessageTrigger
{
[FunctionName("messages")]
public static Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log,
ExecutionContext context)
{
ICredentialProvider credentialProvider = GetCredentialProvider(context);
IBotFrameworkFunctionsAdapter adapter = new BotFrameworkFunctionsAdapter(credentialProvider, logger: log);
IBot bot = new StaticBot();
return adapter.ProcessAsync(req, bot);
}
private static ConfigurationCredentialProvider GetCredentialProvider(ExecutionContext context)
{
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
return new ConfigurationCredentialProvider(config);
}
}
/// MessageTrigger.cs
public static class MessageTrigger
{
[FunctionName("messages")]
public static Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[Inject] IBotFrameworkFunctionsAdapter adapter,
[Inject] IBot bot)
{
return adapter.ProcessAsync(req, bot);
}
}
/// StartUp.cs
[assembly: WebJobsStartup(typeof(FunctionBot.StartUp))]
namespace FunctionBot
{
public class StartUp : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.AddDependencyInjection(SimpleWire);
}
private void SimpleWire(IServiceCollection services)
{
services.AddSingleton<ICredentialProvider>(new SimpleCredentialProvider("<YOUR-APPID>", "<YOUR-PASSWORD>"));
services.AddSingleton<IBotFrameworkFunctionsAdapter, SimpleAdapter>();
services.AddSingleton<IBot, SimpleBot>();
}
}
}
NOTE: This version uses the latest version of Azure Functions with native support for DI.
/// MessageTrigger.cs
public class MessageTrigger
{
private readonly IBotFrameworkFunctionsAdapter adapter;
private readonly IBot bot;
public MessageTrigger(IBotFrameworkFunctionsAdapter adapter, IBot bot)
{
this.adapter = adapter;
this.bot = bot;
}
[FunctionName("messages")]
public Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
return adapter.ProcessAsync(req, bot);
}
}
/// StartUp.cs
[assembly: WebJobsStartup(typeof(DIFunctionBot.StartUp))]
namespace DIFunctionBot
{
public class StartUp : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
builder.Services.AddSingleton<IBotFrameworkFunctionsAdapter, SimpleAdapter>();
builder.Services.AddSingleton<IBot, SimpleBot>();
}
}
}