Channels
Telegram Client
Plugin for openclaw to give it access to telegram from user account with rich restrictions configuration
README
# OpenClaw Telegram Client Plugin
Python wrapper around **telegram-cli** for user account operations. **No APP_ID/APP_HASH needed** โ credentials managed locally.
## Key Difference from Telethon
โ
**No APP_ID exposure** โ Each user runs telegram-cli locally
โ
**Safe to publish** โ No secrets in your code
โ
**User controls credentials** โ Telegram account security is user's responsibility
โ
**Lightweight** โ Just wraps existing telegram-cli binary
## Requirements
- **telegram-cli** installed and in PATH
```bash
# macOS
brew install telegram-cli
# Ubuntu/Debian
sudo apt-get install telegram-cli
# Build from source
git clone https://github.com/vysheng/tg.git
cd tg && ./configure && make
```
- Python 3.9+
## Installation
```bash
git clone https://github.com/yourusername/openclaw-telegram-client-plugin
cd openclaw-telegram-client-plugin
poetry install
```
## Quick Start
### 1. Verify telegram-cli Works
```bash
tg -h # Should show usage
```
### 2. Initialize Telegram Session
First time setup:
```bash
tg # Start tg interactively
# Follow prompts to authenticate
# (phone number โ code from Telegram)
# Type 'quit' to exit
```
This creates `~/.telegram-cli/` with your session.
### 3. Use via Python
```python
import asyncio
from openclaw_telegram import TelegramClient
async def main():
client = TelegramClient()
await client.connect()
# Get dialogs
dialogs = await client.get_dialogs(limit=10)
for d in dialogs:
print(d['title'])
# Send message
await client.send_message("@username", "Hello!")
await client.disconnect()
asyncio.run(main())
```
## Project Structure
```
openclaw-telegram-client-plugin/
โโโ openclaw_telegram/ # Main module
โ โโโ __init__.py # Exports: TelegramClient, Config, exceptions
โ โโโ client.py # High-level API
โ โโโ tg_cli.py # telegram-cli subprocess wrapper
โ โโโ config.py # Configuration (no APP_ID needed!)
โ โโโ cli.py # Click CLI commands
โ โโโ exceptions.py # Custom exception types
โโโ tests/ # Pytest test suite (TDD)
โ โโโ conftest.py
โ โโโ test_client.py
โ โโโ test_config.py
โโโ examples/ # Real usage examples
โ โโโ basic_usage.py
โโโ .github/workflows/ # CI/CD (GitHub Actions)
โโโ pyproject.toml # Poetry configuration
โโโ README.md # This file
โโโ QUICKSTART.md # 5-minute setup guide
โโโ CONTRIBUTING.md # Development guidelines
โโโ AGENTS.md # Agent behavior config
โโโ LICENSE (MIT)
```
## API Reference
### TelegramClient
```python
class TelegramClient:
async def connect() -> bool
async def disconnect() -> None
async def is_connected() -> bool
async def get_dialogs(limit: int = 50) -> List[Dict]
async def get_messages(peer: str, limit: int = 100) -> List[Dict]
async def send_message(peer: str, text: str) -> Dict
```
### Context Manager
```python
async with TelegramClient() as client:
dialogs = await client.get_dialogs()
# client auto-disconnects on exit
```
## Configuration
Create `.env` (optional):
```env
# Path to telegram-cli binary (if not in PATH)
TG_CLI_PATH=/usr/local/bin/tg
# Telegram-cli config directory
TG_CONFIG_DIR=~/.telegram-cli
# Your phone number
PHONE_NUMBER=+1234567890
# Logging level
LOG_LEVEL=INFO
```
## How It Works
1. **telegram-cli** (C binary) runs locally and manages Telegram connection
2. This plugin wraps tg via subprocess
3. All credentials stored locally (`~/.telegram-cli/`)
4. No APP_ID/APP_HASH in code = safe to publish
## Testing
```bash
poetry run pytest
poetry run pytest --cov=openclaw_telegram
```
## Development
```bash
# Format code
poetry run black openclaw_telegram/ tests/
# Type check
poetry run mypy openclaw_telegram/
# Lint
poetry run pylint openclaw_telegram/
# All checks
poetry run pytest && poetry run black --check . && poetry run mypy openclaw_telegram/
```
## Troubleshooting
**"telegram-cli: command not found"**
```bash
# Install telegram-cli
brew install telegram-cli # or apt-get install
# Or set custom path
export TG_CLI_PATH=/path/to/tg
```
**"Could not connect"**
```bash
# Verify tg works standalone
tg -h
# Verify session exists
ls ~/.telegram-cli/
```
**"Authentication failed"**
```bash
# Re-run telegram-cli setup
tg
# Follow prompts, then exit
```
## Security
โ
**No secret keys in code**
โ
**Credentials stored locally** in `~/.telegram-cli/`
โ
**User controls Telegram account access**
โ
**Safe to publish on GitHub**
## Related Projects
- [telegram-cli](https://github.com/vysheng/tg) โ Underlying CLI tool
- [OpenClaw](https://github.com/openclaw/openclaw) โ AI agent framework
- [OpenClaw Telegram Monitor](https://github.com/yourusername/openclaw-telegram-monitor) โ Group monitoring plugin
## License
MIT
## Author
Aleksandr ([email protected])
---
**Safe, simple, no secrets exposed.** ๐
channels
Comments
Sign in to leave a comment