Autonomous Telegram AI Agent for Lead Generation
A production-ready autonomous Telegram userbot that listens to group chats for trigger keywords, engages prospects in natural human-like conversations, and guides them toward a conversion goal — all powered by Grok grok-4-1-fast-reasoning (xAI API).
The agent uses the OARS method (Open → Affirm → Reflect → Summary) to build rapport before pitching. It never says "click this link" in the first message. It listens first.
- 🧠 Grok LLM Integration (
grok-4-1-fast-reasoning) — fastest reasoning model by xAI - 🗣️ OARS Conversation Method — open question → empathy → soft recommendation
- 🕵️ Human Behavior Simulation:
send_read_acknowledgebefore replying (reads first)client.action('typing')— shows typing indicator- Dynamic delay:
T = len(text) × 0.1 + random(1, 3)seconds
- 🚫 Anti-Ban Layer:
FloodWaitErrorauto-sleep (exact seconds Telegram requires + 5s buffer)PeerFloodErrordetection → 10-minute cooldown- First-shot rule: one trigger per chat, no spam
- 💾 Local Memory — 10-message sliding window per
chat_id(SQLite WAL) - 🔐 Encrypted Sessions — StringSession stored with Fernet encryption
- 📊 Daily Reports — stats to console and Telegram DM
- 🧙 CLI Onboarding Wizard — 7-step setup on first run (Rich terminal UI)
- 🔍 Reflection Gate — checks every reply for AI tells, sales pressure, banned phrases → regenerates automatically
| Use Case | Description |
|---|---|
| Course Promotion | Monitor educational groups, engage when someone asks about learning |
| SaaS Lead Gen | Listen for pain points in niche communities, offer your tool |
| Real Estate | Target renovation/design discussions, guide to consultations |
| Community Building | Warm up cold audiences with genuine conversations |
- Python 3.10+
- Telegram account (not a bot — a userbot)
- API credentials from my.telegram.org
- xAI API key from x.ai
git clone https://github.com/Kuzko-coder/tg-agent-leadgen.git
cd tg-agent-leadgen
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txtpython main.pyThe wizard will ask you step-by-step:
API_IDandAPI_HASH(from my.telegram.org)- Phone number authorization (with 2FA support)
- Agent name and persona description
- Conversion goal
- Trigger keywords
GROK_API_KEY- Telegram user ID for daily reports
python main.pyAgent starts automatically using saved (encrypted) session.
Copy .env.example to .env:
cp .env.example .env| Variable | Description |
|---|---|
API_ID |
Telegram API ID |
API_HASH |
Telegram API Hash |
PHONE_NUMBER |
Your phone number |
GROK_API_KEY |
xAI Grok API key |
GROK_MODEL |
grok-4-1-fast-reasoning |
AGENT_NAME |
Agent's display name |
AGENT_PERSONA |
Detailed personality description |
CONVERSION_GOAL |
What the agent should lead to |
TRIGGER_WORDS |
Comma-separated keywords |
REPORT_RECIPIENT_ID |
Telegram user ID for reports |
tg-agent-leadgen/
├── packages/
│ ├── config/ → Pydantic settings (.env loader)
│ ├── database/ → SQLite models + async CRUD
│ ├── memory/ → 10-msg sliding window + OARS state machine
│ ├── llms/ → Grok client + prompt builder + reflection gate
│ ├── core/ → Anti-ban layer + human behavior simulator
│ ├── telegram/ → Telethon client + event handlers + queue
│ ├── agent/ → Main orchestrator + trigger filter + OARS flow
│ ├── cli/ → Onboarding wizard + Fernet session manager
│ └── jobs/ → Message queue job + cleanup job + report job
├── tests/ → 18 unit tests (pytest + pytest-asyncio)
├── docs/ → GitHub Pages landing page
├── main.py → Entry point
└── requirements.txt
The prompting strategy is the core "secret sauce":
Step 1 → Ask open question, show genuine interest
Step 2 → Acknowledge their pain/problem with empathy. NO solutions yet.
Step 3 → Gently probe details. Hint a solution exists, don't reveal it.
Step 4 → Natively recommend {conversion_goal} as a friendly next step.
Humanizer Filters (strictly banned):
- "absolutely", "in today's world", "let's explore", "optimal solution"
- No bullet points in messages
- No long monologues (max 400 chars per reply)
- Never admit to being an AI
Writing Style:
- Active voice, short sentences
- Burstiness: alternate very short (2–5 words) and medium sentences
- Conversational openers: "so", "look", "by the way", "listen"
pytest tests/ -v18 passed in 1.82s ✅
| Test File | Coverage |
|---|---|
test_memory.py |
Rolling window, add/get, role preservation |
test_prompt_builder.py |
No banned words, OARS step diff, name injection, boundaries |
test_trigger_filter.py |
Case-insensitive, first-shot, active chat tracking |
test_anti_ban.py |
FloodWait exact sleep, ban error handling |
See CONTRIBUTING.md for setup and contribution guide.
Please read the Code of Conduct before contributing.
This project is for educational purposes only. Using userbots may violate Telegram's Terms of Service. Use at your own risk. The authors are not responsible for any account bans or misuse.
MIT License — Copyright (c) 2026