在 Azure 代理中配置代理上下文

注释

Copilot Studio 机器人重命名为 Copilot 代理(代理或 AI 代理)。 人工代理现在更名为客户服务代表(服务代表或代表)。 在我们更新产品 UI、文档和培训内容时,您可能会遇到对新旧术语的引用。

对于 Azure 代理,必须在配置代理上下文之前安装代理 SDK 并实例化 Omnichannel 中间件。

在项目中安装机器人 SDK

  1. 若要打开 NuGet 包管理器,请右键单击项目,然后选择“ 管理 NuGet 包”。

  2. 在 NuGet 包管理器中,选择包源作为 nuget.org 并浏览“Microsoft.Dynamics.AgentsSDK.中间件”。 选择包,然后选择“ 安装”。 在 Nuget 页中了解详细信息。

    或者,可以在 NuGet CLI 中使用以下命令。

    Install-Package Microsoft.Dynamics.AgentsSDK.Middleware
    

代理 SDK 现已安装,并且可在项目中使用 Omnichannel 中间件。

在代理代码中使用 Omnichannel 中间件

  1. 打开 AdapterWithErrorHandler.cs 文件。

  2. 添加 import 语句并实例化 Omnichannel 中间件。

    using Microsoft.Dynamics.AgentsSDK.Middleware.Core; 
    Use(new OmnichannelMiddleware()); 
    
    using System.Globalization;
    using System.Text;
    using Microsoft.Agents.Connector;
    using Microsoft.Agents.Core;
    using Microsoft.Agents.Core.Errors;
    using Microsoft.Extensions.Logging;
    using Microsoft.Dynamics.AgentsSDK.Middleware.Core;
    
    namespace Microsoft.CCaaS.MessagingRuntime.TestAgent;
    
    public class AdapterWithErrorHandler : CloudAdapter
    {
        public AdapterWithErrorHandler(
            IChannelServiceClientFactory channelServiceClientFactory,
            IActivityTaskQueue activityTaskQueue,
            ILogger<CloudAdapter> logger)
            : base(channelServiceClientFactory, activityTaskQueue, logger)
        {
            // OmnichannelMiddleware has special handling for OC event messages
            Use(new OmnichannelMiddleware());
    
            OnTurnError = async (turnContext, exception) =>
            {
                var exceptionInfo = GetExceptionInfo(exception);
                logger.LogAppException(exceptionInfo, exception);
    
                // Send a message to the user
                await turnContext.SendActivityAsync($"The bot encountered an error or bug.{Environment.NewLine}{exceptionInfo}");
                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");
            };
        }
    
        private static string GetExceptionInfo(Exception exception)
        {
            var sb = new StringBuilder();
    
            // Pull some well known info from ErrorResponse.Exception if available.
            if (exception is ErrorResponseException responseException)
            {
                sb.AppendLine(CultureInfo.InvariantCulture, $"Error code: {responseException.Body?.Error?.Code ?? "NA"}");
                sb.AppendLine(CultureInfo.InvariantCulture, $"Error message: {responseException.Body?.Error?.Message ?? "NA"}");
            }
    
            sb.AppendLine(CultureInfo.InvariantCulture, $"Exception message: {exception.Message}");
            sb.AppendLine();
            sb.AppendLine(exception.ToString());
    
            var exceptionInfo = sb.ToString();
            return exceptionInfo;
        }
    }
    

后续步骤

分析活动 JSON 以获取代理上下文

发送自定义上下文
setContextProvider
集成 Azure 代理