Muttered3 commited on
Commit
4a79dc0
Β·
verified Β·
1 Parent(s): 34079f2

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +37 -75
bot.py CHANGED
@@ -5,7 +5,6 @@ import io
5
  import asyncio
6
  import aiohttp
7
  from telethon import events, Button
8
- from PIL import Image, ImageDraw, ImageFont
9
  import db
10
  import scraper
11
 
@@ -32,67 +31,6 @@ def get_settings_menu():
32
  [Button.inline("Β« Back to Main Menu", b"back_main")]
33
  ]
34
 
35
- # --- FRAGMENT-STYLE IMAGE GENERATOR ---
36
- def generate_fragment_card(username, status):
37
- width, height = 1000, 450
38
- bg_color = (17, 24, 39)
39
- card_bg = (31, 41, 55)
40
- text_white = (255, 255, 255)
41
- text_gray = (156, 163, 175)
42
- text_cyan = (56, 189, 248)
43
- divider_color = (55, 65, 81)
44
-
45
- status_map = {
46
- "TAKEN": {"bg": (69, 39, 0), "text": (251, 191, 36), "desc": "Someone already claimed this username on Telegram. You can make an offer to the owner."},
47
- "AVAILABLE": {"bg": (6, 78, 59), "text": (52, 211, 153), "desc": "This username is currently available! You can secure it immediately on Fragment."},
48
- "FOR_SALE": {"bg": (30, 58, 138), "text": (147, 197, 253), "desc": "This username is listed for sale. You can purchase it using TON."},
49
- "ON_AUCTION": {"bg": (30, 58, 138), "text": (147, 197, 253), "desc": "This username is currently in an active auction. Place your bid now."},
50
- "SOLD": {"bg": (31, 41, 55), "text": (209, 213, 219), "desc": "This username was recently sold. It is currently locked to a new owner."},
51
- "UNAVAILABLE": {"bg": (64, 64, 64), "text": (212, 212, 212), "desc": "This username is reserved or prohibited by Telegram and cannot be claimed."}
52
- }
53
-
54
- cfg = status_map.get(status.upper(), status_map["UNAVAILABLE"])
55
- img = Image.new('RGB', (width, height), color=bg_color)
56
- draw = ImageDraw.Draw(img)
57
-
58
- try:
59
- font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
60
- font_main = ImageFont.truetype(font_path, 48)
61
- font_pill = ImageFont.truetype(font_path, 22)
62
- font_card_head = ImageFont.truetype(font_path, 26)
63
- font_card_body = ImageFont.truetype(font_path, 18)
64
- except:
65
- font_main = font_pill = font_card_head = font_card_body = ImageFont.load_default()
66
-
67
- header_text = f"{username}.t.me"
68
- draw.text((40, 40), header_text, fill=text_white, font=font_main)
69
- tw = draw.textlength(header_text, font=font_main)
70
- pill_x = 40 + tw + 25
71
- draw.rounded_rectangle([pill_x, 52, pill_x + 135, 92], radius=8, fill=cfg["bg"])
72
- draw.text((pill_x + 22, 59), status.replace("_", " ").capitalize(), fill=cfg["text"], font=font_pill)
73
-
74
- draw.rounded_rectangle([40, 150, 480, 400], radius=10, fill=card_bg)
75
- draw.text((105, 190), "Can I buy this username?", fill=text_white, font=font_card_head)
76
- draw.line([40, 235, 480, 235], fill=divider_color, width=1)
77
- draw.multiline_text((260, 315), cfg["desc"], fill=text_gray, font=font_card_body, align="center", anchor="mm", spacing=8)
78
-
79
- draw.rounded_rectangle([510, 150, 960, 400], radius=10, fill=card_bg)
80
- rows = [("Telegram Username", f"@{username}"), ("Web Address", f"t.me/{username}"), ("TON Web 3.0", f"{username}.t.me")]
81
- y_pos = 185
82
- for i, (label, value) in enumerate(rows):
83
- draw.text((540, y_pos), label, fill=text_white, font=font_card_head)
84
- draw.text((930, y_pos), value, fill=text_cyan, font=font_card_head, anchor="ra")
85
- if i < 2:
86
- y_div = y_pos + 65
87
- draw.line([510, y_div, 960, y_div], fill=divider_color, width=1)
88
- y_pos += 85
89
-
90
- buf = io.BytesIO()
91
- img.save(buf, format='PNG')
92
- buf.seek(0)
93
- buf.name = f"{username}.png"
94
- return buf
95
-
96
  # --- SYSTEM DASHBOARD ---
97
  async def generate_status_msg():
98
  state = await db.get_state()
@@ -152,23 +90,42 @@ def setup_handlers(client):
152
  if is_admin(event):
153
  await event.respond("**Fragment Control System**", buttons=get_main_menu())
154
 
 
155
  @client.on(events.NewMessage(pattern=r'/check\s+([a-zA-Z0-9_]+)'))
156
  async def check_cmd(event):
157
  if not is_admin(event): return
158
  target = event.pattern_match.group(1).lower()
159
- msg = await event.respond(f"πŸ” Analyzing `@{target}`...")
 
 
160
 
161
  try:
162
- status = await scraper.check_fragment(target)
163
- if not status: status = "UNAVAILABLE"
164
- except Exception:
165
- status = "ERROR"
 
 
 
 
 
 
 
 
166
 
167
- img = await asyncio.to_thread(generate_fragment_card, target, status)
168
- await msg.delete()
169
- await client.send_file(event.chat_id, file=img, caption=f"**Report:** `@{target}`")
 
 
 
 
 
 
 
 
170
 
