如何使用Visual Studio的Enterprise Bot Template创建机器人?求教程
Hey there, I’ve put together a step-by-step guide to building a bot using Visual Studio’s Enterprise Bot Template while avoiding the Bot Framework cloud services your company restricts. Let’s dive in:
Prerequisites First
- Make sure you have Visual Studio 2022 (2019 works too, but 2022 is more stable) installed with the .NET Desktop Development and AI Development workloads.
- Install the Enterprise Bot Template: Open VS, go to
Extensions > Manage Extensions, search for "Enterprise Bot Template" in the marketplace, download it, restart VS once installed.
1. Create Your Enterprise Bot Project
- Fire up Visual Studio and click
Create a new project. - Search for "Enterprise Bot" in the project template search bar, select the matching result.
- Name your project, pick a save location, and hit
Create. The template will generate a full solution with separate projects for bot logic, dialogs, models, etc.—this is the core enterprise bot structure.
2. Tweak the Bot Logic for Local Use
The template uses the Bot Framework SDK by default, but we can adjust it to run entirely locally without cloud dependencies:
- Open the
Botproject’sBot.csfile—this is where your bot handles interactions. - Modify the
OnMessageActivityAsyncmethod to handle local input/output. For example, a simple response setup:protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) { var userInput = turnContext.Activity.Text; var botResponse = $"Got it, you said: {userInput} | This is your local enterprise bot speaking!"; await turnContext.SendActivityAsync(MessageFactory.Text(botResponse), cancellationToken); } - Use the template’s built-in dialog system under the
Dialogsproject to add custom flows (like greeting dialogs or help menus) by extending theComponentDialogclass.
3. Set Up a Local Testing Host
To test the bot without cloud channels, we’ll build a simple console host:
- Add a new Console App project to your solution.
- Reference your main
Botproject in the console app, then install only the local-focused Bot Framework SDK NuGet packages (skip any Azure-specific ones likeMicrosoft.Bot.Builder.Azure). - Add this code to the console app’s
Program.csto simulate user interactions:static async Task Main(string[] args) { var yourBot = new YourBotNameBot(); // Replace with your bot class name var adapter = new ConsoleAdapter(); Console.WriteLine("Local Enterprise Bot is running! Type a message to start:"); await adapter.ProcessAsync(async (context, token) => { await yourBot.OnTurnAsync(context, token); }, cancellationToken: CancellationToken.None); } - Run the console project, type messages directly into the console, and your bot will respond locally.
4. Add Enterprise-Grade Features (Locally)
- Intent Recognition: Use
Microsoft.Recognizers.Text(included in the template) to handle user intents locally instead of cloud-based LUIS. - State Management: Swap out Azure storage for local
MemoryStoragein the Bot project’sStartup.cs:services.AddSingleton<IStorage, MemoryStorage>(); services.AddSingleton<UserState>(); services.AddSingleton<ConversationState>(); - Multi-Step Dialogs: Use the template’s
WaterfallDialogto build complex flows (like user onboarding forms) that run entirely on your local machine.
Quick Compliance Check
Double-check your project to remove any references to Bot Framework cloud services (like LUIS, QnA Maker, or Azure storage) to stay aligned with your company’s rules. All logic runs locally, so no external cloud deployments are needed.
内容的提问来源于stack exchange,提问作者Raudel CostaLaHoz
相关产品推荐
相关产品推荐

