Spaces:
Running
Running
File size: 4,182 Bytes
b9b9580 8adbc35 b9b9580 c821a11 b9b9580 8adbc35 b9b9580 ae415f4 3c5ef20 c821a11 b9b9580 ae415f4 3c5ef20 cb00740 b7ed8bb cb00740 1d842be 37f221f 1d842be ae415f4 8ad0356 c821a11 8ad0356 11065e2 2eb6a83 11065e2 8adbc35 c821a11 3c5ef20 ae415f4 b9b9580 ae415f4 3c5ef20 ae415f4 b9b9580 ae415f4 c821a11 bfd6376 3c5ef20 ae415f4 b9b9580 8adbc35 ae415f4 8ad0356 ae415f4 1d842be 8ad0356 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 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import requests
from typing import List, Dict, Any
# ===== 設定 =====
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 = "604730"
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
]
# ===== 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 = "https://desk-api.channel.io/desk/channels/200605/groups?limit=1000"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
groups = data.get("groups", [])
return {
group.get("id"): group.get("title")
for group in groups
if "id" in group and "title" in group
}
def post_welcome_message() -> None:
TITLES = get_group_titles() # {group_id: title}
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()
# personId=599642 の最新発言時刻
latest_599642 = max(
(int(m.get("createdAt", 0))
for m in messages
if str(m.get("personId")) == CHECK_PERSON_ID),
default=0
)
# 条件を満たす join メッセージを抽出
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", ""))
# join 以降に 599642 の発言が無い
if created_at > latest_599642:
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:
invite_person(gid, pid)
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() |