understanding commited on
Commit
640b9a5
·
verified ·
1 Parent(s): 8cbb291

Update bot/integrations/cf_worker2.py

Browse files
Files changed (1) hide show
  1. bot/integrations/cf_worker2.py +54 -8
bot/integrations/cf_worker2.py CHANGED
@@ -1,16 +1,25 @@
1
  # PATH: bot/integrations/cf_worker2.py
 
 
2
  from bot.config import Workers
3
  from bot.integrations.http import post_json
4
  from bot.core.settings import ROTATE_AFTER
5
 
6
 
7
- def _h():
8
  return {
9
  "Authorization": f"Bearer {Workers.HF_API_KEY}",
10
  "Content-Type": "application/json",
11
  }
12
 
13
 
 
 
 
 
 
 
 
14
  def _unwrap(r: dict) -> dict:
15
  """
16
  Unwrap post_json wrapper:
@@ -25,17 +34,17 @@ def _unwrap(r: dict) -> dict:
25
 
26
 
27
  async def allow_user(tg_id: int) -> dict:
28
- r = await post_json(f"{Workers.WORKER2_URL}/api/allow_user", _h(), {"tg_id": str(tg_id)})
29
  return _unwrap(r)
30
 
31
 
32
  async def disallow_user(tg_id: int) -> dict:
33
- r = await post_json(f"{Workers.WORKER2_URL}/api/disallow_user", _h(), {"tg_id": str(tg_id)})
34
  return _unwrap(r)
35
 
36
 
37
  async def is_allowed(tg_id: int) -> bool:
38
- r = await post_json(f"{Workers.WORKER2_URL}/api/is_allowed", _h(), {"tg_id": str(tg_id)})
39
  j = _unwrap(r)
40
  return bool(j.get("ok") and j.get("allowed") is True)
41
 
@@ -43,7 +52,7 @@ async def is_allowed(tg_id: int) -> bool:
43
  async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_AFTER) -> dict:
44
  r = await post_json(
45
  f"{Workers.WORKER2_URL}/api/pick_profile",
46
- _h(),
47
  {"tg_id": str(tg_id), "channel_id": str(channel_id), "rotate_after": int(rotate_after)},
48
  )
49
  return _unwrap(r)
@@ -52,7 +61,7 @@ async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_A
52
  async def access_token(tg_id: int, profile_id: str) -> dict:
53
  r = await post_json(
54
  f"{Workers.WORKER2_URL}/api/access_token",
55
- _h(),
56
  {"tg_id": str(tg_id), "profile_id": profile_id},
57
  )
58
  return _unwrap(r)
@@ -61,12 +70,49 @@ async def access_token(tg_id: int, profile_id: str) -> dict:
61
  async def record_upload(tg_id: int, profile_id: str) -> dict:
62
  r = await post_json(
63
  f"{Workers.WORKER2_URL}/api/record_upload",
64
- _h(),
65
  {"tg_id": str(tg_id), "profile_id": profile_id},
66
  )
67
  return _unwrap(r)
68
 
69
 
70
  async def stats_today() -> dict:
71
- r = await post_json(f"{Workers.WORKER2_URL}/api/stats_today", _h(), {})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  return _unwrap(r)
 
1
  # PATH: bot/integrations/cf_worker2.py
2
+ from __future__ import annotations
3
+
4
  from bot.config import Workers
5
  from bot.integrations.http import post_json
6
  from bot.core.settings import ROTATE_AFTER
7
 
8
 
9
+ def _h_hf():
10
  return {
11
  "Authorization": f"Bearer {Workers.HF_API_KEY}",
12
  "Content-Type": "application/json",
13
  }
14
 
15
 
16
+ def _h_bot():
17
+ return {
18
+ "Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}",
19
+ "Content-Type": "application/json",
20
+ }
21
+
22
+
23
  def _unwrap(r: dict) -> dict:
24
  """
25
  Unwrap post_json wrapper:
 
34
 
35
 
36
  async def allow_user(tg_id: int) -> dict:
37
+ r = await post_json(f"{Workers.WORKER2_URL}/api/allow_user", _h_hf(), {"tg_id": str(tg_id)})
38
  return _unwrap(r)
39
 
40
 
41
  async def disallow_user(tg_id: int) -> dict:
42
+ r = await post_json(f"{Workers.WORKER2_URL}/api/disallow_user", _h_hf(), {"tg_id": str(tg_id)})
43
  return _unwrap(r)
44
 
45
 
46
  async def is_allowed(tg_id: int) -> bool:
47
+ r = await post_json(f"{Workers.WORKER2_URL}/api/is_allowed", _h_hf(), {"tg_id": str(tg_id)})
48
  j = _unwrap(r)
49
  return bool(j.get("ok") and j.get("allowed") is True)
50
 
 
52
  async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_AFTER) -> dict:
53
  r = await post_json(
54
  f"{Workers.WORKER2_URL}/api/pick_profile",
55
+ _h_hf(),
56
  {"tg_id": str(tg_id), "channel_id": str(channel_id), "rotate_after": int(rotate_after)},
57
  )
58
  return _unwrap(r)
 
61
  async def access_token(tg_id: int, profile_id: str) -> dict:
62
  r = await post_json(
63
  f"{Workers.WORKER2_URL}/api/access_token",
64
+ _h_hf(),
65
  {"tg_id": str(tg_id), "profile_id": profile_id},
66
  )
67
  return _unwrap(r)
 
70
  async def record_upload(tg_id: int, profile_id: str) -> dict:
71
  r = await post_json(
72
  f"{Workers.WORKER2_URL}/api/record_upload",
73
+ _h_hf(),
74
  {"tg_id": str(tg_id), "profile_id": profile_id},
75
  )
76
  return _unwrap(r)
77
 
78
 
79
  async def stats_today() -> dict:
80
+ r = await post_json(f"{Workers.WORKER2_URL}/api/stats_today", _h_hf(), {})
81
+ return _unwrap(r)
82
+
83
+
84
+ # ✅ Added: handlers.py expects list_profiles(uid, only_connected=...)
85
+ async def list_profiles(tg_id: int, only_connected: bool = False) -> dict:
86
+ r = await post_json(
87
+ f"{Workers.WORKER2_URL}/api/list_profiles",
88
+ _h_hf(),
89
+ {"tg_id": str(tg_id), "only_connected": bool(only_connected)},
90
+ )
91
+ return _unwrap(r)
92
+
93
+
94
+ # ✅ Added: handlers.py expects get_default_profile(uid) -> {ok, profile_id, access_token}
95
+ async def get_default_profile(tg_id: int) -> dict:
96
+ # worker2 supports default profile when profile_id is "" (it uses idx.default_profile_id)
97
+ j = await access_token(tg_id, "")
98
+ if isinstance(j, dict) and j.get("ok"):
99
+ return {
100
+ "ok": True,
101
+ "profile_id": j.get("profile_id"),
102
+ "access_token": j.get("access_token"),
103
+ "expires_in": j.get("expires_in"),
104
+ "channel_id": j.get("channel_id"),
105
+ "channel_title": j.get("channel_title"),
106
+ }
107
+ return j if isinstance(j, dict) else {"ok": False, "err": "access_token_failed"}
108
+
109
+
110
+ # ✅ Added: handlers.py expects set_default_profile(uid, pid)
111
+ # Your Pages backend protects /api/profile/* with BOT_BACKEND_KEY, so we call WORKER1_URL here.
112
+ async def set_default_profile(tg_id: int, profile_id: str) -> dict:
113
+ r = await post_json(
114
+ f"{Workers.WORKER1_URL}/api/profile/set_default",
115
+ _h_bot(),
116
+ {"tg_id": str(tg_id), "profile_id": str(profile_id)},
117
+ )
118
  return _unwrap(r)