Spaces:
Building
Building
File size: 6,069 Bytes
b9b9580 30fd24c b9b9580 c821a11 b9b9580 8adbc35 b9b9580 ae415f4 30fd24c 2db2628 30fd24c c821a11 b9b9580 ae415f4 3c5ef20 cb00740 b7ed8bb cb00740 1d842be 37f221f cb1a46a 1d842be ae415f4 0a453cf ae415f4 8ad0356 c821a11 8ad0356 11065e2 30fd24c 11065e2 30fd24c 11065e2 8adbc35 30fd24c 1702200 30fd24c c821a11 30fd24c 3c5ef20 ae415f4 b9b9580 ae415f4 3c5ef20 ae415f4 b9b9580 30fd24c ae415f4 c821a11 bfd6376 ae415f4 b9b9580 8adbc35 30fd24c ae415f4 30fd24c ae415f4 30fd24c ae415f4 8ad0356 ae415f4 2911a5e ae415f4 1d842be 2911a5e ae415f4 2911a5e 30fd24c 0a453cf 2911a5e 30fd24c 2911a5e 0a453cf 2911a5e ae415f4 30fd24c ae415f4 30fd24c ae415f4 8adbc35 ae415f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import requests
from typing import List, Dict, Any, Optional
# ===== 設定 =====
X_ACCOUNT = os.getenv("dmsendertoken")
if not X_ACCOUNT:
raise RuntimeError("環境変数 dmsendertoken が設定されていません")
BASE = "https://desk-api.channel.io/desk/channels/200605"
GROUP_ID = 463667
CHECK_PERSON_ID = "614601"
# accountIdベースのブラックリスト
BLACKLIST_ACCOUNT_IDS = ["476793", "486092", "471246"]
HEADERS = {
"accept": "application/json",
"accept-language": "ja",
"content-type": "application/json",
"x-account": X_ACCOUNT,
}
GET_PARAMS = {
"sortOrder": "desc",
"limit": 34,
"logFolded": "false",
}
WELCOME_TEMPLATE = """
こんにちは。ITRSAにようこそ。
自動でいくつかの部屋に招待しています。
<b>使い方:</b>
左側のメニューから、部屋(グループ)を開くことができます。
・「あいてぃーあーるえすえい」は本部で、雑談などをする部屋です。
・「Youtubeダウンローダー」は、URLを送ると自動でYoutubeをダウンロードしてくれる部屋です。
・「チャッピーくん」は、ChatGPTと話せる部屋です。
・「幹部と裁判所からの報告」はここを管理する幹部や裁判所から通知が来る部屋です。この部屋は基本的に抜けないでください。
"""
INVITE_GROUPS = [
519217,
536194,
534868,
521995,
530062,
517180,
524837
]
# ===== API =====
def get_messages() -> List[Dict[str, Any]]:
url = f"{BASE}/groups/{GROUP_ID}/messages"
r = requests.get(url, headers=HEADERS, params=GET_PARAMS, timeout=20)
r.raise_for_status()
return r.json().get("messages", [])
def get_group_titles() -> Dict[int, str]:
url = f"{BASE}/groups?limit=1000"
response = requests.get(url, headers=HEADERS, timeout=20)
response.raise_for_status()
groups = response.json().get("groups", [])
return {
group.get("id"): group.get("title")
for group in groups
if "id" in group and "title" in group
}
def get_manager_account_id(person_id: str) -> Optional[str]:
"""
personId を使って managers API を取得し、
managers[0].accountId を返す
"""
url = f"{BASE}/managers"
params = {
"limit": 1,
"since": person_id
}
r = requests.get(url, headers=HEADERS, params=params, timeout=20)
r.raise_for_status()
data = r.json()
managers = data.get("managers", [])
if not managers:
return None
return str(managers[0].get("accountId"))
def blacklist_manager(person_id: str) -> None:
"""
personId を使って manager を削除
"""
url = f"{BASE}/managers/{person_id}"
requests.delete(url, headers=HEADERS, timeout=20).raise_for_status()
payload = {
"requestId": f"desk-web-{int(time.time() * 1000)}",
"blocks": [
{"type": "text", "value": "さようなら。"}
]
}
url = f"{BASE}/groups/{GROUP_ID}/messages"
requests.post(url, headers=HEADERS, json=payload, timeout=20).raise_for_status()
print(f"[INFO] personId={person_id} を削除しました")
def post_welcome_message() -> None:
TITLES = get_group_titles()
welcome_text = WELCOME_TEMPLATE.format(
hq=TITLES.get(532214, "本部")
)
url = f"{BASE}/groups/{GROUP_ID}/messages"
payload = {
"requestId": f"desk-web-{int(time.time() * 1000)}",
"blocks": [
{"type": "text", "value": welcome_text}
]
}
requests.post(url, headers=HEADERS, json=payload, timeout=20).raise_for_status()
print("[INFO] 歓迎メッセージを送信しました")
def invite_person(group_id: int, person_id: str) -> None:
url = f"{BASE}/groups/{group_id}/invite"
params = {"managerIds": person_id}
requests.post(url, headers=HEADERS, params=params, timeout=20).raise_for_status()
print(f"[INFO] personId={person_id} を group {group_id} に招待しました")
# ===== ロジック =====
def process():
messages = get_messages()
# CHECK_PERSON_ID の最新発言時刻
latest_check_person = max(
(int(m.get("createdAt", 0))
for m in messages
if str(m.get("personId")) == CHECK_PERSON_ID),
default=0
)
join_targets = []
for m in messages:
log = m.get("log") or {}
if log.get("action") != "join":
continue
created_at = int(m.get("createdAt", 0))
person_id = str(m.get("personId", ""))
# ===== ここを追加 =====
# CHECK_PERSON_ID の最新発言より前なら無視
if created_at <= latest_check_person:
continue
# ===== ブラックリスト判定(accountIdベース)=====
try:
account_id = get_manager_account_id(person_id)
except Exception as e:
print(f"[ERROR] manager取得失敗 personId={person_id} : {e}")
continue
if account_id and account_id in BLACKLIST_ACCOUNT_IDS:
try:
blacklist_manager(person_id)
except Exception as e:
print(f"[ERROR] 削除失敗 personId={person_id} : {e}")
continue
# ===== 通常処理 =====
join_targets.append(person_id)
if not join_targets:
return
join_targets = list(set(join_targets))
# 歓迎メッセージは1回だけ
post_welcome_message()
# 招待処理
for pid in join_targets:
for gid in INVITE_GROUPS:
try:
invite_person(gid, pid)
except Exception as e:
print(f"[ERROR] 招待失敗 personId={pid} group={gid} : {e}")
def main():
print("[INFO] Bot 起動(10秒間隔)")
while True:
try:
process()
except Exception as e:
print(f"[ERROR] {e}")
time.sleep(10)
if __name__ == "__main__":
main() |