首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >github上tompaana的代理Handoff中介-bot-sample (c#)不适用于将Microsoft.Bot.Builder和相关软件包升级到Ver 4.10.3

github上tompaana的代理Handoff中介-bot-sample (c#)不适用于将Microsoft.Bot.Builder和相关软件包升级到Ver 4.10.3
EN

Stack Overflow用户
提问于 2020-10-01 08:04:28
回答 1查看 997关注 0票数 0

问题陈述

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

  • Microsoft.Bot.Builder.Integration.AspNet.Core(4.2.2) -样例与Microsoft.Bot.Builder ( 4.2.2 )和依赖版本4.2.2包完美地工作在一起,但不使用对话框.
  • --当我添加包Microsoft.Bot.Builder.Dialogs (4.10.3)时,HandoffMiddleware代码停止调用(,因为我的现有代码需要对话框)。这也导致了升级到Microsoft.Bot.Builder到4.10.3版本的以及它的依赖包,即Microsoft.Bot.Builder.Integration.AspNet.Core等等。

社区支持

最初的作者Handoff中介人-bot Tomi Paananen A.K.已经转移到其他项目上,不再能够将时间花在这个项目上,并请求与members社区成员联系以获得支持(参考:作者对Github问题的答复)。

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

观察:

  • 即使在包升级之后,HandoffMiddleware类也会在启动期间成功实例化。
  • 我的改进代码包含BotController类,通过该类调用所有API。这个BotController类不存在于原始的Handoff中间程序-bot-示例代码中。
  • 在聊天机器人(升级/新代码)上输入任何语句时,BotController控件将放入类,而不是调用/触发。
  • 由于最初的中介-bot-样例代码没有任何BotController/ API控制器,这是否是HandoffMiddleware 中间件(如果是的话,如何解决这个问题?)的原因吗?
代码语言:javascript
复制
// 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);
        }
    }
}

参考包

原始中介-bot-示例参考包

升级的中介-bot-示例参考包

原始中介-bot-示例解决方案文件

升级的中介-bot-示例解决方案文件

查询

你能建议我怎么解决这个问题吗?

  • 由于HandoffMiddleware.OnTurnAsync(..) 在执行( coriginal code_ )时工作良好,但是,在用升级的Microsoft.Bot.Builder<代码>E 283和E 184相关包E 285E 186版本4.10.3<代码>E 287后,不会从 My Code触发。
  • 指向现有的工作代理HandOff示例(c#)也会有所帮助
EN

回答 1

Stack Overflow用户

发布于 2020-10-04 10:51:54

以下解决方案使升级后的Tompanna代理Handoff解决方案工作平稳:

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

Github中的检验中间件示例提供了调用任何中间件的方法,即在有升级的Microsoft.Bot.Builder和相关包的情况下,引入了BotController类/BotController的概念。

AdapterWithInspection.cs在BotBuilder-样本/样本中的代码引用

将以下代码中的InspectionMiddleware替换为HandoffMiddleware

代码语言:javascript
复制
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");
            };
        }
    }
}

新代码应该如下所示

代码语言:javascript
复制
// 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");
            };
        }
    }
}

备注

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

在Startup.cs中注入依赖项

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

代码语言:javascript
复制
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithInspection>();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64151304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档