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

Node.js BotBuilder:Teams频道Adaptive Card输入未传入onAdaptiveCardInvoke

问题解决方案

针对你在Teams频道中发送Adaptive Card后,onAdaptiveCardInvoke无法获取用户输入的问题,以下是可行的修复方案:

核心问题分析

在Teams频道环境下,卡片交互的上下文处理和个人聊天存在差异:

  • ActionSet内的交互动作可能无法正确关联输入控件的数据
  • TeamsInfo.sendMessageToTeamsChannel方法会对消息进行额外封装,破坏卡片的交互上下文,导致输入数据无法传递

修复步骤及代码示例

1. 调整Adaptive Card结构

将Action.Execute移到卡片根级的actions数组,并明确设置associatedInputs: "auto",同时升级卡片版本到Teams更兼容的1.5:

public generate(): Promise<Partial<Activity>> {
    const card = {
      type: "AdaptiveCard",
      $schema: "http://adaptivecards.io/schemas/adaptive-card.json",
      version: "1.5",
      body: [
        {
          type: "Input.Text",
          placeholder: "John",
          label: "First Name",
          id: "firstName",
        }
      ],
      actions: [
        {
          type: "Action.Execute",
          title: "Execute",
          associatedInputs: "auto"
        }
      ]
    };
    return Promise.resolve({
      attachments: [CardFactory.adaptiveCard(card)],
      summary: "Test",
      type: ActivityTypes.Message
    });
  }

2. 修改频道消息发送方式

放弃TeamsInfo.sendMessageToTeamsChannel,直接通过turn.sendActivity发送消息,并指定频道的conversation ID:

public async notify(contact: MsTeamsContactInfo): Promise<void> {
    await this.adapter.continueConversationAsync(this.teamsBotId, contact.commObject, async (turn) => {
      const activity = await this.testCard.generate();
      if (contact.kind !== "user") {
        activity.conversation = { id: contact.channel.id };
        await turn.sendActivity(activity);
      } else {
        await turn.sendActivity(activity);
      }
    });
  }

3. 验证onAdaptiveCardInvoke逻辑

确保你的事件处理方法能正确接收数据,此时invokeValue.action.data.firstName应该能正常获取用户输入:

public override async onAdaptiveCardInvoke(context: TurnContext, invokeValue: AdaptiveCardInvokeValue): Promise<AdaptiveCardInvokeResponse> {
    console.log(JSON.stringify(invokeValue));
    // 处理数据并返回响应
    return {
        statusCode: 200,
        type: "application/json",
        value: { message: "输入已收到" }
    };
}

备选方案:使用Action.Submit

如果坚持使用Action.Submit,注意它不会触发onAdaptiveCardInvoke,而是会触发普通的onMessage事件,需要在该事件中处理输入数据:

public override async onMessage(context: TurnContext): Promise<void> {
    if (context.activity.value) {
        const inputData = context.activity.value as { firstName: string };
        console.log(`用户输入的名字:${inputData.firstName}`);
        await context.sendActivity(`已收到你的输入:${inputData.firstName}`);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 18:22:56