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

Microsoft Teams Bot首次添加时发送欢迎消息失败求助

问题:Microsoft Teams Bot添加时无法发送欢迎消息

我希望用户首次将Bot添加到Microsoft Teams时自动发送欢迎消息,但目前无法实现该功能。

添加Bot的步骤

  • 图1:在Teams界面中搜索目标Bot,进入Bot详情页
  • 图2:点击添加选项,确认将Bot添加到指定团队或聊天会话

期望实现效果

期望Bot在被添加后,自动发送包含自适应卡片的欢迎消息(带样式的欢迎卡片)


当前使用的代码

import { TeamsActivityHandler, TurnContext, ChannelAccount, ActivityTypes } from "botbuilder";

export class TeamsBot extends TeamsActivityHandler {
  constructor() {
    super();
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };
    // Bind the overridden onMembersAdded method
    this.onMembersAdded(this.onMembersAddedActivity.bind(this));
    // Bind the overridden onConversationUpdate method
    this.onConversationUpdate(this.onConversationUpdateActivity.bind(this));
  
  }

  /**
   * Overrides the onMembersAdded method from TeamsActivityHandler.
   * @param membersAdded List of members added to the conversation.
   * @param context The context object for this turn.
   */
  protected async onMembersAddedActivity(membersAdded: ChannelAccount[], context: TurnContext): Promise<void> {
    await context.sendActivity('Thank you for installing me!');
    console.log('onMembersAddedActivity')
    // Create a adaptive card and send it to the conversation when the bot is added to the conversation.
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };
    await context.sendActivity({ attachments: [card] });
  }

  protected async onConversationUpdateActivity(context: TurnContext): Promise<void> {
    const card = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };

    await context.sendActivity({ attachments: [card] });
    console.log('onConversationUpdateActivity')
    if (context.activity.type === ActivityTypes.ConversationUpdate) {
      // Check if the bot is the only member added
      if (context.activity.membersAdded?.length === 1 && context.activity.membersAdded[0].id === context.activity.recipient.id) {
        await context.sendActivity('Thank you for installing me!');
      }
    }
  }
  
}

问题分析与修复建议

  1. 移除冗余事件绑定
    TeamsActivityHandler已默认处理onMembersAdded和onConversationUpdate事件,无需在构造函数中手动绑定,重复绑定会导致逻辑冲突,直接重写对应方法即可。

  2. 修正会话更新逻辑顺序
    原代码先发送卡片再判断场景,会导致非添加Bot的场景也触发消息。应先判断是否为Bot被添加的场景,再执行发送逻辑。

  3. 补充自适应卡片必要字段
    自适应卡片缺少$schema和version字段,这会导致卡片无法正常渲染,必须补充这两个必填项。

  4. 区分添加场景
    区分Bot被添加到个人聊天和团队频道两种场景,确保只在首次添加时发送欢迎消息,避免重复触发。


修复后的代码示例

import { TeamsActivityHandler, TurnContext, ChannelAccount, ActivityTypes } from "botbuilder";

export class TeamsBot extends TeamsActivityHandler {
  constructor() {
    super();
  }

  // 处理Bot被添加到个人聊天的场景
  protected async onMembersAdded(membersAdded: ChannelAccount[], context: TurnContext): Promise<void> {
    const botId = context.activity.recipient.id;
    // 排除Bot自身,仅向添加Bot的用户发送欢迎消息
    for (const member of membersAdded) {
      if (member.id !== botId) {
        await this.sendWelcomeCard(context);
      }
    }
  }

  // 处理Bot被添加到团队频道的场景
  protected async onConversationUpdate(context: TurnContext): Promise<void> {
    const activity = context.activity;
    if (activity.type === ActivityTypes.ConversationUpdate && activity.membersAdded) {
      const botId = activity.recipient.id;
      const isBotAdded = activity.membersAdded.some(member => member.id === botId);
      // 确认是Bot被添加到团队的场景
      if (isBotAdded) {
        await this.sendWelcomeCard(context);
      }
    }
    // 调用父类方法处理其他会话更新逻辑
    await super.onConversationUpdate(context);
  }

  // 封装欢迎卡片发送逻辑,避免代码重复
  private async sendWelcomeCard(context: TurnContext): Promise<void> {
    const welcomeCard = {
      contentType: 'application/vnd.microsoft.card.adaptive',
      content: {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "version": "1.4",
        type: 'AdaptiveCard',
        body: [
          {
            type: 'TextBlock',
            text: 'Thank you for installing me!',
            weight: 'bolder',
            size: 'medium'
          }
        ]
      }
    };
    await context.sendActivity({ attachments: [welcomeCard] });
  }
}

额外注意事项

  • 确保Bot在Teams开发者门户中配置了正确的消息端点,能正常接收Teams的事件通知
  • 检查Bot的权限配置,确保拥有发送消息到团队/个人聊天的权限
  • 测试时使用全新的团队或个人聊天,避免历史缓存影响测试结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 23:00:43