You need to enable JavaScript to run this app. 如何将Web聊天机器人未匹配答案的用户消息发送至Microsoft Teams渠道
优惠活动
大模型
产品
解决方案
定价
更多

如何将Web聊天机器人未匹配答案的用户消息发送至Microsoft Teams渠道

适配Web机器人无匹配答案转Teams专家回复的实现方案

基于你现有的C#+QnA Maker架构的Web聊天机器人,完全可以通过Bot Framework原生能力实现该场景,不需要额外开发复杂的通信对接层,核心流程完全匹配你的业务需求。

整体实现流程

  • 第一阶段:在现有机器人的QnA消息处理链路中增加判断逻辑,当QnA返回结果的置信度低于设定阈值(即未匹配到有效答案)时,不直接返回机器人兜底话术,而是给Web端用户发送“问题已提交专家团队,回复后将第一时间同步”的提示,同时把该用户的会话引用、提问内容、问题唯一ID存入持久化存储。
  • 第二阶段:通过Bot Framework原生的Microsoft Teams渠道接入能力,配置固定的专家团队群聊作为问题接收方,当检测到需转人工的问题时,主动向该专家群发送带交互按钮的自适应卡片,内容包含用户原始提问、对应问题ID、“回复该问题”的操作入口。
  • 第三阶段:专家在Teams端点击对应问题的回复入口、输入回复内容提交后,机器人根据问题ID查找之前存储的Web端用户会话引用,通过主动消息推送接口把专家回复同步发送给Web端的提问用户,同时给专家返回“回复已同步”的确认提示。

核心参考代码(基于Bot Framework 4.x C#版本,适配现有QnA Bot架构)

代码实现前提:已在项目中注册会话状态存储组件、完成Microsoft Teams渠道的基础接入配置。

  1. QnA结果判定转人工的核心逻辑,放置在原有QnA调用完成的处理节点中:
// 调用QnA Maker获取匹配结果
var qnaResult = await qnaServiceClient.GetAnswersAsync(turnContext);
// 设置匹配阈值,低于0.6判定为无有效匹配答案
if (qnaResult == null || qnaResult.FirstOrDefault()?.Score < 0.6)
{
    // 生成问题唯一标识
    var issueId = Guid.NewGuid().ToString();
    // 获取当前Web端用户的会话引用,存入持久化存储
    var userConversationRef = turnContext.Activity.GetConversationReference();
    await issueStorageAccessor.SetAsync(issueId, userConversationRef, turnContext.CancellationToken);
    await conversationState.SaveChangesAsync(turnContext, turnContext.CancellationToken);

    // 给Web端用户发送提示
    await turnContext.SendActivityAsync("您的问题已经提交给对应领域的专家,专家回复后会第一时间同步给您");

    // 组装Teams端带回复按钮的自适应卡片
    var expertReplyCard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 3))
    {
        Body =
        {
            new AdaptiveTextBlock($"收到Web端用户提问,问题ID:{issueId}") { Weight = AdaptiveTextWeight.Bolder },
            new AdaptiveTextBlock($"问题内容:{turnContext.Activity.Text}")
        },
        Actions =
        {
            new AdaptiveSubmitAction()
            {
                Title = "回复该问题",
                Data = new { action = "reply_to_issue", issueId = issueId }
            }
        }
    };
    // 从应用配置中读取提前存储的专家Teams群会话引用,向群内发送卡片
    var expertGroupRef = Configuration.GetSection("TeamsExpertGroupRef").Get<ConversationReference>();
    await (turnContext.Adapter as BotFrameworkAdapter).ContinueConversationAsync(
        appId: Configuration["MicrosoftAppId"],
        reference: expertGroupRef,
        callback: async (teamsTurnContext, ct) =>
        {
            await teamsTurnContext.SendActivityAsync(MessageFactory.Attachment(new Attachment
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = expertReplyCard
            }), ct);
        },
        cancellationToken: turnContext.CancellationToken);
    
    // 终止后续QnA兜底话术的发送逻辑
    return;
}
  1. Teams端卡片交互的处理逻辑,放置在自适应卡片回调处理节点:
protected override async Task<InvokeResponse> OnAdaptiveCardInvokeAsync(
    ITurnContext<IInvokeActivity> turnContext, 
    AdaptiveCardInvokeValue invokeValue, 
    CancellationToken cancellationToken)
{
    // 匹配专家点击“回复该问题”的操作
    if (invokeValue.Action?.Data?.ToString().Contains("reply_to_issue") == true)
    {
        // 提取关联的问题ID,标记当前会话为等待专家输入回复的状态
        var issueData = JObject.Parse(invokeValue.Action.Data.ToString());
        var issueId = issueData["issueId"].ToString();
        var pendingReplyProperty = userState.CreateProperty<string>("PendingReplyIssueId");
        await pendingReplyProperty.SetAsync(turnContext, issueId, cancellationToken);
        await userState.SaveChangesAsync(turnContext, cancellationToken);

        return new InvokeResponse() 
        { 
            StatusCode = 200, 
            Body = new { message = "请输入需要同步给用户的回复内容" } 
        };
    }
    return base.OnAdaptiveCardInvokeAsync(turnContext, invokeValue, cancellationToken);
}
  1. 专家回复内容的同步逻辑,放置在消息处理管线的最前端,优先判断是否为Teams渠道的待回复状态:
// 优先处理Teams渠道的专家回复
if (turnContext.Activity.ChannelId == Channels.Msteams)
{
    var pendingReplyProperty = userState.CreateProperty<string>("PendingReplyIssueId");
    var pendingIssueId = await pendingReplyProperty.GetAsync(turnContext, cancellationToken: cancellationToken);
    
    if (!string.IsNullOrEmpty(pendingIssueId))
    {
        // 根据问题ID查找对应的Web端用户会话引用
        var userRef = await issueStorageAccessor.GetAsync<ConversationReference>(pendingIssueId, cancellationToken);
        if (userRef != null)
        {
            // 主动向Web端用户推送专家回复
            await (turnContext.Adapter as BotFrameworkAdapter).ContinueConversationAsync(
                appId: Configuration["MicrosoftAppId"],
                reference: userRef,
                callback: async (webTurnContext, ct) =>
                {
                    await webTurnContext.SendActivityAsync($"来自专家的回复:{turnContext.Activity.Text}", cancellationToken: ct);
                },
                cancellationToken: cancellationToken);
            
            // 给专家发送确认提示
            await turnContext.SendActivityAsync("回复内容已成功同步给提问用户");
            // 清空待回复状态
            await pendingReplyProperty.SetAsync(turnContext, null, cancellationToken);
            await userState.SaveChangesAsync(turnContext, cancellationToken);
            return;
        }
    }
}

落地注意事项

  • 所有会话引用、问题关联关系必须做持久化存储,不要依赖内存缓存,避免应用重启后丢失会话关联关系,无法完成主动消息推送。
  • Teams专家群的配置需要提前把机器人拉进群聊,获取到群的会话引用后存在应用配置中,不需要自行实现Teams的通信协议对接。
  • 对存储的问题ID增加有效期校验,比如设置72小时的过期时间,避免长期未处理的问题被专家回复后,找不到对应的有效用户会话。

内容的提问来源于stack exchange,提问作者Navin Tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 17:45:04