microsoft / BotBuilder-Samples

Welcome to the Bot Framework samples repository. Here you will find task-focused samples in C#, JavaScript/TypeScript, and Python to help you get started with the Bot Framework SDK!
https://github.com/Microsoft/botframework
MIT License
4.37k stars 4.87k forks source link

C# SDK samples should all use AddBotRuntime() in startup #3287

Closed johnataylor closed 2 years ago

johnataylor commented 3 years ago

The AddBotRuntime() extension function has been recently added to the SDK. This function registers with the Dependency Injection framework everything that is needed to run Adaptive Dialogs. This function builds the Composer runtime.

One of the important mechanisms at play here is that the various dependencies (the Adapter, Middleware, Storage, the Bot itself etc.) are added with the TryAddSingleton function, and this means they can be overridden.

While the default IBot that is registered actually runs Adaptive Dialogs it only takes a line of code in startup to override this with any other implementation. For example, an IBot implementation built in code derived from ActivityHandler. In other words there is nothing that limits this function to Adaptive Dialogs and Composer, it can be used to create the runtime for any IBot implementation.

Other than the obvious appeal of consistency and reuse, the advantage of using this approach is that the model shifts to one of overriding default working behavior with custom behavior. Previously the expectation was that the developer would have to know all the component parts and build up the runtime.

Although ultimately the developer who wishes to customize behavior will need to have an understanding of the various internal dependencies, what needs to be understood by the developer who just wants to get something working is significantly reduced.

This change also means there is an opportunity to consolidate aspects of the documentation as the way by which you customize and extend an Adaptive Dialog based Composer runtime is exactly the same way you customize and extend the runtime for a bot that was written all in code.

Specifically we should switch to using AddBotRuntime across all the SDK samples. For example:

services.AddSingleton<IBot, EchoBot>();
services.AddBotRuntime(Configuration);

and for a bot that uses Dialogs:

services.AddSingleton<MainDialog>();
services.AddSingleton<IBot, DialogBot<MainDialog>>();
services.AddBotRuntime(Configuration);
johnataylor commented 3 years ago

Related to https://github.com/microsoft/BotBuilder-Samples/issues/3284

johnataylor commented 3 years ago

@carlosscastro suggests we add appropriate remarks documentation comments to the product code to document the extensibility and customization points on the runtime.

Tracking issue in the SDK repo https://github.com/microsoft/botbuilder-dotnet/issues/5598

johnataylor commented 3 years ago

We should reassess the need for samples 21 (telemetry) and 81 (skills) as the AddBotRuntime function builds the necessary runtime. Illustrating that was the primary reasons for these samples being introduced.

Possibly we could keep 81, albeit in a simplified form, you at least need to add an extra controller on the parent for the skill callbacks.

And in place of the redundant sample 21 we could consider introducing a sample that illustrated how to override the default telemetry with a custom solution. This also highlights the advantage of standardizing on AddBotRuntime as such a sample would be equally applicable to both Composer and all code bots.

msomanathan commented 3 years ago

I tried modifying skills sample to use AddBotRuntime in the root bot and skill bot to verify the runtime's behavior. The sample works without any modifications needed in the runtime, but it did need the following changes to the sample (which may need to be looked at further as to whether the runtime can be modified to accommodate these):

msomanathan commented 3 years ago

I was investigating an ICM related to using DirectLine-ASE channel on skills, and I added this channel to skills sample along with AddBotRuntime() in startup. I found that the messages coming back from skill are not relayed back to the channel since the ConnectorClient is trying to send the message to urn:botframework:namedpipe:bfv4.pipes since ContinueConversation uses proactive message for skills replies. However, the root bot's messages are sent back correctly since the ConnectorClient's ServiceUrl for this scenario is https://api.botframework.com/. This issue needs to be further investigated.

tracyboehrer commented 3 years ago

@msomanathan For Skills EOC, see https://github.com/microsoft/botbuilder-dotnet/issues/5335 and https://github.com/microsoft/botbuilder-dotnet/issues/5330. For 5530 (Root bot), there will need to some other changes. The way the samples do it won't work the same here.