171
- # --- HTML DEBUG COMMAND (With Redirect Tracking) ---
172
  @client.on(events.NewMessage(pattern=r'/html\s+([a-zA-Z0-9_]+)'))
173
  async def html_cmd(event):
174
  if not is_admin(event): return
@@ -182,7 +139,6 @@ def setup_handlers(client):
182
 
183
  try:
184
  async with aiohttp.ClientSession() as session:
185
- # CRITICAL FIX: allow_redirects=False captures the bounce
186
  async with session.get(url, headers=headers, allow_redirects=False, timeout=15) as resp:
187
  html_text = await resp.text()
188
  status_code = resp.status
@@ -190,6 +146,7 @@ def setup_handlers(client):
190
 
191
  f = io.BytesIO(html_text.encode('utf-8'))
192
  f.name = f"{target}_fragment.html"
 
193
 
194
  caption = (
195
  f"πŸ“„ **Raw Response for** `@{target}`\n"
@@ -198,12 +155,17 @@ def setup_handlers(client):
198
  )
199
 
200
  if status_code in [301, 302, 303, 307]:
201
- caption += f"\n**🚨 FRAGMENT REDIRECTED TO:** `{location}`\n\n*(This proves Fragment kicked the bot off the username page!)*"
202
 
203
- await msg.delete()
204
  await client.send_file(event.chat_id, file=f, caption=caption)
 
 
205
  except Exception as e:
206
- await msg.edit(f"❌ **Error fetching HTML:** `{str(e)}`")
 
 
 
207
 
208
  @client.on(events.NewMessage(pattern='/upload'))
209
  async def upload_cmd(event):
 
5
  import asyncio
6
  import aiohttp
7
  from telethon import events, Button
 
8
  import db
9
  import scraper
10
 
 
31
  [Button.inline("Β« Back to Main Menu", b"back_main")]
32
  ]
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # --- SYSTEM DASHBOARD ---
35
  async def generate_status_msg():
36
  state = await db.get_state()
 
90
  if is_admin(event):
91
  await event.respond("**Fragment Control System**", buttons=get_main_menu())
92
 
93
+ # --- REAL HTML SCREENSHOT COMMAND ---
94
  @client.on(events.NewMessage(pattern=r'/check\s+([a-zA-Z0-9_]+)'))
95
  async def check_cmd(event):
96
  if not is_admin(event): return
97
  target = event.pattern_match.group(1).lower()
98
+ msg = await event.respond(f"πŸ“Έ Taking live screenshot of `@{target}` on Fragment...")
99
+
100
+ url = f"https://fragment.com/username/{target}"
101
 
102
  try:
103
+ # Run the browser screenshot in a separate thread so it doesn't freeze the bot
104
+ def take_screenshot():
105
+ from html2image import Html2Image
106
+ # --no-sandbox is critical for Linux servers
107
+ hti = Html2Image(custom_flags=['--no-sandbox', '--disable-gpu', '--hide-scrollbars', '--disable-dev-shm-usage'])
108
+ hti.size = (1000, 750)
109
+ img_name = f"{target}_fragment.png"
110
+ # Chrome navigates to the URL and renders the exact CSS/HTML
111
+ hti.screenshot(url=url, save_as=img_name)
112
+ return img_name
113
+
114
+ img_filename = await asyncio.to_thread(take_screenshot)
115
 
116
+ await client.send_file(event.chat_id, file=img_filename, caption=f"**Live Web Snapshot:** `@{target}`")
117
+ await msg.delete()
118
+
119
+ if os.path.exists(img_filename):
120
+ os.remove(img_filename)
121
+
122
+ except Exception as e:
123
+ try:
124
+ await msg.edit(f"❌ **Screenshot Error:**\n*(If this fails, your server is missing Chromium packages)*\n`{str(e)}`")
125
+ except:
126
+ pass
127
 
128
+ # --- FIXED HTML DEBUG COMMAND ---
129
  @client.on(events.NewMessage(pattern=r'/html\s+([a-zA-Z0-9_]+)'))
130
  async def html_cmd(event):
131
  if not is_admin(event): return
 
139
 
140
  try:
141
  async with aiohttp.ClientSession() as session:
 
142
  async with session.get(url, headers=headers, allow_redirects=False, timeout=15) as resp:
143
  html_text = await resp.text()
144
  status_code = resp.status
 
146
 
147
  f = io.BytesIO(html_text.encode('utf-8'))
148
  f.name = f"{target}_fragment.html"
149
+ f.seek(0) # CRITICAL FIX: Re-wind the buffer before sending
150
 
151
  caption = (
152
  f"πŸ“„ **Raw Response for** `@{target}`\n"
 
155
  )
156
 
157
  if status_code in [301, 302, 303, 307]:
158
+ caption += f"\n**🚨 FRAGMENT REDIRECTED TO:** `{location}`"
159
 
160
+ # CRITICAL FIX: Send file FIRST, delete message AFTER
161
  await client.send_file(event.chat_id, file=f, caption=caption)
162
+ await msg.delete()
163
+
164
  except Exception as e:
165
+ try:
166
+ await msg.edit(f"❌ **Error fetching HTML:** `{str(e)}`")
167
+ except:
168
+ await event.respond(f"❌ **Error fetching HTML:** `{str(e)}`")
169
 
170
  @client.on(events.NewMessage(pattern='/upload'))
171
  async def upload_cmd(event):