← Back to Plugins
Channels

Telegram Client

Polterageist By Polterageist 👁 125 views ▲ 0 votes

Plugin for openclaw to give it access to telegram from user account with rich restrictions configuration

GitHub

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

Loading comments...