from telegram import Update from telegram.ext import Updater, CommandHandler, CallbackContext import sqlite3 # === DATABASE SETUP === conn = sqlite3.connect('bot.db', check_same_thread=False) cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( user_id INTEGER PRIMARY KEY, credits INTEGER DEFAULT 0 ) ''') cursor.execute(''' CREATE TABLE IF NOT EXISTS videos ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, url TEXT, views INTEGER DEFAULT 0 ) ''') conn.commit() # === FUNCTIONS === def start(update: Update, context: CallbackContext): user_id = update.message.from_user.id cursor.execute("INSERT OR IGNORE INTO users (user_id) VALUES (?)", (user_id,)) conn.commit() update.message.reply_text("👋 Welcome to the Instagram View Exchange Bot!\n\nUse /submit to add your video and /view to watch videos.") def submit(update: Update, context: CallbackContext): user_id = update.message.from_user.id if len(context.args) != 1: update.message.reply_text("Usage: /submit ") return url = context.args[0] cursor.execute("INSERT INTO videos (user_id, url) VALUES (?, ?)", (user_id, url)) conn.commit() update.message.reply_text("✅ Your video has been added! Users will start watching it soon.") def view(update: Update, context: CallbackContext): user_id = update.message.from_user.id cursor.execute("SELECT id, url FROM videos WHERE user_id != ? ORDER BY views ASC LIMIT 1", (user_id,)) result = cursor.fetchone() if result: video_id, url = result cursor.execute("UPDATE videos SET views = views + 1 WHERE id = ?", (video_id,)) cursor.execute("UPDATE users SET credits = credits + 1 WHERE user_id = ?", (user_id,)) conn.commit() update.message.reply_text(f"📺 Watch this video: {url}\n\nYou earned 1 credit!") else: update.message.reply_text("😔 No videos to view right now. Try again later!") def credits(update: Update, context: CallbackContext): user_id = update.message.from_user.id cursor.execute("SELECT credits FROM users WHERE user_id = ?", (user_id,)) credits = cursor.fetchone() if credits: update.message.reply_text(f"💰 You have {credits[0]} credits.") else: update.message.reply_text("No credits found. Use /view to earn some!") # === MAIN BOT SETUP === TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN' # Replace with your bot token updater = Updater(TOKEN) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("submit", submit)) dp.add_handler(CommandHandler("view", view)) dp.add_handler(CommandHandler("credits", credits)) updater.start_polling() updater.idle()