Get the FREE Ultimate OpenClaw Setup Guide →

telethon-development

npx machina-cli add skill Lu1sDV/skillsmd/telethon-development --openclaw
Files (1)
SKILL.md
5.5 KB

Telethon Development Patterns

Production patterns and gotchas for the Telethon Telegram MTProto client library.

When to Use

  • Debugging FloodWaitError, account bans, or flood wait cycling
  • Mocking Telethon clients for unit/integration tests
  • Storing Telethon data in a database (boolean normalization, serialization)
  • Resolving/normalizing chat IDs before DB storage
  • Handling new or version-specific Telethon types

When NOT to Use

  • Bot API (python-telegram-bot) — different library, different patterns
  • Telethon v2 — major API changes; verify patterns still apply

Critical Gotchas

GotchaWhat BreaksFix
Boolean fields are None not FalseNOT NULL DB columnsgetattr(obj, field, False) or False
isinstance() fails with test mocksMedia/reaction type detectiontype(obj).__name__ string comparison
iter_messages() has no max_dateDate filtering silently skippedFilter post-fetch: if msg.date > max_date: continue
to_dict() returns bytes in pollsJSONB serialization crashesBase64-encode bytes before storage
New types missing in older versionsImportError at startupGuard with try/except ImportError
ReactionCount.chosen removed in 1.42AttributeErrorgetattr(r, 'chosen_order', None) is not None
get_peer_id() on non-standard objectsTypeErrortry/except, fallback getattr(entity, 'id', 0)
MessageDeleted non-channel eventschat_id is NoneAlways guard if event.chat_id is not None
Stop listener before poolDangling handler errorslistener.stop() then pool.stop()
aggressive=True on iter_participantsTriggers flood waits fasterUse ChannelParticipantsSearch("") with pagination
2FA detection is string-basedMissed 2FA promptsCatch SessionPasswordNeededError explicitly

Session Management

Always use StringSession — never file sessions. Store in env vars; treat as passwords (full account access).

from telethon import TelegramClient
from telethon.sessions import StringSession

client = TelegramClient(StringSession(os.getenv("TG_SESSION")), api_id, api_hash)
await client.connect()
if not await client.is_user_authorized():
    await client.send_code_request(phone)
    await client.sign_in(phone, code)
session_string = client.session.save()  # Save to env var after auth

FloodWaitError Handling

The raw pattern (basis for all wrappers):

except FloodWaitError as e:
    await asyncio.sleep(e.seconds)  # e.seconds = mandatory wait duration
    # then retry or rotate to next account

Three wrapper patterns — see telethon-reference.md for implementations:

PatternWhen to Use
DecoratorSingle async calls (get_entity, get_messages)
Iterator wrapperasync for loops — supports checkpoint resume on retry
Context managerComplex control flow, manual checkpoint tracking

Type Detection (Mock-Safe)

Use class name strings — works with both real Telethon objects AND test mocks:

attr_name = type(attr).__name__
if attr_name == "DocumentAttributeVideo":
    return "video_note" if getattr(attr, "round_message", False) else "video"

if type(reaction).__name__ == "ReactionEmoji":
    return reaction.emoticon

Version Guards

Always guard imports for types added in recent Telethon versions:

try:
    from telethon.tl.types import MessageMediaPaidMedia
except ImportError:
    MessageMediaPaidMedia = None

Entity Resolution & ID Normalization

from telethon.utils import get_peer_id

entity = await client.get_entity(identifier)  # str, int, or @username
normalized_id = get_peer_id(entity)            # Canonical ID for DB storage

See telethon-reference.md for channel type detection and all link format parsing.

Rate Limiting Guidelines

OperationSafe RateRisk
Messages in single chat1/secondFlood ban
Channel joins2-5s betweenAccount freeze
Participant scraping (no takeout)Don'tInstant ban
Channels scraped per day<20024h soft ban
Mass avatar downloadsDon'tBan after 3-5

Use takeout sessions for heavy participant scraping — see telethon-reference.md.

Common Errors

ErrorMeaningHandle
FloodWaitErrorRate limitedWait e.seconds, rotate account
AuthKeyUnregisteredErrorSession invalidDisable account permanently
ChannelPrivateErrorNo accessSkip, log
ChatAdminRequiredErrorNeed adminReturn empty, log
UserAlreadyParticipantErrorAlready joinedNot an error — treat as success
InviteRequestSentErrorNeeds approvalLog as pending
PhoneNumberBannedErrorAccount bannedDisable permanently

Test Mocking

See telethon-reference.md for complete patterns:

  • Mock client fixture with async generators
  • RPC call mocking (client(Request()))
  • spec= for isinstance() compatibility
  • FloodWaitError construction: FloodWaitError(request=None, capture=0.01)

Source

git clone https://github.com/Lu1sDV/skillsmd/blob/main/telethon-development/SKILL.mdView on GitHub

Overview

This skill covers practical Telethon development patterns for debugging FloodWaitError, mocking Telethon clients for tests, and persisting Telethon data. It emphasizes boolean normalization, ID resolution, and handling version-specific Telethon types to keep your apps robust.

How This Skill Works

It provides concrete code patterns, wrappers, and guards for Telethon workflows: using StringSession for sessions, guarding imports for newer types, and detecting types via class-name strings for mock safety. It also covers boolean normalization and ID normalization before DB storage, plus structured FloodWaitError handling with wrappers.

When to Use It

  • Debugging FloodWaitError, account bans, or flood wait cycling
  • Mocking Telethon clients for unit/integration tests
  • Storing Telethon data in a database (boolean normalization, serialization)
  • Resolving/normalizing chat IDs before DB storage
  • Handling new or version-specific Telethon types

Quick Start

  1. Step 1: Install Telethon and switch to StringSession storage (env vars) for all sessions
  2. Step 2: Implement session management pattern and basic is_user_authorized flow
  3. Step 3: Add guards for booleans, ID normalization, and FloodWaitError wrappers in calls

Best Practices

  • Always use StringSession for all sessions and store the value securely (env vars) as a password-like credential
  • Normalize boolean fields when persisting to the DB (e.g., getattr(obj, field, False) or False)
  • Guard imports for newer Telethon types with try/except ImportError
  • Use class-name string comparisons for mock-safe type detection across real and test objects
  • Apply FloodWaitError handling patterns (Decorator, Iterator wrapper, Context manager) to manage rate limits and retries

Example Use Cases

  • Debugging FloodWaitError in a multi-account Telethon deployment
  • Mock Telethon client in unit tests without hitting real network calls
  • Serializing Telethon data to a database with boolean normalization
  • Resolving and normalizing chat IDs with get_peer_id before DB storage
  • Guarding version-specific Telethon types at startup to avoid ImportError

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers