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

Teams应用:如何在Task Module中调用onInvokeActivity实现实时数据拉取?

解决Teams Task Module中filtered样式Input.ChoiceSet动态搜索触发后端接口的问题

要实现用户在filtered样式的Input.ChoiceSet输入时触发后端接口拉取数据,需要从卡片配置和后端invoke处理两方面调整,具体步骤如下:

1. 修改Adaptive Card中Input.ChoiceSet的配置

当前卡片里的Input.ChoiceSet仅设置了静态choices,需要添加动态搜索触发配置,让Teams客户端在用户输入时自动发送invoke请求到后端:

{
  type: "Input.ChoiceSet",
  id: "tagsChoiceSet",
  style: "filtered",
  isMultiSelect: true,
  // 替换原静态choices,改为动态搜索触发配置
  choices: {
    data: {
      type: "application/search",
      parameters: {
        // 可选:传递初始上下文参数,比如用户ID等
        userId: "<你的用户ID参数>"
      }
    }
  }
}

2. 正确处理后端的onInvokeActivity请求

当前onInvokeActivity方法未返回符合要求的搜索结果响应,且需正确获取用户输入的搜索关键词,修改后的代码如下:

export class ActionApp extends TeamsActivityHandler {
  constructor() {
    super();
  }
  // other codes..

  async onInvokeActivity(context: TurnContext): Promise<any> {
    if (context.activity.name === 'application/search') {
      // 获取用户输入的搜索关键词
      const searchQuery = (context.activity.value as any).query;
      console.log("用户搜索关键词:", searchQuery);

      // 调用后端接口获取匹配的标签数据
      const tags = await this.fetchTagsFromBackend(searchQuery);

      // 返回符合Teams要求的响应格式,包含动态生成的选项
      return {
        status: 200,
        body: {
          choices: tags.map(tag => ({
            value: tag.id, // 标签的实际值
            title: tag.name // 显示给用户的标签名称
          }))
        }
      };
    }

    return super.onInvokeActivity(context);
  }

  // 封装后端接口调用逻辑
  private async fetchTagsFromBackend(searchQuery: string) {
    // 替换为你的实际后端接口请求代码
    const response = await fetch(`<你的后端接口地址>?query=${encodeURIComponent(searchQuery)}`);
    return response.json();
  }
}

3. 注意事项

  • 确保Teams客户端版本支持Adaptive Cards 1.4及以上(你的卡片已设置version: "1.4",满足要求);
  • 检查Bot的endpoint配置是否正确,确保后端能正常接收Teams的invoke请求;
  • 若需传递额外上下文参数(如用户ID),可在choices.data.parameters中添加,后端通过context.activity.value.parameters获取。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:23:27