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

Update bot/handlers.py

Browse files
Files changed (1) hide show
  1. bot/handlers.py +43 -8
bot/handlers.py CHANGED
@@ -26,17 +26,40 @@ from bot.temp.files import cleanup_file
26
 
27
  from bot.youtube.uploader import upload_video
28
 
29
- # βœ… NEW: extra dns helper for /diag
30
  from bot.integrations.diag_extra import dns_check
31
 
32
- # βœ… NEW: use robust fetch with DNS fallback (DoH -> IP)
33
- from bot.integrations.http import fetch_status
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # ---------- in-memory auth state (simple FSM) ----------
36
  # values: "json" | "ci"
37
  _AWAIT_AUTH: Dict[int, str] = {}
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def setup_handlers(app: Client) -> None:
41
 
42
  @app.on_message(filters.command(["start"]) & filters.private)
@@ -99,7 +122,6 @@ def setup_handlers(app: Client) -> None:
99
  if not is_owner_id(m.from_user.id if m.from_user else None):
100
  return await safe_reply(m, texts.OWNER_ONLY)
101
 
102
- # βœ… robust: tries normal request; on DNS fail uses DoH->IP fallback
103
  r1 = await fetch_status((Workers.WORKER1_URL or "") + "/")
104
  r2 = await fetch_status((Workers.WORKER2_URL or "") + "/")
105
 
@@ -174,26 +196,39 @@ def setup_handlers(app: Client) -> None:
174
  return
175
 
176
  try:
 
177
  if mode == "json":
178
  data = json.loads(m.text)
179
  root = data.get("installed") or data.get("web") or {}
180
- client_id = root.get("client_id") or data.get("client_id")
181
- client_secret = root.get("client_secret") or data.get("client_secret")
182
  if not client_id or not client_secret:
183
  return await safe_reply(m, texts.PARSE_FAIL)
184
  else:
185
- txt = m.text.strip()
186
  parts = [p.strip() for p in re.split(r"[|\n]+", txt) if p.strip()]
187
  if len(parts) < 2:
188
  return await safe_reply(m, texts.PARSE_FAIL)
189
  client_id, client_secret = parts[0], parts[1]
190
 
 
191
  j = await profile_add(uid, client_id, client_secret, label="main", ttl_sec=600)
192
  if not j.get("ok"):
193
  return await safe_reply(m, texts.PROFILE_ADD_FAIL.format(j))
194
 
 
195
  _AWAIT_AUTH.pop(uid, None)
196
- await safe_reply(m, texts.SENT_AUTH_LINK + j["login_url"])
 
 
 
 
 
 
 
 
 
 
197
 
198
  except Exception as e:
199
  await safe_reply(m, f"{texts.PARSE_FAIL}\n`{type(e).__name__}: {e}`")
 
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
+
63
  def setup_handlers(app: Client) -> None:
64
 
65
  @app.on_message(filters.command(["start"]) & filters.private)
 
122
  if not is_owner_id(m.from_user.id if m.from_user else None):
123
  return await safe_reply(m, texts.OWNER_ONLY)
124
 
 
125
  r1 = await fetch_status((Workers.WORKER1_URL or "") + "/")
126
  r2 = await fetch_status((Workers.WORKER2_URL or "") + "/")
127
 
 
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:
234
  await safe_reply(m, f"{texts.PARSE_FAIL}\n`{type(e).__name__}: {e}`")