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

如何检测机器人/应用是否已从Microsoft Teams中卸载

解决方案
  • 首先修正监听的事件类型
    你当前使用的onTeamsMembersRemovedEvent仅在机器人被作为团队成员手动移除、或其他团队成员被移除时触发,团队级应用卸载属于独立事件,需要单独监听onTeamsTeamUninstalledEvent事件,修正后代码示例如下:
export class MyBot extends TeamsActivityHandler {
    constructor() {
        super();
        // 原有成员移除事件(可选保留,用于监控机器人被踢/其他成员移除场景)
        this.onTeamsMembersRemovedEvent(async (membersRemoved: ChannelAccount[], teamInfo: TeamInfo, turnContext: TurnContext, next: () => Promise<void>): Promise<void> => {
            let removedMembers: string = '';
            console.log(JSON.stringify(membersRemoved));
            membersRemoved.forEach((account) => {
                removedMembers += account.id + ' ';
            });
            // 修复原代码潜在空指针报错:teamInfo为空时不要直接调用name属性
            const teamName = !teamInfo ? '非团队环境' : teamInfo.name;
            const card = CardFactory.heroCard('成员移除', `${removedMembers} 已从 ${teamName} 移除.`);
            const message = MessageFactory.attachment(card);
            await turnContext.sendActivity(message);
            await next();
        });

        // 新增:团队应用卸载事件监听
        this.onTeamsTeamUninstalledEvent(async (teamInfo: TeamInfo, turnContext: TurnContext, next: () => Promise<void>): Promise<void> => {
            console.log(`应用已从团队 ${teamInfo.name}(ID:${teamInfo.id})卸载`);
            // 此处编写卸载后的自定义业务逻辑
            await next();
        });
    }
}
  • 检查应用配置
  1. 确保应用清单(manifest.json)的scopes配置包含team,声明支持团队级安装
  2. 确保Azure Bot配置的Teams通道已开启,消息端点配置正确,本地测试时需使用内网穿透工具暴露本地服务,端点格式为https://你的公网可访问地址/api/messages
  3. 确保你的服务端点可以正常响应微软的POST请求,返回200状态码,否则微软会停止推送事件
  • 测试注意事项
    修改manifest后需要重新打包应用,先卸载团队中旧版本的应用,再上传新版本安装测试,旧版本的缓存配置不会自动更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 20:45:03