Files
autoreg-skills-max/skills/telegram-automation/references/mcp-fallback-direct-telethon.md

5.6 KiB

MCP Fallback: Direct Telethon When [REDACTED-NAME] MCP Is Down

Problem

The [REDACTED-NAME] MCP returns:

ERROR: Telethon not available or client not connected

This happens when the Telethon client the MCP server wraps isn't authenticated or the MCP process itself is down. All MCP tools (reform_messages, reform_dialogs, reform_me, reform_resolve, reform_search) fail with the same error.

Direct Telethon Bypass

The MCP server wraps a local SQLite session file. You can bypass the MCP entirely by connecting directly with the same session:

from telethon import TelegramClient
from telethon.sessions import SQLiteSession

SESSION_PATH = r"C:\Users\User\Desktop\_PROJECTS\tg_content_agent\MAINTG1_fixed.session"
API_ID = 2040
API_HASH = "[REDACTED-KEY]"

async def main():
    client = TelegramClient(SQLiteSession(SESSION_PATH), API_ID, API_HASH)
    try:
        await asyncio.wait_for(client.start(), timeout=30)
        me = await client.get_me()
        print(f"Connected: @{me.username} (id={me.id})")
        
        # Get dialogs (same as reform_dialogs)
        dialogs = await client.get_dialogs(limit=50)
        
        # Get messages from a chat (same as reform_messages)
        msgs = await client.get_messages(chat_id, limit=30)
        
        # Resolve username (same as reform_resolve)
        entity = await client.get_entity(username)
        
    except asyncio.TimeoutError:
        print("ERROR: Connection timed out — session may be expired")
    finally:
        await client.disconnect()

Session Details

  • Session file: MAINTG1_fixed.session (~630 KB, active)
  • Account: @[REDACTED-NAME] (id=[REDACTED-TGID])
  • API credentials: id=2040, hash [REDACTED-KEY]
  • Auth flow: Session is pre-authenticated — no phone/OTP needed

Pitfalls

  • Session can expire — if client.start() times out (30s+), the auth keys may be stale. Requires re-authentication via phone code.
  • Write_file restrictions — skill_manage(write_file) can write reference/template/script files into existing skills without file-system permission issues. Use this instead of write_file for session-sensitive scripts.
  • No MCP tools available — direct Telethon doesn't provide the convenience wrappers (reform_search, etc.). You have to implement search manually via client.get_messages() iteration.
  • Timeouts — TelegramClient.start() with a large session file can take 10-20s. Set asyncio.wait_for(client.start(), timeout=30).
  • Race with MCP — if both direct client and MCP connect simultaneously with the same session file, one will get Database is locked. Don't overlap.

Typical Cron Job Adaptation

When the cron job's MCP dependency is down, the full flow is:

  1. Detect — call MCP tool, catch error
  2. Fall back — connect via direct TelegramClient(SQLiteSession(...))
  3. Scan — iterate dialogs, get messages per chat
  4. Classify — regex-based AI intel categorization (see below)
  5. Dedup — hash message text, compare with state file
  6. Report — format digest, deliver via cron output contract

AI Intel Classification Pattern

For parsing Telegram messages for AI intelligence:

import re
from collections import defaultdict

CATEGORIES = {
    "api_keys": r"sk-[a-zA-Z0-9]{20,}|Bearer\s+[a-zA-Z0-9_\-\.]{30,}|api.?key[:\s][a-zA-Z0-9]{16,}",
    "jailbreak": r"jailbreak|bypass|prompt inj|uncensored|g0dm0d3|refusal",
    "ai_abuse": r"free api|free key|0元|unlimited|turbo charge",
    "ai_news": r"gpt-?5|claude|opus|sonnet|deepseek|grok|qwen|gemini|release|launch|new model",
    "traffic": r"traffic|seo|seeding|guerrilla|growth hack|virality",
    "funnels": r"funnel|lead gen|conversion|cpa|cpl|autofunnel|landing",
    "code_tips": r"claude code|codex|opencode|cursor|mcp|agent skill|workflow|template",
}

def classify(text: str) -> dict:
    """Score message text against all AI intel categories."""
    scores = defaultdict(float)
    textl = text.lower()
    for cat, pattern in CATEGORIES.items():
        matches = re.findall(pattern, textl, re.IGNORECASE)
        if matches:
            scores[cat] += len(matches) * 2
    
    # Semantic boosters for weak signals
    if any(k in textl for k in ["api key", "token", "secret", "sk-", "bearer"]):
        scores["api_keys"] += 5
    if any(k in textl for k in ["claude code", "codex", "cursor", "opencode", "mcp server"]):
        scores["code_tips"] += 3
    if any(k in textl for k in ["free", "trial", "credit", "generat"]):
        scores["ai_abuse"] += 2
    if any(k in textl for k in ["release", "announce", "new model", "just launched"]):
        scores["ai_news"] += 2
    
    return dict(scores)

Dedup via State File

import json

STATE_PATH = "~/tg_intel_state.json"

def load_state() -> dict:
    try:
        with open(STATE_PATH) as f:
            return json.load(f)
    except FileNotFoundError:
        return {"last_scan": None, "seen_hashes": []}

def hash_msg(text: str) -> str:
    return str(hash(text.strip()[:200]))

def update_state(state: dict, new_items: list) -> dict:
    seen = set(state.get("seen_hashes", []))
    for item in new_items:
        if item["hash"] not in seen:
            seen.add(item["hash"])
    state["seen_hashes"] = list(seen)[-5000:]  # keep last 5000
    state["last_scan"] = datetime.now(timezone.utc).isoformat() + "Z"
    return state

References

  • telegram-account-management — for session audit and health patterns
  • cron-job-execution-discipline — for cron job execution contracts and formatting
  • telegram-automation — parent skill with Telethon repair patterns