import speedtest
import asyncio
from pyrogram import Client, filters
from config import ADMIN
@Client.on_message(filters.user(ADMIN) & filters.command("speedtest"))
async def speedtest_runner(client, message):
msg = await message.reply_text("Running Speedtest... This might take a minute.")
try:
loop = asyncio.get_event_loop()
st = speedtest.Speedtest()
# Select best server
await loop.run_in_executor(None, st.get_best_server)
# Run download test
await msg.edit("Running Download Test...")
await loop.run_in_executor(None, st.download)
# Run upload test
await msg.edit("Running Upload Test...")
await loop.run_in_executor(None, st.upload)
# Get share image
await msg.edit("Finalizing Results...")
results = await loop.run_in_executor(None, st.results.dict)
share_image = await loop.run_in_executor(None, st.results.share)
# Format results
download = results['download'] / 10**6 # bps to Mbps
upload = results['upload'] / 10**6 # bps to Mbps
ping = results['ping']
server = results['server']
text = f"""🚀 Speedtest Results
📥 Download: {download:.2f} Mbps
📤 Upload: {upload:.2f} Mbps
📡 Ping: {ping:.1f} ms
🌍 Server: {server['name']} ({server['country']})
🏢 ISP: {results['client']['isp']}
Bot Managed By @dragbotsupport"""
await message.reply_photo(photo=share_image, caption=text)
await msg.delete()
except Exception as e:
await msg.edit(f"Speedtest Error:\n{str(e)}")