Python Bot Builder框架下Bot Builder emulator自适应卡片显示空白求助
问题解决办法
1. 修复代码错误
你提供的加载代码存在缩进、路径拼接、编码三类错误,会直接导致卡片加载失败或者结构损坏,修正后代码如下:
def adaptive_flight_card_attachment(self): JSONFileName = 'QuickAdaptiveJSONfile.json' file_path = "AdaptiveFlightCardFile" if os.path.exists(file_path): files_in_dir = os.listdir(file_path) if JSONFileName in files_in_dir: # 使用os.path.join拼接路径,兼容不同操作系统的路径格式 file_path = os.path.join(os.getcwd(), file_path, JSONFileName) # 把编码从unicode_escape改为utf-8,前者会错误解析JSON中的转义字符,导致卡片结构异常 with open(file_path, "r", encoding="utf-8") as in_file: card = json.load(in_file) return Attachment(content_type="application/vnd.microsoft.card.adaptive", content=card) # 增加兜底返回,避免路径不存在时函数返回None return None
2. 增加空值校验逻辑
卡片加载失败返回None时,直接发送会导致emulator渲染空白占位,修改发送代码增加校验:
FlightDetailCard = self.adaptive_flight_card_attachment() if FlightDetailCard: response = MessageFactory.attachment(FlightDetailCard) await turn_context.send_activity(response) else: # 可增加调试日志定位加载失败问题 await turn_context.send_activity("卡片加载异常")
3. 确认自适应卡片版本兼容
Bot Framework Emulator最高支持1.5版本的自适应卡片,如果你JSON文件中指定的版本高于1.5,会出现空白渲染问题,修改JSON头部的版本声明即可:
{ "$schema": "http://adaptivecards.io/schemas/adaptive-card-1.5.json", "type": "AdaptiveCard", "version": "1.5", ... }
4. 检查Emulator配置
进入Emulator的设置页,在「特性」分类下确认自适应卡片渲染开关已开启,旧版本Emulator该选项默认关闭。
内容的提问来源于stack exchange,提问作者KingKong BigBong
相关产品推荐
相关产品推荐

