The ultimate viral growth bot with reward system
Each user gets their own personalized referral link to share with friends
Users earn rewards for every 5 successful referrals they make
Prevents fake referrals by tracking only first-time users
pip install python-telegram-bot
import logging
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters
import sqlite3
import os
# Database setup
def init_db():
conn = sqlite3.connect('referrals.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
(user_id INTEGER PRIMARY KEY, username TEXT, referral_count INTEGER DEFAULT 0, rewards INTEGER DEFAULT 0)''')
c.execute('''CREATE TABLE IF NOT EXISTS referrals
(ref_id INTEGER PRIMARY KEY, referring_user INTEGER, new_user INTEGER)''')
conn.commit()
conn.close()
# Bot functions
def start(update: Update, context: CallbackContext) -> None:
user = update.effective_user
user_id = user.id
username = user.username or user.first_name
# Add user to DB if not exists
conn = sqlite3.connect('referrals.db')
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO users (user_id, username) VALUES (?, ?)", (user_id, username))
conn.commit()
# Check if user came from referral
if context.args and context.args[0].isdigit():
referring_user_id = int(context.args[0])
if referring_user_id != user_id: # Prevent self-referral
# Check if this is user's first time
c.execute("SELECT COUNT(*) FROM referrals WHERE new_user = ?", (user_id,))
if c.fetchone()[0] == 0:
# Add referral record
c.execute("INSERT INTO referrals (referring_user, new_user) VALUES (?, ?)",
(referring_user_id, user_id))
# Update referral count
c.execute("UPDATE users SET referral_count = referral_count + 1 WHERE user_id = ?",
(referring_user_id,))
conn.commit()
# Check for rewards
c.execute("SELECT referral_count FROM users WHERE user_id = ?", (referring_user_id,))
count = c.fetchone()[0]
if count % 5 == 0:
c.execute("UPDATE users SET rewards = rewards + 1 WHERE user_id = ?",
(referring_user_id,))
conn.commit()
context.bot.send_message(
chat_id=referring_user_id,
text="🎉 Congrats! You earned a reward for 5 referrals!"
)
# Send welcome message with referral link
referral_link = f"https://t.me/{context.bot.username}?start={user_id}"
update.message.reply_text(
f"👋 Welcome {username}!\n\n"
f"🔗 Your unique referral link:\n{referral_link}\n\n"
f"Invite friends and earn rewards every 5 referrals!",
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton("📤 Share Link",
url=f"https://t.me/share/url?url={referral_link}&text=Join%20me%20on%20this%20awesome%20bot!")]
])
)
conn.close()
def stats(update: Update, context: CallbackContext) -> None:
user_id = update.effective_user.id
conn = sqlite3.connect('referrals.db')
c = conn.cursor()
c.execute("SELECT referral_count, rewards FROM users WHERE user_id = ?", (user_id,))
result = c.fetchone()
if result:
count, rewards = result
update.message.reply_text(
f"📊 Your Stats:\n\n"
f"👥 Referrals: {count}\n"
f"🎁 Rewards Earned: {rewards}\n\n"
f"Keep going! {5 - (count % 5)} more for next reward!"
)
conn.close()
def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text(
"🤖 Bot Commands:\n\n"
"/start - Get your referral link\n"
"/stats - Check your referral stats\n"
"/help - Show this help message\n\n"
"Invite friends to earn rewards every 5 referrals!"
)
def main() -> None:
# Initialize database
init_db()
# Create the Updater and pass it your bot's token.
updater = Updater("YOUR_BOT_TOKEN")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Register commands
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("stats", stats))
dispatcher.add_handler(CommandHandler("help", help_command))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C
updater.idle()
if __name__ == '__main__':
main()
python bot.py
Replace "YOUR_BOT_TOKEN" with your actual Telegram bot token from @BotFather