github 上 tompaana 的 Agent Handoff intermediator-bot-sample (c#) 无法将 Microsoft.Bot.Builder 和相关包升级到版本 4.10.3

2024-01-06

问题陈述

这是关于由 tompaana 创建的 Live Agent Handoff intermediator-bot-sample (c#)https://github.com/tompaana/intermediator-bot-sample https://github.com/tompaana/intermediator-bot-sample在 github 上。

  • intermediator-bot-sample 与 Microsoft.Bot.Builder (4.2.2) 完美配合和 Microsoft.Bot.Builder.Integration.AspNet.Core(4.2.2) 和依赖版本 4.2.2 包,但是,它不使用对话框。

  • HandoffMiddleware 代码停止被调用,当我添加包时Microsoft.Bot.Builder.Dialogs (4.10.3) (as my existing代码需要对话框)。这也造成了升级到 Microsoft.Bot.Builder 至版本 4.10.3及其依赖包,即 Microsoft.Bot.Builder.Integration.AspNet.Core 等。

社区支持

原作者 Handoff intermediator-bot-sample托米·帕纳宁 (Tomi Paananen) 又名 tompaana https://github.com/tompaana已转移到其他项目,无法再投入时间到该项目,并请求联系 MS Botframework 社区成员寻求支持 (参考:作者对 Github 问题的回应 https://github.com/tompaana/intermediator-bot-sample/issues/59).

  • 要求BotFramework 社区请帮助将代理移交功能添加到我现有的聊天机器人中

观察:

  • 即使在软件包升级后,切换中间件类在启动期间成功实例化。

  • 我的改进代码包含 BotController 类,通过它可以调用所有 API。原始 Handoff intermediator-bot-sample 代码中不存在此 BotController 类。

  • 在聊天机器人上输入任何话语(升级/新代码)时,控制进行进入机器人控制器 class 而不是调用/触发HandoffMiddleware.OnTurnAsync(...)

  • As the 原始中介机器人示例代码没有任何 BotController/API 控制器,这可能是话语未通过路由的原因,切换中间件 中间件如果是这样,我该如何解决这个问题?

// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;

namespace Neo.Controllers
{
    // 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")]
    [ApiController]
    public class BotController : ControllerBase
    {
        private readonly IBotFrameworkHttpAdapter Adapter;
        private readonly IBot Bot;

        public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
        {
            Adapter = adapter;
            Bot = bot;
        }

        [HttpPost, HttpGet]
        public async Task PostAsync()
        {
            // Delegate the processing of the HTTP POST to the adapter.
            // The adapter will invoke the bot.
            await Adapter.ProcessAsync(Request, Response, Bot);
        }
    }
}

参考包

原始 intermediator-bot-sample 引用的包

升级的 intermediator-bot-sample 引用包

原始 intermediator-bot-sample 解决方案文件

升级的 intermediator-bot-sample 解决方案文件

Query

您能否建议我如何解决这个问题?

  • As the HandoffMiddleware.OnTurnAsync(..)工作正常当我执行时原始代码 but, 不会被触发 My Code改造 IntermediateBot 代码后升级后的 Microsoft.Bot.Builder and 相关包 to 版本4.10.3 .

  • 指向现有的工作 Agent HandOff 示例(c#)也会有所帮助


以下解决方案使升级后的 Tompanna Agent Handoff 解决方案能够顺利运行:

  • 解决方案在于BotFrameworkHttpAdapter需要调用HandoffMiddleware的方式。

检查中间件 https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/47.inspectionGithub 中的示例提供了调用任何中间件的方法,即在我们升级了 Microsoft.Bot.Builder 和引入 BotController 类/API Controller 概念的相关包的情况下。

AdapterWithInspection.cs 的代码参考来自BotBuilder-样本/样本 https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/csharp_dotnetcore/47.inspection/AdapterWithInspection.cs

Replace 检查中间件在下面的代码中切换中间件


namespace Microsoft.BotBuilderSamples
{
    public class AdapterWithInspection : BotFrameworkHttpAdapter
    {
        public AdapterWithInspection(IConfiguration configuration, InspectionState inspectionState, UserState userState, ConversationState conversationState, ILogger<BotFrameworkHttpAdapter> logger)
            : base(configuration, logger)
        {
            // Inspection needs credentiaols because it will be sending the Activities and User and Conversation State to the emulator
            var credentials = new MicrosoftAppCredentials(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"]);

//***********************************************************************************//
//* InspectionMiddleware needs to be replace HandOffMddieWare in the execution pipeline *// 
//***********************************************************************************//          
              Use(new InspectionMiddleware(inspectionState, userState, conversationState, credentials));

            OnTurnError = async (turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");

                // Send a message to the user
                await turnContext.SendActivityAsync("The bot encountered an error or bug.");
                await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    }
}

新代码应如下所示

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.


namespace Microsoft.BotBuilderSamples
{
    public class AdapterWithInspection : BotFrameworkHttpAdapter
    {
        public AdapterWithInspection(IConfiguration configuration, InspectionState inspectionState, UserState userState, ConversationState conversationState, ILogger<BotFrameworkHttpAdapter> logger)
            : base(configuration, logger)
        {
            // Inspection needs credentials because it will be sending the Activities and User and Conversation State to the emulator
            var credentials = new MicrosoftAppCredentials(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"]);

//***********************************************************************************//
//*************** Adding HandOffMddieWare in the execution pipeline *****************//           
//***********************************************************************************//
            Use(new HandoffMiddleware(configuration));

            OnTurnError = async (turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");

                // Send a message to the user
                await turnContext.SendActivityAsync("The bot encountered an error or bug.");
                await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    }
}

NOTE

  • 您需要相应地在 Startup.cs 中注入依赖项

在 Startup.cs 中注入依赖项

添加以下代码以方便 AdapterWithInspection 的依赖注入

            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithInspection>();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

github 上 tompaana 的 Agent Handoff intermediator-bot-sample (c#) 无法将 Microsoft.Bot.Builder 和相关包升级到版本 4.10.3 的相关文章

随机推荐