import httpx
import asyncio
from aiogram import types
async def foydaAds_handler(message: types.Message):
# Your Bot API Key (Store this securely)
API_KEY = "YOUR_API_KEY_HERE"
USER_ID = message.from_user.id
# Send background request to Ad Server
# The Ad Server will securely deliver the ad to the user via your bot
url = f"https://api.foydaads.uz/api/serve/{API_KEY}?user_id={USER_ID}"
try:
async with httpx.AsyncClient() as client:
response = await client.get(url, timeout=5.0)
if response.status_code == 200:
data = response.json()
# Server will return status "ok" if ad was successfully served
if data.get("status") == "ok":
ad_id = data.get("ad_id")
# Ad was served successfully. Funds are added to your balance.
print(f"Ad served successfully! ID: {ad_id}")
else:
reason = data.get("message")
# No ad served (Limit reached or pool empty). Continue bot execution.
print(f"Ad skipped: {reason}")
except Exception as e:
print(f"Error connecting to Ad Server: {e}")