AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

template_list能否上传图片

Open mjy1113451 opened this issue 1 week ago • 2 comments

mjy1113451 avatar Nov 20 '26 15:11 mjy1113451

Hi, 可以的。template_list 的每个模板的 items 里都能放 file 类型字段,WebUI 会在每条条目内单独渲染上传按钮;上传后的值是一组相对路径,不会把图片本身写进配置。

{
  "rules": {
    "type": "template_list",
    "description": "自动回复规则",
    "default": [],
    "templates": {
      "keyword": {
        "name": "关键词回复",
        "display_item": "keyword",
        "items": {
          "keyword": {
            "type": "string",
            "description": "关键词",
            "default": "help"
          },
          "reply": {
            "type": "text",
            "description": "回复内容",
            "default": ""
          },
          "images": {
            "type": "file",
            "description": "配图",
            "default": [],
            "file_types": ["png", "jpg", "jpeg", "webp", "gif"]
          }
        }
      }
    }
  }
}

保存后的条目结构大致是:

{
  "rules": [
    {
      "__template_key": "keyword",
      "keyword": "help",
      "reply": "请看图",
      "images": ["files/rules/templates/keyword/images/help.png"]
    }
  ]
}

插件侧读取时,路径相对于插件数据目录 data/plugin_data/<插件名>/:

from pathlib import Path
from astrbot.api.util import get_astrbot_plugin_data_path

for rule in self.config.get("rules", []):
    for rel in rule.get("images", []):
        image_path = Path(get_astrbot_plugin_data_path()) / self.name / rel

限制:file_types 要自己声明图片扩展名,否则不会限制类型;display_item 只支持 string 字段,图片不能用作列表摘要,摘要仍需选一个文本字段。

跳过:没有验证模板嵌套 object 里的 file 上传路径是否与顶层完全一致,建议实际在 WebUI 里传一张图确认落盘目录。

风险:如果插件手写配置绕过 WebUI,file 路径必须符合 files/<配置键路径>/ 前缀,否则保存时校验会报错。

mantoujun12 avatar Nov 20 '26 16:11 mantoujun12

好的,谢谢解答!

mjy1113451 avatar Nov 20 '26 23:11 mjy1113451