Teams Bot Framework中composeExtensions的Adaptive Card按钮不显示问题
问题原因
- 返回结构不符合Teams规范:你当前直接将自适应卡原始JSON赋值给了
task.value.card字段,而Teams的任务模块(Task Module)要求card必须是标准附件对象,需要明确指定contentType,否则会丢失部分渲染规则,包括ActionSet的解析。 - 任务模块场景渲染限制:消息扩展触发的任务模块中,Teams会默认忽略自适应卡
body内定义的ActionSet,仅支持两种按钮渲染方式:要么将按钮定义在自适应卡根节点的actions属性下,要么直接配置到任务模块的buttons字段中。 - 动作类型兼容性问题:
Action.Execute在任务模块场景下的客户端兼容性较差,部分版本的Teams会直接过滤该类型的动作。
解决方法
步骤1:修正返回的卡片结构
将自适应卡包装为标准的Teams卡片附件,修改你的handleTeamsMessagingExtensionFetchTask代码如下:
const cardContent = this.renderAdaptiveCard(enableInboundCard, { channel }) return { task: { type: 'continue', value: { // 把卡片包成标准附件格式 card: { contentType: "application/vnd.microsoft.card.adaptive", content: cardContent }, height: 500, title: 'Enable Inbound', width: 600 } } };
步骤2:调整按钮的定义位置(二选一即可)
方案A:修改自适应卡结构(推荐,保留卡片内按钮样式)
将ActionSet从body中移除,放到自适应卡根节点的actions字段下,同时降级Adaptive Card版本到1.4获得更好的兼容性,修改后的卡片定义如下:
{ "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.4", "body": [ { "type": "TextBlock", "size": "Large", "weight": "Bolder", "wrap": true, "style": "heading", "text": "Enable Inbound SMS" }, { "type": "TextBlock", "text": "A wholeeeeeeee bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch bunch of txt", "wrap": true }, { "type": "FactSet", "facts": [ { "title": "Current Channel", "value": "${channel}" } ] } ], "actions": [ { "type": "Action.Submit", "title": "Enable Inbound SMS", "data": { "verb": "enable_inbound" } } ] }
该方案触发的请求会走handleTeamsMessagingExtensionSubmitAction回调,你需要在该方法中实现后续业务逻辑。
方案B:将按钮配置到任务模块底部(更符合Teams交互规范)
如果不需要按钮嵌入卡片内容中,可以直接把按钮配置到任务模块的底部,修改返回代码如下:
const cardContent = this.renderAdaptiveCard(enableInboundCard, { channel }) return { task: { type: 'continue', value: { card: { contentType: "application/vnd.microsoft.card.adaptive", content: cardContent }, height: 500, title: 'Enable Inbound', width: 600, // 配置任务模块底部按钮 buttons: [ { type: "action", title: "Enable Inbound SMS", value: { verb: "enable_inbound" } } ] } } };
该方案的按钮点击同样会触发handleTeamsMessagingExtensionSubmitAction回调。
步骤3:验证清单配置
你当前的manifest配置没有问题,fetchTask: true已经正确开启,不需要额外修改。
内容的提问来源于stack exchange,提问作者atymic
相关产品推荐
相关产品推荐

