| name | telegram-cli |
| description | Python CLI wrapper for Telegram interaction using Telethon (MTProto protocol).
Use when the user wants to: (1) send messages, (2) read messages from chats/channels/groups,
(3) list chats, (4) download files, or (5) search messages.
On first use, guide the user through authentication (API ID/Hash โ phone โ code โ 2FA password).
Store session and config in ~/.libragent/telegram_config.json.
Subsequent requests use the stored session without re-authentication.
Triggers on requests like: "ํ
๋ ๊ทธ๋จ ๋ฉ์์ง ๋ณด๋ด์ค", "ํ
๋ ๊ทธ๋จ ํ์ธ", "send telegram", "ํ
๋ ๊ทธ๋จ ์ฑ๋ ํ์ธ", "ํ
๋ ๊ทธ๋จ ํ์ผ ๋ค์ด๋ก๋".
|
Telegram CLI Skill
Enables the agent to interact with Telegram on behalf of the user via Telethon (MTProto protocol).
Path conventions
Paths in this skill are relative to the directory containing this SKILL.md, not to the workspace root or the shell's current ./.
- Scripts in this skill use paths like
scripts/...
- When a command below says
python scripts/..., resolve that script path against the skill's absolute Base Directory
- On Linux/macOS, use
python3 if python is unavailable
- In command examples below, replace
<skill-base-dir> with the skill's actual absolute Base Directory
โ ๏ธ Security Rules (Mandatory)
- NEVER ask for 2FA password, verification codes, or other transient secrets in chat
- NEVER display or repeat the contents of
~/.libragent/telegram_config.json
- Collect transient secrets (verification codes, 2FA passwords) exclusively through
requireUserInput=true shell prompts โ never in chat
- For verification codes and 2FA, prefer
requireUserInput=true with python setup.py --code-stdin / --password-stdin (LibrAgent auto-pipes UI input to child stdin on Windows when these flags are present)
- ALWAYS use
setup.py to persist credentials and session
- If the user accidentally pastes a password or code in chat, acknowledge receipt, do NOT echo it back, and immediately run setup to store it properly
Overview
Telegram integration involves these steps:
- Detect config โ run
check_config.py to check if account is configured
- Setup (first time only) โ gather API ID/Hash, phone number, then run
setup.py with hidden prompts for code/password
- Dispatch action โ classify the user's request and call
telegram_cli.py with the right action
- Present results โ format and summarize the output for the user
Dependencies
Required dependencies (should already be installed):
- telethon:
pip3 install telethon (MTProto client library)
References
These reference files are located in the skill directory:
Step 1: Detect Config
Always start by checking if the account is configured:
python "<skill-base-dir>/scripts/check_config.py"
- Exit code
0 with "status": "ok" โ configured and authorized, proceed to Step 3
- Exit code
1 โ missing config/session or not authorized (missing, missing_session, unauthorized, auth_restart_needed), go to Step 2
- Exit code
2 โ config exists but is incomplete/corrupt, go to Step 2 to reconfigure/overwrite it
"status": "auth_restart_needed" โ delete ~/.libragent/telegram_session.session and restart from Step A (send_code)
Step 2: Account Setup (First Time or Reset)
Do not run python "<skill-base-dir>/scripts/setup.py" bare inside LibrAgent. That old terminal wizard asks for multiple prompts and can time out under the current prompt-resume shell contract.
Instead:
2.1: Guide API ID/Hash Acquisition
If the user doesn't have API credentials, guide them to:
- Visit https://my.telegram.org
- Log in with their phone number
- Click "API development tools"
- Fill in App title and Short name (can be anything)
- Copy the
api_id and api_hash
2.2: Two-Step Authentication Flow
1. Collect API credentials in chat
Ask the user for:
api_id (integer)
api_hash (string)
phone (international format, e.g., +821012345678)
2. Interactive setup commands
Step A โ Send verification code:
python "<skill-base-dir>/scripts/setup.py" `
--api-id 12345678 `
--api-hash "abcdef0123456789..." `
--phone "+821012345678" `
--action send_code
This outputs a JSON confirmation that the code was sent. On AuthRestartError, the script clears the partial session and retries once. If it still fails, you get "status": "auth_restart_needed" โ run send_code again.
Step B โ Sign in with verification code:
Execute workspace__runInPersistentShell (or workspace__runInPersistentPowerShell) with:
LibrAgent auto-detects --code-stdin and pipes the UI input into Python stdin (stdinDelivery=child).
Fallback if piping is unavailable on an older build:
$code = Read-Host; python "<skill-base-dir>/scripts/setup.py" --action sign_in --code-value $code
Step C โ Handle 2FA (if Step B returns "password_needed"):
After successful setup, re-run check_config.py to confirm "status": "ok", then proceed to Step 3.
Step 3: Dispatch Action
Classify the user's request into one of six actions and call telegram_cli.py.
Output handling (recommended for message/search actions)
On Windows, large JSON payloads can break in PowerShell (cp949). Prefer saving results to a UTF-8 file:
python "<skill-base-dir>/scripts/telegram_cli.py" --action get_messages `
--chat "<chat_id_or_username>" `
--limit 50 `
--output "<workspace>/telegram_messages.json"
When --output is set, stdout prints a compact summary (status, output path, count). Read the file for full message bodies.
All CLI output uses UTF-8 (ensure_ascii=False). Errors go to stderr as UTF-8 JSON.
Output handling (Windows ํ์)
On Windows, always set these before invoking any CLI command:
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$env:PYTHONUTF8 = "1"
Input encoding (Windows / Unicode workaround)
On Windows, passing Unicode characters (like Korean) as command-line arguments can cause character corruption (mojibake) due to PowerShell's default encoding (cp949).
To bypass this bottleneck, it is highly recommended to write the message or query to a UTF-8 file and use --message-file or --query-file instead of --message or --query (especially on older Windows PowerShell environments; PowerShell 7+ with $OutputEncoding set to UTF-8 may work with inline arguments):
Send Message (Windows Recommended)
# 1. Write the message to a UTF-8 file in the workspace
$msg = "์๋
ํ์ธ์, ํ
๋ ๊ทธ๋จ ๋ฉ์์ง ํ
์คํธ์
๋๋ค."
$msg | Out-File -Encoding utf8 "<workspace>/tg_message.txt"
# 2. Call the CLI with --message-file
python "<skill-base-dir>/scripts/telegram_cli.py" --action send_message `
--chat "<chat_id_or_username>" `
--message-file "<workspace>/tg_message.txt"
# 3. Clean up the temporary file (recommended)
Remove-Item -Path "<workspace>/tg_message.txt" -ErrorAction SilentlyContinue
Search Messages (Windows Recommended)
# 1. Write the query to a UTF-8 file in the workspace
$query = "ํ
์คํธ"
$query | Out-File -Encoding utf8 "<workspace>/tg_query.txt"
# 2. Call the CLI with --query-file
python "<skill-base-dir>/scripts/telegram_cli.py" --action search_messages `
--query-file "<workspace>/tg_query.txt" `
--output "<workspace>/telegram_search.json"
# 3. Clean up the temporary file (recommended)
Remove-Item -Path "<workspace>/tg_query.txt" -ErrorAction SilentlyContinue
Dispatch CLI Actions
Classify the user's request into one of the actions (send_message, get_messages, list_chats, search_messages, download_file, get_chat_info) and execute the CLI. For a detailed reference of CLI parameters, JSON output schemas, and pagination strategies, see the cli-reference.md guide.
Step 4: Present Results
- Message list: Show as a numbered table โ
# From Date Content
- Send confirmation: Confirm action completed with brief summary
- Chat list: Show as a table โ
# Name Type Members/ID Last Activity
- Search results: Same as message list, with match count
- Errors: See Error Handling in cli-reference.md
Always provide the next concrete step, never just report the error.
Output Format Guidelines
For message listings, use this format:
๐จ ํ
๋ ๊ทธ๋จ ๋ฉ์์ง (chat: @example_channel)
# ๋ ์ง ๋ด์ฉ
1 06/01 14:23 ์ค๋ ํ์๋ 14์์ ์์๋ฉ๋๋ค.
2 06/01 13:45 [์ด๋ฏธ์ง]
3 06/01 12:00 ์๋ก์ด ๊ธฐ๋ฅ ๋ฐฐํฌ ์๋ฃ
๋ ๋ณด๋ ค๋ฉด: "๋ค์ 20๊ฐ ๋ณด์ฌ์ค"
For send confirmation:
โ
ํ
๋ ๊ทธ๋จ ๋ฉ์์ง ๋ฐ์ก ์๋ฃ
๋ฐ๋ ๊ณณ: @example_channel
๋ฐ์ก ์๊ฐ: 2026-06-01 14:30
For chat listings:
๐ฌ ํ
๋ ๊ทธ๋จ ์ฑํ
๋ชฉ๋ก (์ด 25๊ฐ)
# ์ด๋ฆ ์ ํ ๋ง์ง๋ง ํ๋
1 โ LibrAgent Dev ์ฑ๋ 10๋ถ ์
2 GitHub Notifications ์ฑ๋ 1์๊ฐ ์
3 โ ํ๋ก์ ํธ A ๊ทธ๋ฃน(42) 3์๊ฐ ์
4 ๊น์ฒ ์ ๊ฐ์ธ ์ด์
โ = ์ฝ์ง ์์