You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Bot Service对接MS Teams消息接收异常及SDK选择咨询

Azure Bot Service对接Teams无法接收用户回复的问题

问题背景

我在应用中使用Azure Bot Service对接MS Teams,通过Azure Bot Service Web Chat发送消息时能收到机器人的回复,但当我回复消息时,Bot Service无法接收该响应。我不想使用Bot Framework,而是尝试通过Agent实现。由于是在Bot Service中使用MS Teams,请问我应该使用现有代码,还是优先选择365 Agent SDK或Teams SDK?现有代码能生成完整响应内容,但Bot Service无法接收该响应。

当前代码实现

Program.cs 代码

app.MapBotEndpoints();

消息服务类代码

namespace AgentApp.Services
{
    public interface IMessageService
    {
        Task<Activity> HandleMessageAsync(Activity activity);
    }

    public class MessageService : IMessageService
    {
        public Task<Activity> HandleMessageAsync(Activity activity)
        {
            var userMessage = activity.Text?.ToLower();
            string reply = "Hi! This is your bot.";

            // Build response for your custom bot
            return Task.FromResult(new Activity
            {
                Type = "message",
                Text = reply,
                From = activity.Recipient,
                Recipient = activity.From
            });
        }
    }   
}

Bot端点配置代码

namespace AgentApp.Endpoints
{
    public static class BotEndpoints
    {
        public static void MapBotEndpoints(this WebApplication app)
        {
            app.MapPost("/api/messages", async (HttpContext context, IMessageService service) =>
            {
                var activity = await context.Request.ReadFromJsonAsync<Activity>();

                if (activity == null || activity.Type != "message")
                    return Results.Ok();

                var response = await service.HandleMessageAsync(activity);
                return Results.Ok(response);
            });
        }
    }
}

问题分析与解决方案

现有代码的核心问题

现有代码无法被Bot Service识别的原因是手动构建的Activity缺少Teams通信协议要求的关键字段:

  • 未设置ReplyToId:必须指向用户原消息的ID,告诉Bot Service这是对特定消息的回复
  • 未复用Conversation对象:缺少会话信息会导致Bot Service无法正确路由回复到用户会话
  • 未设置ServiceUrl:指定发送回复的Teams服务地址,是Bot Service转发消息的必要参数

SDK选择建议

  • 优先使用Teams SDK:如果你不想依赖完整的Bot Framework,Teams SDK(Microsoft Teams SDK for .NET)专为Teams场景设计,提供了简化的消息构建、会话管理API,能避免手动构建Activity的协议错误,比365 Agent SDK更贴合Teams机器人的需求。
  • 365 Agent SDK更适合跨Microsoft 365应用的Agent场景,单纯对接Teams机器人的话不是最优选择。

现有代码修复方案

修改MessageService中的回复Activity构建逻辑,补充必填字段:

public Task<Activity> HandleMessageAsync(Activity activity)
{
    var userMessage = activity.Text?.ToLower();
    string reply = "Hi! This is your bot.";

    return Task.FromResult(new Activity
    {
        Type = ActivityTypes.Message,
        Text = reply,
        From = activity.Recipient,
        Recipient = activity.From,
        ReplyToId = activity.Id, // 绑定到原消息ID
        Conversation = activity.Conversation, // 复用会话信息
        ServiceUrl = activity.ServiceUrl // 指定服务地址
    });
}

同时修改端点返回方式,明确返回JSON格式:

return Results.Json(response);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 12:29:52