github上tompaana制作的Agent Handoff intermediator-bot-samplec#无法将Microsoft.Bot.Builder和相关软件包升级到Ver 4.10.3

问题描述

问题陈述

这是关于tompaana在guithub上的https://github.com/tompaana/intermediator-bot-sample创建的Live Agent Handover中介程序机器人示例(c#)。

  • 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) 现有 代码需要对话框)。这也导致升级到Microsoft.Bot.Builder到版本4.10.3 及其附属程序包,即Microsoft.Bot.Builder.Integration.AspNet.Core等。

社区支持

原始作者交接中介人机器人示例Tomi Paananen A.K.A tompaana已移至其他项目,将不再能够花时间在该项目上,并已请求MS Botframework社区成员寻求支持({{ 3}})。

  • 请求 BotFramework社区,请帮忙将Agent Hand Off功能添加到我现有的chatbot中。

观察:

  • 即使程序包升级后, HandoffMiddleware类 在启动过程中也已成功实例化。

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

  • 在聊天机器人上键入任何语音信息(已升级/新代码)后,控件会进入 BotController 而不是调用/触发 HandoffMiddleware.OnTurnAsync(...)

  • 由于原始的中介人机器人样本代码没有任何BotController / API控制器,这可能是导致发话不通过 HandoffMiddleware Middleware ,如果是, 我该如何解决此问题?

// 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);
        }
    }
}

参考软件包

原始的中介人机器人示例引用的软件包

Refer: Author Response to Github issue raised

参考了升级的intermediator-bot-sample程序包

Original intermediator-bot-sample referenced Packages

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

enter image description here

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

enter image description here

查询

请问我如何解决此问题?

    当我执行原始代码_ 时,
  • 作为 HandoffMiddleware.OnTurnAsync(..)正常工作,但是 升级的Microsoft.Bot.Builder 相关程序包改装为后,对IntermediateBot代码进行修改后,不会从我的代码中触发em 版本4.10.3

  • 指向现有的有效Agent HandOff示例(c#)也将有所帮助

解决方法

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

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

The inspection Middleware示例提供了调用任何中间件的方法,即在我们具有升级的Microsoft.Bot.Builder和引入了BotController类/ API Controller概念的相关程序包的情况下。

来自BotBuilder-Samples/samples的AdapterWithInspection.cs的代码参考

在以下代码中用 HandoffMiddleware

替换 InspectionMiddleware


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,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,"TurnError");
            };
        }
    }
}

注意

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

在Startup.cs中注入依赖项

添加以下代码以方便对AdapterWithInspection进行依赖注入

            services.AddSingleton<IBotFrameworkHttpAdapter,AdapterWithInspection>();