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

Update join.py

Browse files
Files changed (1) hide show
  1. join.py +53 -67
join.py CHANGED
@@ -4,19 +4,21 @@
4
  import os
5
  import time
6
  import requests
7
- from typing import Dict, Any, List
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" # この personId の発言有無をチェック
16
- BASE_MESSAGES = f"https://desk-api.channel.io/desk/channels/200605/groups/{GROUP_ID}/messages"
17
 
18
  INVITE_GROUPS = [519217, 536194, 534868]
19
 
 
 
 
20
  PARAMS = {
21
  "sortOrder": "desc",
22
  "limit": 34,
@@ -30,96 +32,80 @@ HEADERS = {
30
  "x-account": X_ACCOUNT,
31
  }
32
 
33
- WELCOME_MESSAGE = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
34
 
35
 
36
- # --- API ---
37
- def fetch_messages() -> List[Dict[str, Any]]:
38
- r = requests.get(BASE_MESSAGES, headers=HEADERS, params=PARAMS, timeout=20)
 
39
  r.raise_for_status()
40
  return r.json().get("messages", [])
41
 
42
 
43
  def post_message(text: str) -> None:
 
44
  payload = {
45
  "requestId": f"desk-web-{int(time.time() * 1000)}",
46
  "blocks": [{"type": "text", "value": text}],
47
  }
48
- r = requests.post(BASE_MESSAGES, headers=HEADERS, json=payload, timeout=20)
49
- r.raise_for_status()
50
- print("[INFO] ウェルカムメッセージ送信")
51
 
52
 
53
- def invite_to_group(group_id: int, manager_id: str) -> None:
54
- url = (
55
- f"https://desk-api.channel.io/desk/channels/200605/"
56
- f"groups/{group_id}/invite"
57
- f"?managerIds={manager_id}"
58
- )
59
- r = requests.post(url, headers=HEADERS, timeout=20)
60
- r.raise_for_status()
61
- print(f"[INFO] group {group_id} に personId={manager_id} を招待")
62
 
63
 
64
- # --- ロジック ---
65
  def process():
66
- messages = fetch_messages()
67
-
68
- # personId=599642 の最新メッセージ時刻
69
- latest_watch_created_at = max(
70
- (int(m.get("createdAt") or 0)
71
- for m in messages
72
- if str(m.get("personId", "")) == WATCH_PERSON_ID),
73
- default=0
74
- )
75
-
76
- join_events = []
77
-
78
- for msg in messages:
79
- created_at = int(msg.get("createdAt") or 0)
80
- logs = msg.get("logs") or []
81
 
82
- for log in logs:
83
- if log.get("action") != "join":
84
- continue
 
 
85
 
86
- join_person_id = str(log.get("personId", ""))
 
 
 
 
 
 
 
 
87
 
88
- # join メッセージ以降に 599642 の発言があるか
89
- has_watch_after = any(
90
- str(m.get("personId", "")) == WATCH_PERSON_ID
91
- and int(m.get("createdAt") or 0) > created_at
92
- for m in messages
93
- )
94
 
95
- join_events.append({
96
- "created_at": created_at,
97
- "person_id": join_person_id,
98
- "has_watch_after": has_watch_after,
99
- })
100
 
101
- if not join_events:
102
  return
103
 
104
- # createdAt が新しい順に処理
105
- join_events.sort(key=lambda x: x["created_at"], reverse=True)
 
 
106
 
107
- # 599642 の発言がない join が1つでもあれば最初だけ挨拶
108
- need_welcome = any(not e["has_watch_after"] for e in join_events)
109
- if need_welcome:
110
- post_message(WELCOME_MESSAGE)
111
 
112
- # 招待処理(条件どおり:複数あっても全部)
113
- for e in join_events:
114
- if e["created_at"] <= latest_watch_created_at:
 
115
  continue
116
-
117
  for gid in INVITE_GROUPS:
118
- invite_to_group(gid, e["person_id"])
119
 
120
 
121
- def main_loop():
122
- print("[INFO] Bot 起動(10秒ごと)")
123
  while True:
124
  try:
125
  process()
@@ -129,4 +115,4 @@ def main_loop():
129
 
130
 
131
  if __name__ == "__main__":
132
- main_loop()
 
4
  import os
5
  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",
24
  "limit": 34,
 
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()
 
115
 
116
 
117
  if __name__ == "__main__":
118
+ main()