Telegram File Browser
by @just-cj
Build or improve Telegram inline-button file browsers and menu-style navigators. Use when creating Telegram chat UIs for browsing directories, paging lists,...
clawhub install telegram-file-browser๐ About This Skill
name: telegram-file-browser description: Build or improve Telegram inline-button file browsers and menu-style navigators. Use when creating Telegram chat UIs for browsing directories, paging lists, previewing files, returning to parent views, closing menus, or exposing copyable file paths. Especially useful when the interaction should stay inside one message via button state updates instead of spamming new messages.
> โ ๏ธ ๐จ CRITICAL BUTTON STRUCTURE WARNING ๐จ
>
> The buttons array from the script is a 2D array where each inner array = one row.
> ALWAYS pass it directly to the message tool without modification.
>
> - In OpenClaw context: use response['buttons'] directly
> - In Python scripts: use payload['buttons'] from build_message_payload()
>
> โ NEVER flatten, restructure, re-group, or stringify rows.
Zero-Footgun Protocol
When sending any browser menu, follow the correct workflow for your context:
If in OpenClaw (using exec + message tools):
1. Run browser_dispatcher.py via exec tool
2. Use response['messageToolCall'] as the complete payload for the message tool
3. Don't touch the buttons โ use them exactly as returned inside that payload
4. If send succeeds and the response includes postSend, then update liveMessageId and optionally delete the previous menu
If in standalone Python script:
1. Generate plan via run_browser_action.py or import functions
2. Run build_message_payload(plan) and require ok: true
3. Use payload['message'] and payload['buttons'] from the validated result
Common mistakes to avoid:
Telegram File Browser
Build Telegram file-browsing flows around a single live menu plus optional side messages for previews, paths, and downloads.
Invocation
When the user explicitly invokes telegram-file-browser or asks to use this skill:
1. Open an interactive browser immediately in the same turn.
2. Default the root to ~/.openclaw/workspace unless the user gave a path.
3. Do not reply with a conversational clarification like โไฝ ๆณ่ฎฉๆๅไปไน๏ผโ when the user already asked to browse.
4. Ask a follow-up only when the target path or safety boundary is genuinely ambiguous.
5. If the input is config, open the configuration flow instead of the browser tree.
Do not stop at describing the skill when you already have enough information to launch the UI.
Runtime Contexts
This skill can be used in two different contexts. Choose the one that matches your environment.
Context 1: OpenClaw Tool Calls (Most Common)
When you're running inside OpenClaw and using exec + message tools (like right now), use browser_dispatcher.py via exec, then call the message tool with the exact JSON from the script.
> โ ๏ธ The script already validates and returns the correct payload โ use it directly, don't rewrite anything.
Step 1: Run the script via exec tool
# Run the dispatcher to get the final message-tool payload
result = exec(command="python3 ~/.openclaw/workspace/skills/telegram-file-browser/scripts/browser_dispatcher.py open-root")
response = json.loads(result) # response contains messageToolCall, postSend, and compatibility fields
Step 2: Call message tool with the script's output
# โ
CORRECT: pass the dispatcher payload through directly
message(**response['messageToolCall'])โ WRONG: don't rewrite buttons yourself
message(
action='send',
message=response['message'],
buttons=[[{...}, {...}]]
)โ WRONG: don't stringify the payload either
message(action='send', message=response['message'], buttons=json.dumps(response['buttons']))
Complete example:
# 1. Get the complete message-tool payload
result = exec(command="python3 ~/.openclaw/workspace/skills/telegram-file-browser/scripts/browser_dispatcher.py open-root")
response = json.loads(result.stdout)if response['ok'] and response.get('messageToolCall'):
# 2. Send exactly what the dispatcher returned
msg_result = message(**response['messageToolCall'])
# 3. Update liveMessageId if requested
post_send = response.get('postSend') or {}
if msg_result.get('messageId') and post_send.get('updateLiveMessageId'):
exec(command="python3 ~/.openclaw/workspace/skills/telegram-file-browser/scripts/browser_dispatcher.py update-message-id " + str(msg_result['messageId']))
# 4. Delete the previous live menu only after the new one succeeds
previous_id = post_send.get('previousMessageId')
if previous_id and post_send.get('cleanupPreviousMessage'):
message(action='delete', messageId=previous_id)
Callback handling:
# 1. Handle callback
result = exec(command="python3 ~/.openclaw/workspace/skills/telegram-file-browser/scripts/browser_dispatcher.py handle-callback " + callback_data)
response = json.loads(result.stdout)2. Execute the exact tool payload returned by the script
if response.get('messageToolCall'):
msg_result = message(**response['messageToolCall']) post_send = response.get('postSend') or {}
if msg_result.get('messageId') and post_send.get('updateLiveMessageId'):
exec(command="python3 ~/.openclaw/workspace/skills/telegram-file-browser/scripts/browser_dispatcher.py update-message-id " + str(msg_result['messageId']))
previous_id = post_send.get('previousMessageId')
if previous_id and post_send.get('cleanupPreviousMessage'):
message(action='delete', messageId=previous_id)
Important for current Telegram/OpenClaw routing:
If a button click arrives as a plain inbound text message like tfb_root_v12_w8 instead of a native callback event, treat that text as the callback_data and run the exact same callback flow above. Do not ignore it just because it came in as a message.
Recommended detection rule:
^tfb_(root|dir|preview|path|download|back|close)_browser_dispatcher.py handle-callback immediatelyContext 2: Standalone Python Script
When you're writing a standalone Python script (not inside OpenClaw), use the import-based approach with build_message_payload.
import sys
sys.path.insert(0, '~/.openclaw/workspace/skills/telegram-file-browser/scripts')
from run_browser_action import open_root, handle_callback
from send_plan import build_message_payloadSTATE = '~/.openclaw/workspace/.openclaw/telegram-file-browser/state.json'
ROOT = '~/.openclaw/workspace'
def send_browser_plan(plan):
"""Send message using OpenClaw's message tool or Telegram API."""
if plan['toolAction'] == 'noop':
return None
if plan['toolAction'] == 'delete':
return {"action": "delete", "messageId": plan['messageId']}
if plan['toolAction'] == 'send-file':
return {"action": "send", "path": plan['path'], "caption": plan.get('caption')}
# MUST validate through build_message_payload
wrapped = build_message_payload(plan)
if not wrapped['ok']:
raise RuntimeError(wrapped['error'])
payload = wrapped['payload']
return {
"action": payload['action'],
"message": payload['message'],
"buttons": payload['buttons'],
"replyTo": payload.get('replyTo')
}
First open
send_browser_plan(open_root(STATE, ROOT))Handle callback
send_browser_plan(handle_callback(STATE, callback_data))
Why build_message_payload is required in Python scripts:
Why it's NOT needed in OpenClaw context:
browser_dispatcher.py already returns a validated payload> โ ๏ธ For OpenClaw tool calls: Just use browser_dispatcher.py โ see Context 1 above.
>
> The examples below are for standalone Python scripts only.
Tool Plan Contract
In OpenClaw context (most common): Use browser_dispatcher.py โ it already validates and returns the correct payload. Just use the JSON values directly.
In standalone Python scripts: Use run_browser_action.py + build_message_payload(plan) as the guardrail.
Supported toolAction values:
Plans that send buttons should also include viewType (for example directory or file-actions) so the validator can enforce row-shape rules.
senddeletesend-filenoopMap them to OpenClaw message actions like this:
sendbrowser_dispatcher.py โ the response already contains validated message and buttons. Just pass them to the message tool:
message(action='send', message=response['message'], buttons=response['buttons'])
- In Python scripts: Run build_message_payload(plan) first, then use:
payload = wrapped["payload"]
message(action=payload["action"], message=payload["message"], buttons=payload["buttons"])
- Pass buttons as a real 2D array (each inner array = one row)
- Do not stringify or flatten buttonsโ ๏ธ Common mistakes to avoid:
# โ WRONG โ flatten all buttons into one row
buttons = [[{...item1...}, {...item2...}, {...item3...}]]โ WRONG โ stringify buttons
buttons = json.dumps(response['buttons'])โ
CORRECT โ use script output directly
buttons = response['buttons']
deletemessage action=delete
- pass messageIdsend-filemessage action=send
- pass path or filePath
- pass caption
- if replyTo is present, pass it as replyTonoopImportant runtime note:
send, not on edit.edit-message mode as replace the prior menu with minimal chat noise, not as a literal button-preserving in-place edit.Display Modes
Persist display mode in config.json.
edit-message
edit, implement this as:liveMessageId
3. delete the previous menu after send succeeds
new-message
liveMessageId to the newly sent menu after successCore Interaction Rules
Keep callback payloads short
Never place full file paths in callback_data.
Use opaque ids and store the real mapping in state.
Split navigation from content
Use the menu for navigation only.
Use separate messages for:
File click behavior
When a user clicks a file from a directory listing:
1. Open a file action menu.
2. Do not immediately dump file contents.
3. Offer at least ๐ ้ข่ง, ๐ ่ทฏๅพ, โฌ๏ธ ไธ่ฝฝ, โฌ
๏ธ ่ฟๅ, โ ๅ
ณ้ญ.
4. Prefer one button per row here as well unless a later UX change explicitly says otherwise.
Pagination
Keep pagination inside the same state machine.
Required behavior:
State Requirements
Persist at least:
rootcurrentstackliveMessageIdmenuVersionviewsRecommended shape:
{
"root": "/abs/root",
"current": "/abs/root/subdir",
"stack": ["/abs/root"],
"liveMessageId": "2317",
"menuVersion": 4,
"views": {
"/abs/root/subdir": {
"path": "/abs/root/subdir",
"page": 2,
"pageSize": 12,
"items": [
{ "id": "d12313", "name": "demo.py", "path": "/abs/root/subdir/demo.py", "type": "file" }
]
}
}
}
Callback Safety
Protect against replayed and stale callbacks.
Required behavior:
noop over noisy execution for stale callbacksFailure Handling
Missing or stale target
If the resolved file or directory no longer exists:
Callback execution loop
When Telegram delivers a button click as a callback message such as callback_data: tfb_root_v2_w13, do this immediately:
1. run scripts/run_browser_action.py handle-callback
2. inspect the returned tool plan
3. if toolAction == "send", run it through send_plan.py / build_message_payload(plan) first
4. execute the validated payload with the OpenClaw message tool
5. if a fresh menu was sent successfully, write the returned messageId back into state as liveMessageId
6. if the plan requests previous-menu cleanup, delete the previous menu only after the new menu send succeeds
Do not answer a callback message with a normal conversational reply when the callback belongs to the file browser.
Do not ask a follow-up unless the callback cannot be resolved safely.
Prefer silent noop over chatty recovery for stale callbacks.
Message edit failure
If an edit fails because the menu no longer exists or cannot be edited:
liveMessageIdOversized or invalid callbacks
If Telegram rejects callback payloads:
Bundled Scripts
scripts/build_view.py โ list a directory, sort entries, paginate, and emit view JSONscripts/file_browser_state.py โ initialize/load/save state and manage current path, back stack, menu version, and live message idscripts/preview_file.py โ generate safe file previewsscripts/render_buttons.py โ build Telegram button matrices for directory and file-action viewsscripts/resolve_callback.py โ resolve callback payloads into browser actionsscripts/browser_controller.py โ orchestrate open-root, open-dir, file actions, back, paging, and live-message state updatesscripts/browser_config.py โ load and persist display configscripts/run_browser_action.py โ convert browser actions into concrete tool plans for messaging and file deliveryscripts/send_plan.py โ validate send plans and build the only approved message(action="send", ...) payloadscripts/validate_buttons.py โ CLI validator for plan JSON before sendingscripts/test_buttons_integrity.py โ regression test for root/page/dir/file-action flows and flattened-button rejectionscripts/browser_dispatcher.py โ โญ RECOMMENDED one-click wrapper: generates plan, validates, returns exact payload. Use this instead of hand-rolling the flow.Runtime artifacts
Treat state/ as runtime-only scratch data. Do not commit it. Keep it ignored in git.
Pre-send Checklist
Before sending or replacing a browser menu, verify all of the following:
In OpenClaw context (using exec + message tools):
browser_dispatcher.py to get the planresponse['buttons'] directly to the message tool โ no manual rewritingbuttons is a real 2D array (each inner array = one row)In standalone Python scripts:
build_message_payload(plan) to validatepayload['buttons'] from the validated resultGeneral rules (always):
replyTo is forwarded when the tool plan includes itliveMessageId is updatedHard rule
Treat any manual rewrite of plan["buttons"] as a bug, not an optimization.
Notes
Read references/interaction-patterns.md when you need concrete UX guidance for:
๐ Tips & Best Practices
Read references/interaction-patterns.md when you need concrete UX guidance for: