telegram-telethon is a local Telegram skill and CLI built on Python and Telethon.
It is designed for operator workflows that need structured access to Telegram dialogs and history from the terminal, while keeping credentials in macOS Keychain instead of session files or shell environment variables.
The repository contains two layers:
- an agent-facing skill in
SKILL.md - a local CLI that the skill uses for deterministic Telegram operations
- a managed install layer that renders localized metadata and publishes the runtime
- bootstrap a local Telethon environment
- authenticate one or more Telegram user accounts
- store
api_id,api_hash, phone, and the TelethonStringSessionin macOS Keychain - list Telegram folders
- list dialogs across the account or within a specific folder
- export 1:1 or group chat history to
ndjsonorjson - manage multiple account labels through profile commands
- pin an active profile or override it per shell or per agent run
- macOS
python3- the macOS
securityCLI - a Telegram user account
- a Telegram
api_idandapi_hashfromhttps://my.telegram.org/apps
Install or update the managed copy:
make install LOCALE=ru-enThis does the following:
- creates or refreshes the managed runtime copy under
${XDG_DATA_HOME:-~/.local/share}/agents/skills/telegram-telethon - renders localized metadata and trigger previews from
.skill_triggers - creates
.venv/ - installs Python dependencies from
requirements.txt - installs local wrapper commands into
~/.local/bin - links the skill into
~/.claude/skills/and~/.codex/skills/
For backward compatibility, ./setup.sh still works and defaults to --locale en on first install.
Installed wrapper commands:
tg-telethontg-telethon-auth-logintg-telethon-auth-logout
Run the login helper in a real interactive shell:
./scripts/auth-login.sh --profile workThe helper prompts for:
api_idapi_hash- phone number
- Telegram login code
- Telegram 2FA password, if enabled
Where to get this information:
api_idandapi_hash: create an application athttps://my.telegram.org/apps- phone number: the Telegram account that this profile should use
- login code: Telegram sends it to the logged-in account during sign-in
- 2FA password: only if Telegram cloud password is enabled on the account
Practical recommendation for my.telegram.org/apps:
- use a clean browser session with no extensions or privacy filters on the page
- if the form behaves inconsistently, retry in a different browser profile or a browser with extensions disabled
- keep field values simple and ASCII-only
Field values that have worked well in practice:
App title:SkillTelethonShort name:tgtelethonURL:https://example.comPlatform:DesktopDescription:Local Telegram export CLI
Keep the following values in a reliable password manager:
api_idapi_hash- the Telegram phone number used for the profile
- the local profile label you assign, for example
workorpersonal
After a successful login, the tool stores credentials in macOS Keychain. No Telethon SQLite session file is used.
Check the result:
tg-telethon profiles current
tg-telethon auth statusA profile is a local label for one Telegram account session.
Examples:
workpersonalclient-aresearch
Profile selection precedence is:
--profile <name>TG_TELETHON_PROFILE=<name>- pinned active profile
default
Common profile commands:
tg-telethon profiles list --format table
tg-telethon profiles current
tg-telethon profiles use work
tg-telethon profiles rename work xflow
tg-telethon --profile personal auth statusprofiles list includes the profile label, phone identifier, readiness state, and active marker.
List folders:
tg-telethon folders list --format tableList dialogs:
tg-telethon chats list --format table
tg-telethon chats list --folder-id 247 --format table
tg-telethon chats list --folder-id 247 --archived exclude --format tableExport a chat:
tg-telethon export messages \
--chat -5118756561 \
--since 2026-02-17 \
--output /tmp/xflow.ndjsonExport formats:
--format ndjson: best for large histories--format json: single JSON array
Export schemas:
--schema minimal: compact normalized fields--schema full: includes a normalized raw Telethon payload
This is the recommended format for learning the tool and the skill. It matches how an agent should discover profile context, find dialogs, and run exports with explicit shell commands.
Inspect the active profile:
tg-telethon profiles current
tg-telethon profiles list --format tablePin a default profile for later agent runs:
tg-telethon profiles use workOverride the profile for a single shell or agent execution:
TG_TELETHON_PROFILE=personal tg-telethon chats list --format tableTypical discovery flow:
tg-telethon folders list --format table
tg-telethon chats list --folder-id 247 --format tableTypical export flow:
TG_TELETHON_PROFILE=work tg-telethon export messages \
--chat -5118756561 \
--since 2026-02-17 \
--output /tmp/xflow.ndjsonRemove only the session:
tg-telethon --profile work auth logoutRemove the entire stored profile:
tg-telethon --profile work auth logout --allOr use the helper:
./scripts/auth-logout.sh --profile work --allThe repository is intentionally small.
SKILL.md describes:
- when the skill should be used
- the standing rules for Telegram access
- the supported operator workflows
- the command patterns the agent should prefer
This is the agent-facing contract.
make install is the canonical install entrypoint.
It creates or refreshes the managed runtime copy, bootstraps .venv, publishes wrapper commands into the user environment, and refreshes the skill symlinks.
setup.sh remains as a backward-compatible wrapper.
scripts/telegram_telethon.py is the main implementation.
It provides five command groups:
profilesauthfolderschatsexport
The CLI uses:
- Telethon for Telegram access
- macOS Keychain for secrets
~/.config/telegram-telethon/state.jsonfor non-secret local state such as the pinned active profile and the known profile registry
The shell helpers are thin wrappers:
scripts/auth-login.shscripts/auth-logout.shscripts/tg-telethon
They exist to make interactive and agent-driven usage predictable.
This repository depends on the following open-source software:
- Telethon by Lonami Exo, licensed under the MIT License
The Python standard library is also used throughout the CLI implementation.
This repository is released under the MIT License. See LICENSE.
telegram-telethon/
├── .skill_triggers/
├── agents/
├── locales/
├── README.md
├── SKILL.md
├── Makefile
├── requirements.txt
├── setup.sh
├── scripts/
├── auth-login.sh
├── auth-logout.sh
├── bootstrap.sh
├── setup_main.py
├── setup_support.py
├── tg-telethon
└── telegram_telethon.py
└── tests/
- Telegram folders are filters, not exclusive containers.
- A dialog can appear in more than one folder.
- Archive is a separate scope.
- Use numeric dialog IDs for exports when possible.
Syntax checks:
bash -n scripts/bootstrap.sh
bash -n setup.sh
bash -n scripts/auth-login.sh
bash -n scripts/auth-logout.sh
python3 -m py_compile scripts/telegram_telethon.pyRe-run install after changing wrappers or dependencies:
make install LOCALE=en