Spaces:
Running
Running
Update join.py
Browse files
join.py
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import requests
|
| 7 |
-
from typing import List, Dict, Any
|
| 8 |
|
| 9 |
# ===== 設定 =====
|
| 10 |
X_ACCOUNT = os.getenv("dmsendertoken")
|
|
@@ -13,8 +13,11 @@ if not X_ACCOUNT:
|
|
| 13 |
|
| 14 |
BASE = "https://desk-api.channel.io/desk/channels/200605"
|
| 15 |
GROUP_ID = 463667
|
| 16 |
-
CHECK_PERSON_ID = "
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
HEADERS = {
|
| 19 |
"accept": "application/json",
|
| 20 |
"accept-language": "ja",
|
|
@@ -51,18 +54,6 @@ INVITE_GROUPS = [
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# ===== API =====
|
| 54 |
-
def blacklist_manager(person_id: str) -> None:
|
| 55 |
-
url = f"https://desk-api.channel.io/desk/channels/200605/managers/{person_id}"
|
| 56 |
-
|
| 57 |
-
# OPTIONS
|
| 58 |
-
opt = requests.options(url, headers=HEADERS, timeout=20)
|
| 59 |
-
opt.raise_for_status()
|
| 60 |
-
|
| 61 |
-
# DELETE
|
| 62 |
-
delete = requests.delete(url, headers=HEADERS, timeout=20)
|
| 63 |
-
delete.raise_for_status()
|
| 64 |
-
|
| 65 |
-
print(f"[INFO] personId={person_id} をブラックリスト処理しました")
|
| 66 |
|
| 67 |
def get_messages() -> List[Dict[str, Any]]:
|
| 68 |
url = f"{BASE}/groups/{GROUP_ID}/messages"
|
|
@@ -70,22 +61,54 @@ def get_messages() -> List[Dict[str, Any]]:
|
|
| 70 |
r.raise_for_status()
|
| 71 |
return r.json().get("messages", [])
|
| 72 |
|
| 73 |
-
def get_group_titles() -> dict[int, str]:
|
| 74 |
-
url = "https://desk-api.channel.io/desk/channels/200605/groups?limit=1000"
|
| 75 |
-
response = requests.get(url, headers=HEADERS)
|
| 76 |
-
response.raise_for_status()
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
|
|
|
| 81 |
return {
|
| 82 |
group.get("id"): group.get("title")
|
| 83 |
for group in groups
|
| 84 |
if "id" in group and "title" in group
|
| 85 |
}
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def post_welcome_message() -> None:
|
| 88 |
-
TITLES = get_group_titles()
|
| 89 |
|
| 90 |
welcome_text = WELCOME_TEMPLATE.format(
|
| 91 |
hq=TITLES.get(532214, "本部")
|
|
@@ -98,11 +121,11 @@ def post_welcome_message() -> None:
|
|
| 98 |
{"type": "text", "value": welcome_text}
|
| 99 |
]
|
| 100 |
}
|
|
|
|
| 101 |
requests.post(url, headers=HEADERS, json=payload, timeout=20).raise_for_status()
|
| 102 |
print("[INFO] 歓迎メッセージを送信しました")
|
| 103 |
|
| 104 |
|
| 105 |
-
|
| 106 |
def invite_person(group_id: int, person_id: str) -> None:
|
| 107 |
url = f"{BASE}/groups/{group_id}/invite"
|
| 108 |
params = {"managerIds": person_id}
|
|
@@ -111,50 +134,61 @@ def invite_person(group_id: int, person_id: str) -> None:
|
|
| 111 |
|
| 112 |
|
| 113 |
# ===== ロジック =====
|
|
|
|
| 114 |
def process():
|
| 115 |
messages = get_messages()
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
(int(m.get("createdAt", 0))
|
| 120 |
for m in messages
|
| 121 |
-
if str(m.get("
|
| 122 |
default=0
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# 条件を満たす join メッセージを抽出
|
| 126 |
join_targets = []
|
|
|
|
| 127 |
for m in messages:
|
| 128 |
log = m.get("log") or {}
|
| 129 |
if log.get("action") != "join":
|
| 130 |
continue
|
| 131 |
-
|
| 132 |
created_at = int(m.get("createdAt", 0))
|
| 133 |
person_id = str(m.get("personId", ""))
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
continue
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# ===== 通常処理 =====
|
| 142 |
-
if created_at >
|
| 143 |
join_targets.append(person_id)
|
| 144 |
|
| 145 |
if not join_targets:
|
| 146 |
return
|
| 147 |
|
| 148 |
-
# 重複排除
|
| 149 |
join_targets = list(set(join_targets))
|
| 150 |
|
| 151 |
# 歓迎メッセージは1回だけ
|
| 152 |
post_welcome_message()
|
| 153 |
|
| 154 |
-
#
|
| 155 |
for pid in join_targets:
|
| 156 |
for gid in INVITE_GROUPS:
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
def main():
|
|
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import requests
|
| 7 |
+
from typing import List, Dict, Any, Optional
|
| 8 |
|
| 9 |
# ===== 設定 =====
|
| 10 |
X_ACCOUNT = os.getenv("dmsendertoken")
|
|
|
|
| 13 |
|
| 14 |
BASE = "https://desk-api.channel.io/desk/channels/200605"
|
| 15 |
GROUP_ID = 463667
|
| 16 |
+
CHECK_PERSON_ID = "614601"
|
| 17 |
+
|
| 18 |
+
# accountIdベースのブラックリスト
|
| 19 |
+
BLACKLIST_ACCOUNT_IDS = ["463667", "481108"]
|
| 20 |
+
|
| 21 |
HEADERS = {
|
| 22 |
"accept": "application/json",
|
| 23 |
"accept-language": "ja",
|
|
|
|
| 54 |
]
|
| 55 |
|
| 56 |
# ===== API =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def get_messages() -> List[Dict[str, Any]]:
|
| 59 |
url = f"{BASE}/groups/{GROUP_ID}/messages"
|
|
|
|
| 61 |
r.raise_for_status()
|
| 62 |
return r.json().get("messages", [])
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
def get_group_titles() -> Dict[int, str]:
|
| 66 |
+
url = f"{BASE}/groups?limit=1000"
|
| 67 |
+
response = requests.get(url, headers=HEADERS, timeout=20)
|
| 68 |
+
response.raise_for_status()
|
| 69 |
|
| 70 |
+
groups = response.json().get("groups", [])
|
| 71 |
return {
|
| 72 |
group.get("id"): group.get("title")
|
| 73 |
for group in groups
|
| 74 |
if "id" in group and "title" in group
|
| 75 |
}
|
| 76 |
|
| 77 |
+
|
| 78 |
+
def get_manager_account_id(person_id: str) -> Optional[str]:
|
| 79 |
+
"""
|
| 80 |
+
personId を使って managers API を取得し、
|
| 81 |
+
managers[0].accountId を返す
|
| 82 |
+
"""
|
| 83 |
+
url = f"{BASE}/managers"
|
| 84 |
+
params = {
|
| 85 |
+
"limit": 1,
|
| 86 |
+
"since": person_id
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
r = requests.get(url, headers=HEADERS, params=params, timeout=20)
|
| 90 |
+
r.raise_for_status()
|
| 91 |
+
|
| 92 |
+
data = r.json()
|
| 93 |
+
managers = data.get("managers", [])
|
| 94 |
+
|
| 95 |
+
if not managers:
|
| 96 |
+
return None
|
| 97 |
+
|
| 98 |
+
return str(managers[0].get("accountId"))
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def blacklist_manager(person_id: str) -> None:
|
| 102 |
+
"""
|
| 103 |
+
personId を使って manager を削除
|
| 104 |
+
"""
|
| 105 |
+
url = f"{BASE}/managers/{person_id}"
|
| 106 |
+
requests.delete(url, headers=HEADERS, timeout=20).raise_for_status()
|
| 107 |
+
print(f"[INFO] personId={person_id} を削除しました")
|
| 108 |
+
|
| 109 |
+
|
| 110 |
def post_welcome_message() -> None:
|
| 111 |
+
TITLES = get_group_titles()
|
| 112 |
|
| 113 |
welcome_text = WELCOME_TEMPLATE.format(
|
| 114 |
hq=TITLES.get(532214, "本部")
|
|
|
|
| 121 |
{"type": "text", "value": welcome_text}
|
| 122 |
]
|
| 123 |
}
|
| 124 |
+
|
| 125 |
requests.post(url, headers=HEADERS, json=payload, timeout=20).raise_for_status()
|
| 126 |
print("[INFO] 歓迎メッセージを送信しました")
|
| 127 |
|
| 128 |
|
|
|
|
| 129 |
def invite_person(group_id: int, person_id: str) -> None:
|
| 130 |
url = f"{BASE}/groups/{group_id}/invite"
|
| 131 |
params = {"managerIds": person_id}
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
# ===== ロジック =====
|
| 137 |
+
|
| 138 |
def process():
|
| 139 |
messages = get_messages()
|
| 140 |
|
| 141 |
+
# CHECK_PERSON_ID の最新発言時刻
|
| 142 |
+
latest_check_person = max(
|
| 143 |
(int(m.get("createdAt", 0))
|
| 144 |
for m in messages
|
| 145 |
+
if str(m.get("personId")) == CHECK_PERSON_ID),
|
| 146 |
default=0
|
| 147 |
)
|
| 148 |
|
|
|
|
| 149 |
join_targets = []
|
| 150 |
+
|
| 151 |
for m in messages:
|
| 152 |
log = m.get("log") or {}
|
| 153 |
if log.get("action") != "join":
|
| 154 |
continue
|
| 155 |
+
|
| 156 |
created_at = int(m.get("createdAt", 0))
|
| 157 |
person_id = str(m.get("personId", ""))
|
| 158 |
+
|
| 159 |
+
# ===== ブラックリスト判定(accountIdベース)=====
|
| 160 |
+
try:
|
| 161 |
+
account_id = get_manager_account_id(person_id)
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"[ERROR] manager取得失敗 personId={person_id} : {e}")
|
| 164 |
continue
|
| 165 |
+
|
| 166 |
+
if account_id and account_id in BLACKLIST_ACCOUNT_IDS:
|
| 167 |
+
try:
|
| 168 |
+
blacklist_manager(person_id)
|
| 169 |
+
except Exception as e:
|
| 170 |
+
print(f"[ERROR] 削除失敗 personId={person_id} : {e}")
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
# ===== 通常処理 =====
|
| 174 |
+
if created_at > latest_check_person:
|
| 175 |
join_targets.append(person_id)
|
| 176 |
|
| 177 |
if not join_targets:
|
| 178 |
return
|
| 179 |
|
|
|
|
| 180 |
join_targets = list(set(join_targets))
|
| 181 |
|
| 182 |
# 歓迎メッセージは1回だけ
|
| 183 |
post_welcome_message()
|
| 184 |
|
| 185 |
+
# 招待処理
|
| 186 |
for pid in join_targets:
|
| 187 |
for gid in INVITE_GROUPS:
|
| 188 |
+
try:
|
| 189 |
+
invite_person(gid, pid)
|
| 190 |
+
except Exception as e:
|
| 191 |
+
print(f"[ERROR] 招待失敗 personId={pid} group={gid} : {e}")
|
| 192 |
|
| 193 |
|
| 194 |
def main():
|