name: feishu-doc-manager description: | 飛書文件管理技能。支援建立、讀取、寫入、刪除文件和文件塊。當用戶需要操作飛書文件時使用此技能。觸發條件:(1) 建立飛書文件 (2) 讀取飛書文件內容 (3) 寫入/追加內容到飛書文件 (4) 刪除文件塊 (5) 清空文件。
完整的飛書文件操作技能,支援建立、讀取、寫入、刪除文件。
在飛書開放平臺開通以下許可權:
| 許可權標識 | 許可權名稱 | 用途 |
|---|---|---|
docx:document |
獲取、新建、刪除文件 | 建立/刪除文件 |
docx:document:write_only |
編輯文件 | 寫入/刪除塊 |
docx:document:readonly |
讀取文件 | 讀取內容 |
docs:permission.member |
文件許可權管理 | 協作者管理 |
在飛書客戶端: 1. 開啟文件 → 右上角 "..." 2. 更多 → 新增文件應用 3. 搜尋應用 → 新增,授予 "可編輯" 許可權
$config = Get-Content "$env:USERPROFILE\.openclaw\openclaw.json" | ConvertFrom-Json
$appId = $config.channels.feishu.appId
$appSecret = $config.channels.feishu.appSecret
$body = @{ app_id = $appId; app_secret = $appSecret } | ConvertTo-Json
$resp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" -Method Post -Body $body -ContentType "application/json"
$token = $resp.tenant_access_token
$createBody = '{"title":"文件標題","folder_token":""}'
$createResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents" -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($createBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" }
$documentId = $createResp.data.document.document_id
Write-Host "文件 ID: $documentId"
Write-Host "文件連結: https://xxx.feishu.cn/docx/$documentId"
# 獲取文件所有塊
$blocksResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Get -Headers @{ Authorization = "Bearer $token" }
foreach ($block in $blocksResp.data.items) {
Write-Host "型別: $($block.block_type), ID: $($block.block_id)"
}
# 寫入標題和文本
$writeBody = '{"index":-1,"children":[{"block_type":3,"heading1":{"elements":[{"text_run":{"content":"標題"}}]}},{"block_type":2,"text":{"elements":[{"text_run":{"content":"文本內容"}}]}}]}'
$writeResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($writeBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" }
塊型別說明:
| block_type | 型別 | 說明 |
|---|---|---|
| 2 | text | 普通文本 |
| 3 | heading1 | 一級標題 |
| 4 | heading2 | 二級標題 |
| 12 | bullet | 無序列表 |
使用 batch_delete API 批次刪除:
# 獲取當前塊數量
$blocksResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Get -Headers @{ Authorization = "Bearer $token" }
$totalBlocks = $blocksResp.data.items.Count
# 刪除所有塊(左閉右開區間)
$deleteBody = @{
start_index = 0
end_index = $totalBlocks
} | ConvertTo-Json
$delResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children/batch_delete?document_revision_id=-1" -Method Delete -Body ([System.Text.Encoding]::UTF8.GetBytes($deleteBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" }
if ($delResp.code -eq 0) {
Write-Host "✅ 刪除成功"
}
API 說明:
- URL: DELETE /documents/:document_id/blocks/:block_id/children/batch_delete
- 引數: start_index(起始索引)、end_index(結束索引,左閉右開)
- 注意: start_index 必須小於 end_index
# 1. 獲取塊數量
$blocksResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Get -Headers @{ Authorization = "Bearer $token" }
$totalBlocks = $blocksResp.data.items.Count
# 2. 刪除所有塊
if ($totalBlocks -gt 0) {
$deleteBody = @{ start_index = 0; end_index = $totalBlocks } | ConvertTo-Json
Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children/batch_delete?document_revision_id=-1" -Method Delete -Body ([System.Text.Encoding]::UTF8.GetBytes($deleteBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" } | Out-Null
}
# 3. 寫入新內容
$writeBody = '{"index":-1,"children":[{"block_type":3,"heading1":{"elements":[{"text_run":{"content":"新標題"}}]}},{"block_type":2,"text":{"elements":[{"text_run":{"content":"新內容"}}]}}]}'
Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($writeBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" }
Write-Host "✅ 文件已更新"
Wiki 文件需要先獲取實際的 obj_token:
# Wiki URL: https://xxx.feishu.cn/wiki/XXXXXXXX
$wikiToken = "SRDiwPCx8iEtx6kreaCckFW3nOb"
# 獲取實際文件 ID
$wikiResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/wiki/v2/spaces/get_node?token=$wikiToken" -Method Get -Headers @{ Authorization = "Bearer $token" }
$documentId = $wikiResp.data.node.obj_token
Write-Host "實際文件 ID: $documentId"
| 錯誤碼 | 說明 | 解決方案 |
|---|---|---|
| 1770032 | 許可權不足 | 新增應用為文件協作者 |
| 1770002 | 文件不存在 | 檢查 document_id |
| 9499 | JSON 格式錯誤 | 使用純 JSON 字串 |
# 配置 $config = Get-Content "$env:USERPROFILE\.openclaw\openclaw.json" | ConvertFrom-Json $appId = $config.channels.feishu.appId $appSecret = $config.channels.feishu.appSecret # 獲取 Token $body = @{ app_id = $appId; app_secret = $appSecret } | ConvertTo-Json $resp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" -Method Post -Body $body -ContentType "application/json" $token = $resp.tenant_access_token $documentId = "你的文件ID" # 獲取天氣 $weather = curl.exe -s "wttr.in/Tianjin?format=3" $timeStr = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' # 清空文件 $blocksResp = Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Get -Headers @{ Authorization = "Bearer $token" } if ($blocksResp.data.items.Count -gt 0) { $deleteBody = @{ start_index = 0; end_index = $blocksResp.data.items.Count } | ConvertTo-Json Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children/batch_delete?document_revision_id=-1" -Method Delete -Body ([System.Text.Encoding]::UTF8.GetBytes($deleteBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" } | Out-Null } # 寫入天氣 $writeBody = '{"index":-1,"children":[{"block_type":3,"heading1":{"elements":[{"text_run":{"content":"天津天氣"}}]}},{"block_type":2,"text":{"elements":[{"text_run":{"content":"' + $weather + '"}}]}},{"block_type":2,"text":{"elements":[{"text_run":{"content":"更新時間: ' + $timeStr + '"}}]}}]}' Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/docx/v1/documents/$documentId/blocks/$documentId/children" -Method Post -Body ([System.Text.Encoding]::UTF8.GetBytes($writeBody)) -ContentType "application/json; charset=utf-8" -Headers @{ Authorization = "Bearer $token" } Write-Host "✅ 天氣已更新"來源於7w4.net。
這個技能功能比較全面,能滿足基礎的飛書文件管理需求,文件寫得很詳細,配有使用示例和錯誤說明,小白也能看懂配置步驟。主要問題是隻有使用說明沒有現成工具,每次用都要自己寫程式碼;而且不支援表格、圖片等複雜內容的處理,讀取文件也只能看到結構框架,無法直接提取文字內容。總體來說適合有 PowerShell 基礎的使用者拿來參考,普通人直接用會比較吃力。