izuemon commited on
Commit
bfd6376
·
verified ·
1 Parent(s): 3e9169a

Update join.py

Browse files
Files changed (1) hide show
  1. join.py +47 -61
join.py CHANGED
@@ -6,18 +6,16 @@ import time
6
  import requests
7
  from typing import List, Dict, Any
8
 
9
- # ===== 設定 =====
10
  X_ACCOUNT = os.getenv("dmsendertoken")
11
  if not X_ACCOUNT:
12
- raise RuntimeError("環境変数 dmsendertoken が設定されていません")
13
-
14
- GROUP_ID = 463667
15
- WATCH_PERSON_ID = "599642"
16
 
 
17
  INVITE_GROUPS = [519217, 536194, 534868]
18
 
19
- BASE_MESSAGES = "https://desk-api.channel.io/desk/channels/200605/groups/{group_id}/messages"
20
- BASE_INVITE = "https://desk-api.channel.io/desk/channels/200605/groups/{group_id}/invite"
21
 
22
  PARAMS = {
23
  "sortOrder": "desc",
@@ -32,80 +30,69 @@ HEADERS = {
32
  "x-account": X_ACCOUNT,
33
  }
34
 
35
- WELCOME_TEXT = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
36
-
37
-
38
- # ===== API =====
39
- def get_messages() -> List[Dict[str, Any]]:
40
- url = BASE_MESSAGES.format(group_id=GROUP_ID)
41
- r = requests.get(url, headers=HEADERS, params=PARAMS, timeout=20)
42
- r.raise_for_status()
43
- return r.json().get("messages", [])
44
-
45
-
46
  def post_message(text: str) -> None:
47
- url = BASE_MESSAGES.format(group_id=GROUP_ID)
48
  payload = {
49
  "requestId": f"desk-web-{int(time.time() * 1000)}",
50
  "blocks": [{"type": "text", "value": text}],
51
  }
52
- requests.post(url, headers=HEADERS, json=payload, timeout=20)
53
-
54
-
55
- def invite(group_id: int, person_id: str) -> None:
56
- url = BASE_INVITE.format(group_id=group_id)
57
- params = {"managerIds": person_id}
58
- requests.post(url, headers=HEADERS, params=params, timeout=20)
59
 
 
 
 
 
 
 
60
 
61
- # ===== ロジック =====
62
  def process():
63
- messages = get_messages()
 
 
 
 
 
 
 
 
 
64
 
65
- # personId == 599642 の最新メッセージ時刻
66
- latest_watch_created = max(
67
- (int(m.get("createdAt") or 0) for m in messages if str(m.get("personId")) == WATCH_PERSON_ID),
68
- default=0,
69
- )
70
 
71
- # join ログ抽出
72
- join_logs = []
73
  for m in messages:
74
- log = m.get("log") or {}
75
- if log.get("action") == "join":
76
- join_logs.append({
77
- "createdAt": int(m.get("createdAt") or 0),
78
- "personId": str(m.get("personId") or ""),
79
- })
80
-
81
- if not join_logs:
82
- return
83
 
84
- # 599642 以降の join
85
- targets = [j for j in join_logs if j["createdAt"] > latest_watch_created]
 
 
 
 
86
 
87
- if not targets:
88
  return
89
 
90
- # 先頭 join に対してのみ挨拶判定
91
- should_send_welcome = latest_watch_created == 0 or (
92
- targets and targets[0]["createdAt"] > latest_watch_created
93
- )
94
-
95
- if should_send_welcome:
96
  post_message(WELCOME_TEXT)
97
 
98
- # invite は全 join 対象に実行
99
- for j in targets:
100
- pid = j["personId"]
101
- if not pid:
 
102
  continue
103
  for gid in INVITE_GROUPS:
104
- invite(gid, pid)
105
-
106
 
107
  def main():
108
- print("[INFO] bot start (10秒間隔)")
109
  while True:
110
  try:
111
  process()
@@ -113,6 +100,5 @@ def main():
113
  print(f"[ERROR] {e}")
114
  time.sleep(10)
115
 
116
-
117
  if __name__ == "__main__":
118
  main()
 
6
  import requests
7
  from typing import List, Dict, Any
8
 
9
+ # --- 設定 ---
10
  X_ACCOUNT = os.getenv("dmsendertoken")
11
  if not X_ACCOUNT:
12
+ raise RuntimeError("環境変数 dmsendertoken が設定されていません。")
 
 
 
13
 
14
+ BASE_MESSAGES = "https://desk-api.channel.io/desk/channels/200605/groups/463667/messages"
15
  INVITE_GROUPS = [519217, 536194, 534868]
16
 
17
+ BOT_PERSON_ID = "599642"
18
+ WELCOME_TEXT = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
19
 
20
  PARAMS = {
21
  "sortOrder": "desc",
 
30
  "x-account": X_ACCOUNT,
31
  }
32
 
33
+ # --- API ---
 
 
 
 
 
 
 
 
 
 
34
  def post_message(text: str) -> None:
 
35
  payload = {
36
  "requestId": f"desk-web-{int(time.time() * 1000)}",
37
  "blocks": [{"type": "text", "value": text}],
38
  }
39
+ r = requests.post(BASE_MESSAGES, headers=HEADERS, json=payload, timeout=30)
40
+ r.raise_for_status()
41
+ print("[INFO] 歓迎メッセージ送信")
 
 
 
 
42
 
43
+ def invite_to_group(group_id: int, manager_id: str) -> None:
44
+ url = f"https://desk-api.channel.io/desk/channels/200605/groups/{group_id}/invite"
45
+ params = {"managerIds": manager_id}
46
+ r = requests.post(url, headers=HEADERS, params=params, timeout=30)
47
+ r.raise_for_status()
48
+ print(f"[INFO] group {group_id} に {manager_id} を招待")
49
 
50
+ # --- メイン処理 ---
51
  def process():
52
+ r = requests.get(BASE_MESSAGES, headers=HEADERS, params=PARAMS, timeout=30)
53
+ r.raise_for_status()
54
+ messages = r.json().get("messages", [])
55
+
56
+ # 最新の bot メッセージ時刻
57
+ bot_created_at = None
58
+ for m in messages:
59
+ if str(m.get("personId")) == BOT_PERSON_ID:
60
+ bot_created_at = int(m.get("createdAt") or 0)
61
+ break # desc なので最初でOK
62
 
63
+ join_messages: List[Dict[str, Any]] = []
 
 
 
 
64
 
 
 
65
  for m in messages:
66
+ created_at = int(m.get("createdAt") or 0)
67
+ if bot_created_at and created_at <= bot_created_at:
68
+ continue
 
 
 
 
 
 
69
 
70
+ for log in m.get("log", []) or []:
71
+ if log.get("action") == "join":
72
+ join_messages.append({
73
+ "createdAt": created_at,
74
+ "personId": str(m.get("personId")),
75
+ })
76
 
77
+ if not join_messages:
78
  return
79
 
80
+ # bot発言以降に join があり、botの発言がまだ無い場合のみ歓迎メッセージ
81
+ if bot_created_at is None:
 
 
 
 
82
  post_message(WELCOME_TEXT)
83
 
84
+ # join 全員を invite(複数可)
85
+ invited = set()
86
+ for jm in join_messages:
87
+ pid = jm["personId"]
88
+ if not pid or pid in invited:
89
  continue
90
  for gid in INVITE_GROUPS:
91
+ invite_to_group(gid, pid)
92
+ invited.add(pid)
93
 
94
  def main():
95
+ print("[INFO] Bot 起動(10秒ごとにチェック)")
96
  while True:
97
  try:
98
  process()
 
100
  print(f"[ERROR] {e}")
101
  time.sleep(10)
102
 
 
103
  if __name__ == "__main__":
104
  main()