jmax98 commited on
Commit
17ca108
·
verified ·
1 Parent(s): e5527dd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -12
main.py CHANGED
@@ -1,36 +1,83 @@
1
  import asyncio
 
2
  from fastapi import FastAPI
3
  from aiogram import Bot, Dispatcher, types
4
  from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
5
  import uvicorn
 
6
 
7
- # إعدادات البوت
8
  TOKEN = '8628327909:AAETtkAFrUYUMwBhpv3BWsTWH09-fErd8WM'
9
  BLOCK_ID = '26019'
10
  bot = Bot(token=TOKEN)
11
  dp = Dispatcher()
12
-
13
  app = FastAPI()
14
 
15
- # --- الجزء الخاص بالبوت ---
 
 
 
 
 
 
 
 
 
 
 
 
16
  @dp.message()
17
  async def start_handler(message: types.Message):
18
- url = f"https://render.adsgram.ai/show?blockId={BLOCK_ID}&userId={message.from_user.id}"
 
 
 
 
 
 
 
 
 
 
 
19
  kb = InlineKeyboardMarkup(inline_keyboard=[
20
  [InlineKeyboardButton(text="💰 Watch Ad & Earn", web_app=WebAppInfo(url=url))]
21
  ])
22
- await message.answer("مرحباً بك في DooVoo! كليكي باش تتفرج وتربح:", reply_markup=kb)
23
 
24
- async def run_bot():
25
- await dp.start_polling(bot)
26
-
27
- # --- الجزء الخاص بالـ Reward API ---
28
  @app.get("/reward")
29
  async def reward_user(userId: str):
30
- print(f"✅ User {userId} rewarded!")
31
- return {"status": "success", "user": userId}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # تشغيل البوت عند إقلاع الـ Space
34
  @app.on_event("startup")
35
  async def startup_event():
36
  asyncio.create_task(run_bot())
 
1
  import asyncio
2
+ import sqlite3
3
  from fastapi import FastAPI
4
  from aiogram import Bot, Dispatcher, types
5
  from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, WebAppInfo
6
  import uvicorn
7
+ from datetime import datetime
8
 
9
+ # إعدادات البوت والـ Space
10
  TOKEN = '8628327909:AAETtkAFrUYUMwBhpv3BWsTWH09-fErd8WM'
11
  BLOCK_ID = '26019'
12
  bot = Bot(token=TOKEN)
13
  dp = Dispatcher()
 
14
  app = FastAPI()
15
 
16
+ # --- إعداد قاعدة البيانات ---
17
+ def init_db():
18
+ conn = sqlite3.connect('doovoo_users.db')
19
+ c = conn.cursor()
20
+ # جدول المستخدمين: ID، الإسم، عدد المشاهدات، وتاريخ أول دخول
21
+ c.execute('''CREATE TABLE IF NOT EXISTS users
22
+ (user_id TEXT PRIMARY KEY, username TEXT, ad_views INTEGER DEFAULT 0, joined_at TEXT)''')
23
+ conn.commit()
24
+ conn.close()
25
+
26
+ init_db()
27
+
28
+ # --- وظائف البوت ---
29
  @dp.message()
30
  async def start_handler(message: types.Message):
31
+ user_id = str(message.from_user.id)
32
+ username = message.from_user.username or "Unknown"
33
+
34
+ # تسجيل المستخدم إذا كان جديداً
35
+ conn = sqlite3.connect('doovoo_users.db')
36
+ c = conn.cursor()
37
+ c.execute("INSERT OR IGNORE INTO users (user_id, username, joined_at) VALUES (?, ?, ?)",
38
+ (user_id, username, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
39
+ conn.commit()
40
+ conn.close()
41
+
42
+ url = f"https://render.adsgram.ai/show?blockId={BLOCK_ID}&userId={user_id}"
43
  kb = InlineKeyboardMarkup(inline_keyboard=[
44
  [InlineKeyboardButton(text="💰 Watch Ad & Earn", web_app=WebAppInfo(url=url))]
45
  ])
46
+ await message.answer(f"مرحباً {username}! تفرج واربح النقاط.\nإحصائياتك مسجلة عندنا ✅", reply_markup=kb)
47
 
48
+ # --- الـ Reward API (تحديث عدد المشاهدات) ---
 
 
 
49
  @app.get("/reward")
50
  async def reward_user(userId: str):
51
+ conn = sqlite3.connect('doovoo_users.db')
52
+ c = conn.cursor()
53
+ # زيادة عدد المشاهدات للمستخدم بـ +1
54
+ c.execute("UPDATE users SET ad_views = ad_views + 1 WHERE user_id = ?", (userId,))
55
+ conn.commit()
56
+ conn.close()
57
+ print(f"🔥 Reward confirmed for: {userId}")
58
+ return {"status": "success", "new_reward": True}
59
+
60
+ # --- صفحة الإحصائيات (ليك أنت فقط) ---
61
+ @app.get("/admin/stats")
62
+ async def get_stats():
63
+ conn = sqlite3.connect('doovoo_users.db')
64
+ c = conn.cursor()
65
+ c.execute("SELECT COUNT(*), SUM(ad_views) FROM users")
66
+ total_users, total_views = c.fetchone()
67
+
68
+ c.execute("SELECT * FROM users ORDER BY ad_views DESC LIMIT 10")
69
+ top_users = c.fetchall()
70
+ conn.close()
71
+
72
+ return {
73
+ "total_active_users": total_users,
74
+ "total_global_views": total_views or 0,
75
+ "top_10_players": top_users
76
+ }
77
+
78
+ async def run_bot():
79
+ await dp.start_polling(bot)
80
 
 
81
  @app.on_event("startup")
82
  async def startup_event():
83
  asyncio.create_task(run_bot())