izuemon commited on
Commit
b9b9580
·
verified ·
1 Parent(s): f8bf022

Create join.py

Browse files
Files changed (1) hide show
  1. join.py +132 -0
join.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
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,
23
+ "logFolded": "false",
24
+ }
25
+
26
+ HEADERS = {
27
+ "accept": "application/json",
28
+ "accept-language": "ja",
29
+ "content-type": "application/json",
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()
126
+ except Exception as e:
127
+ print(f"[ERROR] {e}")
128
+ time.sleep(10)
129
+
130
+
131
+ if __name__ == "__main__":
132
+ main_loop()