asemxin commited on
Commit
cd89f2e
·
1 Parent(s): 6c10eb6

feat: add feishu image proxy + skill

Browse files
Files changed (3) hide show
  1. Dockerfile +4 -2
  2. image_proxy.py +105 -0
  3. skills/feishu_image/SKILL.md +27 -0
Dockerfile CHANGED
@@ -6,13 +6,13 @@ RUN apt-get update && apt-get install -y \
6
  && rm -rf /var/lib/apt/lists/*
7
 
8
  # Python 依赖
9
- RUN pip3 install flask psutil --break-system-packages
10
 
11
  # 安装 OpenClaw
12
  RUN npm install -g openclaw@latest
13
 
14
  # 创建目录
15
- RUN mkdir -p /root/.openclaw/workspace /root/.openclaw/extensions /root/.openclaw/credentials
16
 
17
  # 安装飞书插件
18
  RUN openclaw plugins install feishu-openclaw 2>/dev/null || true
@@ -21,6 +21,8 @@ RUN cd /root/.openclaw/extensions/feishu-openclaw && npm install @sinclair/typeb
21
  # 复制文件
22
  COPY SOUL.md /root/.openclaw/workspace/SOUL.md
23
  COPY status_page.py /app/status_page.py
 
 
24
  COPY entrypoint.sh /app/entrypoint.sh
25
  RUN chmod +x /app/entrypoint.sh
26
 
 
6
  && rm -rf /var/lib/apt/lists/*
7
 
8
  # Python 依赖
9
+ RUN pip3 install flask psutil requests --break-system-packages
10
 
11
  # 安装 OpenClaw
12
  RUN npm install -g openclaw@latest
13
 
14
  # 创建目录
15
+ RUN mkdir -p /root/.openclaw/workspace /root/.openclaw/extensions /root/.openclaw/credentials /root/.openclaw/skills
16
 
17
  # 安装飞书插件
18
  RUN openclaw plugins install feishu-openclaw 2>/dev/null || true
 
21
  # 复制文件
22
  COPY SOUL.md /root/.openclaw/workspace/SOUL.md
23
  COPY status_page.py /app/status_page.py
24
+ COPY image_proxy.py /app/image_proxy.py
25
+ COPY skills/ /root/.openclaw/skills/
26
  COPY entrypoint.sh /app/entrypoint.sh
27
  RUN chmod +x /app/entrypoint.sh
28
 
image_proxy.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ 飞书图片代理:下载飞书消息中的图片 → 上传到免费图床 → 返回可访问 URL
4
+ 用法: python3 image_proxy.py <image_key>
5
+ """
6
+ import os, sys, requests
7
+
8
+ def get_tenant_token():
9
+ """获取飞书 tenant_access_token"""
10
+ app_id = os.environ.get("FEISHU_APP_ID")
11
+ app_secret = os.environ.get("FEISHU_APP_SECRET")
12
+ if not app_id or not app_secret:
13
+ print("ERROR: FEISHU_APP_ID / FEISHU_APP_SECRET 未设置", file=sys.stderr)
14
+ sys.exit(1)
15
+ resp = requests.post(
16
+ "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal",
17
+ json={"app_id": app_id, "app_secret": app_secret}
18
+ )
19
+ data = resp.json()
20
+ if data.get("code") != 0:
21
+ print(f"ERROR: 获取 token 失败: {data}", file=sys.stderr)
22
+ sys.exit(1)
23
+ return data["tenant_access_token"]
24
+
25
+ def download_image(image_key, token):
26
+ """从飞书 API 下载图片"""
27
+ resp = requests.get(
28
+ f"https://open.feishu.cn/open-apis/im/v1/images/{image_key}",
29
+ headers={"Authorization": f"Bearer {token}"},
30
+ stream=True
31
+ )
32
+ if resp.status_code != 200:
33
+ print(f"ERROR: 下载图片失败 (HTTP {resp.status_code}): {resp.text[:200]}", file=sys.stderr)
34
+ sys.exit(1)
35
+ return resp.content
36
+
37
+ def upload_to_catbox(image_data):
38
+ """上传到 catbox.moe(免费,无需 API Key)"""
39
+ resp = requests.post(
40
+ "https://catbox.moe/user/api.php",
41
+ data={"reqtype": "fileupload"},
42
+ files={"filedata": ("image.jpg", image_data, "image/jpeg")}
43
+ )
44
+ if resp.status_code == 200 and resp.text.startswith("http"):
45
+ return resp.text.strip()
46
+ return None
47
+
48
+ def upload_to_0x0(image_data):
49
+ """备用:上传到 0x0.st"""
50
+ resp = requests.post(
51
+ "https://0x0.st",
52
+ files={"file": ("image.jpg", image_data, "image/jpeg")}
53
+ )
54
+ if resp.status_code == 200 and resp.text.startswith("http"):
55
+ return resp.text.strip()
56
+ return None
57
+
58
+ def upload_to_tmpfiles(image_data):
59
+ """备用:上传到 tmpfiles.org"""
60
+ resp = requests.post(
61
+ "https://tmpfiles.org/api/v1/upload",
62
+ files={"file": ("image.jpg", image_data, "image/jpeg")}
63
+ )
64
+ if resp.status_code == 200:
65
+ data = resp.json()
66
+ url = data.get("data", {}).get("url", "")
67
+ # tmpfiles.org 返回的 URL 需要加 /dl/ 才能直接访问
68
+ if url:
69
+ return url.replace("tmpfiles.org/", "tmpfiles.org/dl/")
70
+ return None
71
+
72
+ if __name__ == "__main__":
73
+ if len(sys.argv) < 2:
74
+ print("用法: python3 image_proxy.py <image_key>")
75
+ print(" 从飞书下载图片并上传到免费图床,返回可访问的 URL")
76
+ sys.exit(1)
77
+
78
+ image_key = sys.argv[1]
79
+ print(f"📥 正在下载飞书图片: {image_key}", file=sys.stderr)
80
+
81
+ token = get_tenant_token()
82
+ image_data = download_image(image_key, token)
83
+ print(f"📥 下载成功 ({len(image_data)} bytes)", file=sys.stderr)
84
+
85
+ # 依次尝试多个图床
86
+ for name, uploader in [("catbox.moe", upload_to_catbox),
87
+ ("0x0.st", upload_to_0x0),
88
+ ("tmpfiles.org", upload_to_tmpfiles)]:
89
+ print(f"📤 正在上传到 {name}...", file=sys.stderr)
90
+ try:
91
+ url = uploader(image_data)
92
+ if url:
93
+ print(f"✅ 上传成功: {url}", file=sys.stderr)
94
+ print(url) # stdout 只输出 URL,供 agent 捕获
95
+ sys.exit(0)
96
+ except Exception as e:
97
+ print(f"⚠️ {name} 上传失败: {e}", file=sys.stderr)
98
+
99
+ # 所有图床都失败,保存到本地
100
+ local_path = f"/tmp/{image_key}.jpg"
101
+ with open(local_path, "wb") as f:
102
+ f.write(image_data)
103
+ print(f"⚠️ 所有图床失败,已保存到本地: {local_path}", file=sys.stderr)
104
+ print(local_path)
105
+ sys.exit(1)
skills/feishu_image/SKILL.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 飞书图片分析技能
2
+
3
+ 当用户在飞书发送图片消息时,你会收到包含 `image_key` 的消息(格式类似 `img_v3_xxxx`)。
4
+
5
+ ## 处理流程
6
+
7
+ 1. **提取 image_key**: 从消息中找到图片的 image_key(通常是 `img_v3_` 开头的字符串)
8
+ 2. **运行代理脚本**: 执行以下命令下载图片并获取可访问的 URL:
9
+ ```bash
10
+ python3 /app/image_proxy.py <image_key>
11
+ ```
12
+ 3. **分析图片**: 使用返回的 URL,通过 `web_fetch` 工具获取图片内容并进行分析
13
+
14
+ ## 示例
15
+
16
+ 用户发送了一张图片,你看到消息中包含 `img_v3_0285_abc123`:
17
+
18
+ ```bash
19
+ python3 /app/image_proxy.py img_v3_0285_abc123
20
+ ```
21
+
22
+ 脚本会输出图片的公开 URL(如 `https://files.catbox.moe/abc123.jpg`),然后你就可以基于这个 URL 分析图片内容。
23
+
24
+ ## 注意
25
+ - 脚本会自动尝试多个免费图床(catbox.moe → 0x0.st → tmpfiles.org)
26
+ - 如果所有图床都失败,图片会保存到本地 `/tmp/` 目录
27
+ - image_key 只有在 tenant_access_token 有效期内才能下载