Teams Bot(Java SDK):嵌入式Web视图提交后无法显示FileConsentCard
问题:嵌入式Web视图提交任务后无法显示文件同意卡片
我在嵌入式Web视图任务模块中调用microsoftTeams.tasks.submitTask()传递数据,代码如下:
var fsObj = {}; fsObj.name = "give me a file"; microsoftTeams.tasks.submitTask(fsObj);
数据会到达Bot并触发onTeamsMessagingExtensionSubmitAction事件,我的处理代码如下:
@Override protected CompletableFuture<MessagingExtensionActionResponse> onTeamsMessagingExtensionSubmitAction(TurnContext turnContext, MessagingExtensionAction action) { File filePath = new File("files", "10840-002.pdf"); String filename = filePath.getName(); long filesize = filePath.length(); Map<String, String> consentContext = new HashMap<>(); consentContext.put("filename", filename); FileConsentCard fileCard = new FileConsentCard(); fileCard.setDescription("Here is a file..."); fileCard.setSizeInBytes(filesize); fileCard.setAcceptContext(consentContext); fileCard.setDeclineContext(consentContext); MessagingExtensionAttachment asAttachment = new MessagingExtensionAttachment(); asAttachment.setContent(fileCard); asAttachment.setContentType(FileConsentCard.CONTENT_TYPE); asAttachment.setName(filename); MessagingExtensionResult msgExtRes = new MessagingExtensionResult(); msgExtRes.setType("result"); msgExtRes.setAttachmentLayout("list"); msgExtRes.setAttachment(asAttachment); MessagingExtensionActionResponse actionResponse = new MessagingExtensionActionResponse(); actionResponse.setComposeExtension(msgExtRes); return CompletableFuture.completedFuture(actionResponse); }
代码正常运行,嵌入式Web视图任务模块会关闭,但聊天中未显示文件同意卡片。我已成功复现文件上传Bot示例,直接与Bot对话时可以正常接收文件。请问是否可以响应嵌入式Web视图触发的提交动作返回FileConsentCard?如果可以,我的响应中缺少了什么?
回答
可以响应嵌入式Web视图的提交动作返回FileConsentCard,你的响应主要有两处核心问题:
1. 响应类型与格式错误
你当前使用MessagingExtensionActionResponse搭配composeExtension字段返回卡片,这种格式仅适用于消息扩展的结果展示场景。而任务模块提交后,Teams并不识别这种格式来展示聊天消息,正确做法是主动通过TurnContext发送包含FileConsentCard的消息活动,而非封装在消息扩展响应中。
2. 任务模块提交后的逻辑偏差
调用microsoftTeams.tasks.submitTask()后,Teams仅需要确认任务模块关闭的响应,不需要直接返回消息扩展结果。你需要先完成任务模块关闭的确认,再主动向对话窗口发送带卡片的消息。
修改后的代码示例
@Override protected CompletableFuture<MessagingExtensionActionResponse> onTeamsMessagingExtensionSubmitAction(TurnContext turnContext, MessagingExtensionAction action) { File filePath = new File("files", "10840-002.pdf"); String filename = filePath.getName(); long filesize = filePath.length(); Map<String, String> consentContext = new HashMap<>(); consentContext.put("filename", filename); FileConsentCard fileCard = new FileConsentCard(); fileCard.setDescription("Here is a file..."); fileCard.setSizeInBytes(filesize); fileCard.setAcceptContext(consentContext); fileCard.setDeclineContext(consentContext); MessagingExtensionAttachment asAttachment = new MessagingExtensionAttachment(); asAttachment.setContent(fileCard); asAttachment.setContentType(FileConsentCard.CONTENT_TYPE); asAttachment.setName(filename); // 主动向聊天窗口发送包含FileConsentCard的消息 Activity reply = MessageFactory.attachment(asAttachment); turnContext.sendActivity(reply).join(); // 返回空响应确认任务模块关闭 MessagingExtensionActionResponse actionResponse = new MessagingExtensionActionResponse(); return CompletableFuture.completedFuture(actionResponse); }
关键说明
- 优先通过
turnContext.sendActivity()将FileConsentCard作为独立消息发送到对话中 - 返回的
MessagingExtensionActionResponse无需设置composeExtension字段,保持默认即可,Teams会自动处理任务模块关闭逻辑
内容的提问来源于stack exchange,提问作者lfdevjmp
相关产品推荐
相关产品推荐

