Bot Platform & In-Chat CRM
Admin Dashboard, Analytics & Payments
Key Features
In-Chat CRM
Admins can block users, view logs, and refund payments directly in Telegram.
Targeted Monetization
System for sending promotional invoices and managing free trials.
Fuzzy Data Retrieval
Smart caching system with FuzzyWuzzy for typo-tolerant search.
How It Works
The goal was to create a serverless-feeling architecture where the bot acts as both the client interface and the admin dashboard. Instead of building a separate web panel, I implemented a complex command suite for administrators. This allows real-time control: viewing user history, broadcasting messages, managing blacklists, and even processing financial refunds via the Telegram Stars API instantly. The data layer uses a smart caching mechanism with Google Sheets, ensuring high performance without a heavy database backend.
Code Highlights
Refund System Implementation
This is a critical administrative feature. It allows admins to refund Telegram Stars directly from the chat using a command. The system logs the transaction, interacts with the payment API, and notifies the user — handling potentially complex financial operations with a simple interface.
async def refund_stars(update, context):
# Parse User ID and Payment Charge ID
target_id = args[0]
charge_id = args[1]
try:
# Execute refund via Telegram API
await context.bot.refund_star_payment(
user_id=target_id,
telegram_payment_charge_id=charge_id
)
# Log success and notify user
payment_logger.info(f"Refund Success - UserID: {target_id}")
await context.bot.send_message(
chat_id=target_id,
text="⭐ Admin refunded your payment."
)
except BadRequest as e:
await update.message.reply_text(f"API Error: {e.message}")Targeted Invoice Broadcasting
Unlike simple text broadcasts, this function enables the business logic of 'Special Offers'. The admin can generate real invoices (XTR) and send them to all users or specific targets with a custom payload, allowing for dynamic marketing campaigns.
async def send_invoice_message(update, context):
# Select target audience (All or Single)
users = get_target_users(args[0])
for user_id in users:
payload = f"promo_{user_id}_{timestamp}"
# Generate and send invoice with payload
await context.bot.send_invoice(
chat_id=user_id,
title=title,
description=description,
payload=payload,
currency="XTR",
prices=[LabeledPrice("Promo Price", price)]
)
# Rate limiting for mass broadcast
await asyncio.sleep(0.04) # Avoid flood limitsFuzzy Search & Caching
To ensure speed, the bot caches Google Sheets data into memory. The search uses Levenshtein distance (FuzzyWuzzy) to find records even when users misspell names, returning the most relevant results via an Inline Keyboard.
async def handle_text_message(update, context):
query = update.message.text
df = context.bot_data.get('sheet_data')
# Extract potential matches with scores
choices = df['Full_Name'].tolist()
matches = process.extract(query, choices, limit=5)
# Filter results with high confidence
found_indices = [
choices.index(match)
for match, score in matches if score > 75
]
if len(found_indices) > 1:
# Dynamic keyboard for user selection
keyboard = build_choice_keyboard(found_indices)
await update.message.reply_markup(keyboard)