import os import sys import threading from http.server import HTTPServer, BaseHTTPRequestHandler import asyncio import urllib.parse # ========================================== # šŸŖ„ THE HUGGING FACE TRICK # ========================================== class DummyHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b"Bot is online and running on Hugging Face!") def keep_alive(): server = HTTPServer(('0.0.0.0', 7860), DummyHandler) server.serve_forever() threading.Thread(target=keep_alive, daemon=True).start() # ========================================== # šŸ¤– BOT LIBRARIES (MODERN PY-TGCALLS) # ========================================== from pyrogram import Client, filters from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery # šŸ› MONKEY PATCH import pyrogram.errors if not hasattr(pyrogram.errors, 'GroupcallForbidden'): class GroupcallForbidden(Exception): pass pyrogram.errors.GroupcallForbidden = GroupcallForbidden from pytgcalls import PyTgCalls from pytgcalls.types import MediaStream, AudioQuality, VideoQuality import requests import sqlite3 import random # ========================================== # šŸ”‘ YOUR CREDENTIALS # ========================================== API_ID = 2040 API_HASH = "b18441a1ff607e10a989891a5462e627" BOT_TOKEN = "8402411770:AAFvqCzvHwKRX0ScCzbGO3jGsDoD6LJphg4" app = Client("MusicQuizBot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN) call_py = PyTgCalls(app) # ========================================== # šŸ’¾ DATABASE SETUP # ========================================== db = sqlite3.connect("bot_database.db", check_same_thread=False) cursor = db.cursor() cursor.execute("CREATE TABLE IF NOT EXISTS local_quizzes (id INTEGER PRIMARY KEY, question TEXT, correct TEXT, wrong1 TEXT, wrong2 TEXT, wrong3 TEXT)") db.commit() # ========================================== # šŸŽµ DAVID CYRIL API DOWNLOADERS # ========================================== def download_audio_api(query): # Hit your API to get the audio download URL api_url = f"https://apis.davidcyril.name.ng/play?query={urllib.parse.quote(query)}" res = requests.get(api_url).json() if not res.get("status") or not res.get("result"): raise Exception("API could not find the song.") data = res["result"] title = data.get("title", "Unknown_Audio").replace("/", "_") download_url = data.get("download_url") # Download the MP3 to the server so PyTgCalls can stream it smoothly file_path = f"{title}.mp3" audio_data = requests.get(download_url).content with open(file_path, "wb") as f: f.write(audio_data) return file_path, title def download_video_api(query): # Hit your API to get the video link search_res = requests.get(f"https://apis.davidcyril.name.ng/play?query={urllib.parse.quote(query)}").json() if not search_res.get("status"): raise Exception("API could not find the video.") yt_url = search_res["result"]["video_url"] title = search_res["result"]["title"].replace("/", "_") video_res = requests.get(f"https://apis.davidcyril.name.ng/download/ytmp4?url={urllib.parse.quote(yt_url)}").json() if not video_res.get("success"): raise Exception("API failed to generate video link.") download_url = video_res["result"]["download_url"] # Download the MP4 to the server file_path = f"{title}.mp4" video_data = requests.get(download_url).content with open(file_path, "wb") as f: f.write(video_data) return file_path, title # ========================================== # šŸŽ™ļø VOICE CHAT STREAMING # ========================================== @app.on_message(filters.command("play") & filters.group) async def play_audio(client, message): if len(message.command) < 2: return await message.reply_text("🩸 `/play `") query = message.text.split(" ", 1)[1] status = await message.reply_text("šŸ” Fetching Audio from API...") try: # Download in background so bot doesn't freeze file_path, title = await asyncio.to_thread(download_audio_api, query) await status.edit_text(f"šŸš€ Joining VC & Playing: **{title}**") # Stream the file directly into the VC! await call_py.play(message.chat.id, MediaStream(file_path, audio_parameters=AudioQuality.HIGH)) except Exception as e: await status.edit_text(f"🩸 **VC Error:** {str(e)}") @app.on_message(filters.command("vplay") & filters.group) async def play_video(client, message): if len(message.command) < 2: return await message.reply_text("🩸 `/vplay