izuemon commited on
Commit
ae415f4
·
verified ·
1 Parent(s): e3e1fb6

Update join.py

Browse files
Files changed (1) hide show
  1. join.py +75 -65
join.py CHANGED
@@ -5,21 +5,15 @@ import os
5
  import time
6
  import requests
7
  from typing import List, Dict, Any
8
- import re
9
 
10
  # ===== 設定 =====
11
  X_ACCOUNT = os.getenv("dmsendertoken")
12
  if not X_ACCOUNT:
13
  raise RuntimeError("環境変数 dmsendertoken が設定されていません")
14
 
15
- BASE_MESSAGES = (
16
- "https://desk-api.channel.io/desk/channels/200605/groups/463667/messages"
17
- )
18
- PARAMS = {
19
- "sortOrder": "desc",
20
- "limit": 34,
21
- "logFolded": "false",
22
- }
23
 
24
  HEADERS = {
25
  "accept": "application/json",
@@ -27,6 +21,13 @@ HEADERS = {
27
  "content-type": "application/json",
28
  "x-account": X_ACCOUNT,
29
  }
 
 
 
 
 
 
 
30
  TXT_WELCOME = """ようこそ
31
  こんにちは。ITRSAにようこそ。
32
  自動でいくつかの部屋に招待しています。
@@ -35,34 +36,10 @@ TXT_WELCOME = """ようこそ
35
  左側のメニューから、部屋(グループ)を開くことができます。
36
  ・Youtubeダウンローダーは、URLを送ると自動でYoutubeをダウンロードしてくれる部屋
37
  ・チャッピーは、ChatGPTと話せる部屋
38
- ・幹部と裁判所からの報告はここを管理する幹部や裁判所から通知が来る部屋
39
- {get_group_title(200605)}
40
  """
41
 
42
- def get_group_title(group_id: int) -> str | None:
43
- url = f"https://desk-api.channel.io/desk/channels/{group_id}/groups?limit=1000"
44
- response = requests.get(url)
45
- response.raise_for_status()
46
- data = response.json()
47
- groups = data.get("groups", [])
48
- for group in groups:
49
- if group.get("id") == group_id:
50
- return group.get("title")
51
- return None
52
-
53
- # 文字列内の {get_group_title(...)} を置換する関数
54
- def replace_titles_in_text(text: str) -> str:
55
- pattern = r"\{get_group_title\((\d+)\)\}"
56
-
57
- def replacer(match):
58
- group_id = int(match.group(1))
59
- title = get_group_title(group_id)
60
- return title if title else "タイトルなし"
61
-
62
- return re.sub(pattern, replacer, text)
63
-
64
- BOT_PERSON_ID = "599642"
65
-
66
  INVITE_GROUPS = [
67
  519217,
68
  536194,
@@ -71,50 +48,83 @@ INVITE_GROUPS = [
71
  530062
72
  ]
73
 
74
- # ===== API操作 =====
75
- def fetch_messages() -> List[Dict[str, Any]]:
76
- r = requests.get(BASE_MESSAGES, headers=HEADERS, params=PARAMS, timeout=20)
 
77
  r.raise_for_status()
78
  return r.json().get("messages", [])
79
 
80
 
81
  def post_welcome_message() -> None:
82
- WELCOME_TEXT = replace_titles_in_text(TXT_WELCOME)
83
  payload = {
84
- "requestId": f"welcome-{int(time.time() * 1000)}",
85
- "blocks": [{"type": "text", "value": WELCOME_TEXT}],
 
 
86
  }
87
- requests.post(BASE_MESSAGES, headers=HEADERS, json=payload, timeout=20)
 
88
 
89
 
90
- def invite_to_group(group_id: int, manager_id: str) -> None:
91
- url = (
92
- f"https://desk-api.channel.io/desk/channels/200605/"
93
- f"groups/{group_id}/invite?managerIds={manager_id}"
94
- )
95
- requests.post(url, headers=HEADERS, timeout=20)
96
 
97
 
98
  # ===== ロジック =====
99
- def process_once() -> None:
100
- messages = fetch_messages()
101
-
102
- # 599642 の発言時刻一覧(新しい順)
103
- bot_message_times = [
104
- int(m.get("createdAt") or 0)
105
- for m in messages
106
- if str(m.get("personId")) == BOT_PERSON_ID
107
- ]
108
-
109
- target_join_persons: List[str] = []
110
 
111
- for msg in messages:
112
- log = msg.get("log") or {}
 
 
113
  if log.get("action") != "join":
114
  continue
115
 
116
- join_created_at = int(msg.get("createdAt") or 0)
117
- join_person_id = str(msg.get("personId", ""))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- # この join より新しい bot 発言があるか
120
- has_newer_
 
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
+ BASE = "https://desk-api.channel.io/desk/channels/200605"
15
+ GROUP_ID = 463667
16
+ CHECK_PERSON_ID = "599642"
 
 
 
 
 
17
 
18
  HEADERS = {
19
  "accept": "application/json",
 
21
  "content-type": "application/json",
22
  "x-account": X_ACCOUNT,
23
  }
24
+
25
+ GET_PARAMS = {
26
+ "sortOrder": "desc",
27
+ "limit": 34,
28
+ "logFolded": "false",
29
+ }
30
+
31
  TXT_WELCOME = """ようこそ
32
  こんにちは。ITRSAにようこそ。
33
  自動でいくつかの部屋に招待しています。
 
36
  左側のメニューから、部屋(グループ)を開くことができます。
37
  ・Youtubeダウンローダーは、URLを送ると自動でYoutubeをダウンロードしてくれる部屋
38
  ・チャッピーは、ChatGPTと話せる部屋
39
+ ・幹部と裁判所からの報告はここを管理する幹部や裁判所から通知が来る部屋です。
40
+
41
  """
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  INVITE_GROUPS = [
44
  519217,
45
  536194,
 
48
  530062
49
  ]
50
 
51
+ # ===== API =====
52
+ def get_messages() -> List[Dict[str, Any]]:
53
+ url = f"{BASE}/groups/{GROUP_ID}/messages"
54
+ r = requests.get(url, headers=HEADERS, params=GET_PARAMS, timeout=20)
55
  r.raise_for_status()
56
  return r.json().get("messages", [])
57
 
58
 
59
  def post_welcome_message() -> None:
60
+ url = f"{BASE}/groups/{GROUP_ID}/messages"
61
  payload = {
62
+ "requestId": f"desk-web-{int(time.time() * 1000)}",
63
+ "blocks": [
64
+ {"type": "text", "value": WELCOME_TEXT}
65
+ ]
66
  }
67
+ requests.post(url, headers=HEADERS, json=payload, timeout=20).raise_for_status()
68
+ print("[INFO] 歓迎メッセージを送信しました")
69
 
70
 
71
+ def invite_person(group_id: int, person_id: str) -> None:
72
+ url = f"{BASE}/groups/{group_id}/invite"
73
+ params = {"managerIds": person_id}
74
+ requests.post(url, headers=HEADERS, params=params, timeout=20).raise_for_status()
75
+ print(f"[INFO] personId={person_id} を group {group_id} に招待しました")
 
76
 
77
 
78
  # ===== ロジック =====
79
+ def process():
80
+ messages = get_messages()
81
+
82
+ # personId=599642 の最新発言時刻
83
+ latest_599642 = max(
84
+ (int(m.get("createdAt", 0))
85
+ for m in messages
86
+ if str(m.get("personId")) == CHECK_PERSON_ID),
87
+ default=0
88
+ )
 
89
 
90
+ # 条件を満たす join メッセージを抽出
91
+ join_targets = []
92
+ for m in messages:
93
+ log = m.get("log") or {}
94
  if log.get("action") != "join":
95
  continue
96
 
97
+ created_at = int(m.get("createdAt", 0))
98
+ person_id = str(m.get("personId", ""))
99
+
100
+ # join 以降に 599642 の発言が無い
101
+ if created_at > latest_599642:
102
+ join_targets.append(person_id)
103
+
104
+ if not join_targets:
105
+ return
106
+
107
+ # 重複排除
108
+ join_targets = list(set(join_targets))
109
+
110
+ # 歓迎メッセージは1回だけ
111
+ post_welcome_message()
112
+
113
+ # 全員を各グループへ招待
114
+ for pid in join_targets:
115
+ for gid in INVITE_GROUPS:
116
+ invite_person(gid, pid)
117
+
118
+
119
+ def main():
120
+ print("[INFO] Bot 起動(10秒間隔)")
121
+ while True:
122
+ try:
123
+ process()
124
+ except Exception as e:
125
+ print(f"[ERROR] {e}")
126
+ time.sleep(10)
127
+
128
 
129
+ if __name__ == "__main__":
130
+ main()