将设置从 .bot 文件迁移到 appsettings 时如何修复机器人配置

How to fix the bot configuration when migrating settings form .bot file to appsettings

我 运行 在尝试将我的机器人部署到 Azure 时遇到了问题。当我尝试创建 Azure 资源时出现以下错误:error: InvalidBotData, message: Version: Bot Version has an invalid value. 我仔细研究了一下,发现我的机器人是 4.3 版,而现在您需要 4.4 版才能部署……

我发现 Mircosoft 已经有针对此处发现的问题的解决方案:https://docs.microsoft.com/bs-latn-ba/azure/bot-service/bot-file-basics?view=azure-bot-service-4.0&tabs=csharp 我按照步骤操作,我还更改了 QnAmaker 和 Luis 的调用方式。但是当我 运行 应用程序时,我收到以下错误: System.InvalidOperationException: Unable to resolve service for type 'VacancyBot.VacancyBot.Services.BotServices' while attempting to activate 'VacancyBot.VacancyBotBot'. 我意识到该机器人不再添加到任何地方,所以我尝试使用 services.AddSingleton<VacancyBotBot>() 添加它,但这没有用。将其添加为 T运行sient 也不起作用。

正常添加bot的部分是这样的:

var secretKey = Configuration.GetSection("botFileSecret")?.Value;
var botFilePath = Configuration.GetSection("botFilePath")?.Value;

var botConfig = BotConfiguration.Load(botFilePath ?? @".\nlp-with-luis.bot", secretKey);
services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));

var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);

但这不再有效,因为从方面来看,无法找到 .\nlp-with-luis.bot。 (我还没有真正删除 .bot 文件,但我猜它现在不再使用它了?)。

我想知道是否有人碰巧知道如何添加机器人,或以使其再次运行的方式更改 BotConfiguration。我真的希望这是可能的!如果有人需要查看更多代码,请说明,我会尽力提供 (:

我忘了补充一点,我尝试将 "botFilePath": "VacancyBot.bot", "botFileSecret": "", 放回 appsettings 文件中,但结果在 Azure 中再次出现相同的错误...

.bot 文件仍然可以使用,但看起来您正在尝试结合使用 .bot 文件和 appsettings.json。让我们帮你理清头绪。

从 appsettings.json 开始:您不再需要 botFilePathbotFileSecret。相反,请像下面这样构建您的 appsettings.json:

{
  "MicrosoftAppId": "",
  "MicrosoftAppPassword": "",
  "LuisAppId": "",
  "LuisAPIKey": "",
  "LuisAPIHostName": ""
}

MicrosoftAppIdMicrosoftAppPassword 现在通过 ConfigurationCredentialProvider.cs 文件拉入,稍后将作为单例添加到 Startup.cs 中。 ConfigurationCredentialProvider 应如下所示:

using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;

namespace CoreBot1
{
    public class ConfigurationCredentialProvider : SimpleCredentialProvider
    {
        public ConfigurationCredentialProvider(IConfiguration configuration)
            : base(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"])
        {
        }
    }
}

简短、甜美、切中要害。最后,像下面这样构造您的 startup.cs,以添加机器人和 ICredentialProvider:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.DependencyInjection;

using CoreBot1.Bots;
using CoreBot1.Dialogs;

namespace CoreBot1
{
    public class Startup
    {
        public Startup()
        {
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();

            // Create the Bot Framework Adapter with error handling enabled.
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

            // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
            services.AddSingleton<IStorage, MemoryStorage>();

            // The Dialog that will be run by the bot.
            services.AddSingleton<MainDialog>();

            // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
            services.AddTransient<IBot, DialogAndWelcomeBot<MainDialog>>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

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