understanding commited on
Commit
ebafdb9
Β·
verified Β·
1 Parent(s): 7fd0716

Update bot/handlers.py

Browse files
Files changed (1) hide show
  1. bot/handlers.py +30 -38
bot/handlers.py CHANGED
@@ -1,5 +1,5 @@
1
  # PATH: bot/handlers.py
2
- import asyncio, json, re, time
3
  from typing import Dict
4
 
5
  from hydrogram import Client, filters
@@ -26,37 +26,33 @@ from bot.temp.files import cleanup_file
26
 
27
  from bot.youtube.uploader import upload_video
28
 
29
- # βœ… extra dns helper for /diag
30
  from bot.integrations.diag_extra import dns_check
31
-
32
- # βœ… robust fetch_status (optional). If not present, fallback to get_client().get()
33
- try:
34
- from bot.integrations.http import fetch_status # type: ignore
35
- except Exception:
36
- async def fetch_status(url: str) -> str:
37
- try:
38
- from bot.integrations.http import get_client
39
- c = get_client()
40
- r = await c.get(url)
41
- return str(r.status_code)
42
- except Exception as e:
43
- return f"ERR:{type(e).__name__}:{e}"
44
-
45
 
46
  # ---------- in-memory auth state (simple FSM) ----------
47
- # values: "json" | "ci"
48
  _AWAIT_AUTH: Dict[int, str] = {}
49
 
50
 
51
  def _pick_login_url(j: dict) -> str:
52
- """Backend sometimes returns loginUrl instead of login_url. Accept both."""
53
  if not isinstance(j, dict):
54
  return ""
 
 
 
 
 
 
 
55
  return (
56
- (j.get("login_url") or "").strip()
57
- or (j.get("loginUrl") or "").strip()
58
- or (j.get("auth_url") or "").strip()
59
- or (j.get("url") or "").strip()
 
 
 
 
60
  )
61
 
62
 
@@ -196,38 +192,34 @@ def setup_handlers(app: Client) -> None:
196
  return
197
 
198
  try:
199
- # ---- parse credentials ----
200
  if mode == "json":
201
  data = json.loads(m.text)
202
  root = data.get("installed") or data.get("web") or {}
203
- client_id = (root.get("client_id") or data.get("client_id") or "").strip()
204
- client_secret = (root.get("client_secret") or data.get("client_secret") or "").strip()
205
  if not client_id or not client_secret:
206
  return await safe_reply(m, texts.PARSE_FAIL)
207
  else:
208
- txt = (m.text or "").strip()
209
  parts = [p.strip() for p in re.split(r"[|\n]+", txt) if p.strip()]
210
  if len(parts) < 2:
211
  return await safe_reply(m, texts.PARSE_FAIL)
212
  client_id, client_secret = parts[0], parts[1]
213
 
214
- # ---- call backend ----
215
  j = await profile_add(uid, client_id, client_secret, label="main", ttl_sec=600)
216
- if not j.get("ok"):
 
 
 
 
 
217
  return await safe_reply(m, texts.PROFILE_ADD_FAIL.format(j))
218
 
219
  login_url = _pick_login_url(j)
220
- _AWAIT_AUTH.pop(uid, None)
221
-
222
  if not login_url:
223
- # βœ… Do not crash; show backend response for debugging
224
- return await safe_reply(
225
- m,
226
- "❌ profile_add OK but login link missing.\n"
227
- "Backend response:\n"
228
- f"`{j}`"
229
- )
230
 
 
231
  await safe_reply(m, texts.SENT_AUTH_LINK + login_url)
232
 
233
  except Exception as e:
@@ -296,7 +288,7 @@ def setup_handlers(app: Client) -> None:
296
 
297
  file_path = ""
298
  try:
299
- file_path, file_size, file_name = await download_to_temp(app_, m)
300
  title, desc = extract_title_description(m, file_name)
301
 
302
  se = SpeedETA()
 
1
  # PATH: bot/handlers.py
2
+ import json, re, time
3
  from typing import Dict
4
 
5
  from hydrogram import Client, filters
 
26
 
27
  from bot.youtube.uploader import upload_video
28
 
 
29
  from bot.integrations.diag_extra import dns_check
30
+ from bot.integrations.http import fetch_status
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # ---------- in-memory auth state (simple FSM) ----------
 
33
  _AWAIT_AUTH: Dict[int, str] = {}
34
 
35
 
36
  def _pick_login_url(j: dict) -> str:
37
+ """Accept login_url from either top-level OR nested {data:{...}} shapes."""
38
  if not isinstance(j, dict):
39
  return ""
40
+
41
+ data = j.get("data") if isinstance(j.get("data"), dict) else {}
42
+
43
+ def _get(d: dict, k: str) -> str:
44
+ v = d.get(k)
45
+ return (v or "").strip() if isinstance(v, str) else ""
46
+
47
  return (
48
+ _get(j, "login_url")
49
+ or _get(j, "loginUrl")
50
+ or _get(j, "auth_url")
51
+ or _get(j, "url")
52
+ or _get(data, "login_url")
53
+ or _get(data, "loginUrl")
54
+ or _get(data, "auth_url")
55
+ or _get(data, "url")
56
  )
57
 
58
 
 
192
  return
193
 
194
  try:
 
195
  if mode == "json":
196
  data = json.loads(m.text)
197
  root = data.get("installed") or data.get("web") or {}
198
+ client_id = root.get("client_id") or data.get("client_id")
199
+ client_secret = root.get("client_secret") or data.get("client_secret")
200
  if not client_id or not client_secret:
201
  return await safe_reply(m, texts.PARSE_FAIL)
202
  else:
203
+ txt = m.text.strip()
204
  parts = [p.strip() for p in re.split(r"[|\n]+", txt) if p.strip()]
205
  if len(parts) < 2:
206
  return await safe_reply(m, texts.PARSE_FAIL)
207
  client_id, client_secret = parts[0], parts[1]
208
 
 
209
  j = await profile_add(uid, client_id, client_secret, label="main", ttl_sec=600)
210
+
211
+ if not isinstance(j, dict):
212
+ return await safe_reply(m, f"❌ profile_add bad response: `{j}`")
213
+
214
+ # βœ… handle both response shapes
215
+ if not j.get("ok") and not (isinstance(j.get("data"), dict) and j["data"].get("ok")):
216
  return await safe_reply(m, texts.PROFILE_ADD_FAIL.format(j))
217
 
218
  login_url = _pick_login_url(j)
 
 
219
  if not login_url:
220
+ return await safe_reply(m, f"❌ profile_add OK but login link missing.\nBackend response:\n`{j}`")
 
 
 
 
 
 
221
 
222
+ _AWAIT_AUTH.pop(uid, None)
223
  await safe_reply(m, texts.SENT_AUTH_LINK + login_url)
224
 
225
  except Exception as e:
 
288
 
289
  file_path = ""
290
  try:
291
+ file_path, _file_size, file_name = await download_to_temp(app_, m)
292
  title, desc = extract_title_description(m, file_name)
293
 
294
  se = SpeedETA()