-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (108 loc) · 3.48 KB
/
main.py
File metadata and controls
118 lines (108 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import asyncio
import os
import logging
import time
import aiohttp
from aiohttp import web
from pyrogram import idle
from bot import Bot
# Calculate uptime
START_TIME = time.time()
def get_uptime():
elapsed = time.time() - START_TIME
days, rem = divmod(elapsed, 86400)
hours, rem = divmod(rem, 3600)
minutes, seconds = divmod(rem, 60)
return f"{int(days)}d {int(hours)}h {int(minutes)}m {int(seconds)}s"
async def web_server():
async def handle(request):
uptime = get_uptime()
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bot Status</title>
<style>
body {{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}}
.container {{
background-color: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 400px;
width: 100%;
}}
h1 {{
color: #1a73e8;
margin-bottom: 20px;
}}
p {{
color: #555;
font-size: 18px;
margin: 10px 0;
}}
.status-active {{
color: #28a745;
font-weight: bold;
}}
.footer {{
margin-top: 30px;
font-size: 14px;
color: #888;
}}
</style>
</head>
<body>
<div class="container">
<h1>Bot is Running</h1>
<p>Status: <span class="status-active">Active</span></p>
<p>Uptime: {uptime}</p>
<div class="footer">
Powered by Auto Forward Bot V2
</div>
</div>
</body>
</html>
"""
return web.Response(text=html_content, content_type='text/html')
app = web.Application()
app.add_routes([web.get('/', handle)])
runner = web.AppRunner(app)
await runner.setup()
port = int(os.environ.get('PORT', 8080))
site = web.TCPSite(runner, '0.0.0.0', port)
await site.start()
logging.info(f"Web server started on port {port}")
async def ping_server():
while True:
await asyncio.sleep(300) # Ping every 5 minutes
try:
port = int(os.environ.get('PORT', 8080))
url = f'http://127.0.0.1:{port}'
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
logging.info(f"Self-ping to {url}: Status {resp.status}")
except Exception as e:
logging.error(f"Self-ping failed: {e}")
async def main():
bot = Bot()
await bot.start()
# Start web server
await web_server()
# Start self-ping task
asyncio.create_task(ping_server())
await idle()
await bot.stop()
if __name__ == "__main__":
asyncio.run(main())