Spaces:
Running
Running
Update join.py
Browse files
join.py
CHANGED
|
@@ -11,14 +11,14 @@ X_ACCOUNT = os.getenv("dmsendertoken")
|
|
| 11 |
if not X_ACCOUNT:
|
| 12 |
raise RuntimeError("環境変数 dmsendertoken が設定されていません")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
HEADERS = {
|
| 24 |
"accept": "application/json",
|
|
@@ -29,100 +29,57 @@ HEADERS = {
|
|
| 29 |
|
| 30 |
WELCOME_TEXT = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def fetch_messages() -> List[Dict[str, Any]]:
|
| 35 |
-
|
| 36 |
-
params = {
|
| 37 |
-
"sortOrder": "desc",
|
| 38 |
-
"limit": WATCH_LIMIT,
|
| 39 |
-
"logFolded": "false",
|
| 40 |
-
}
|
| 41 |
-
r = requests.get(url, headers=HEADERS, params=params, timeout=20)
|
| 42 |
r.raise_for_status()
|
| 43 |
return r.json().get("messages", [])
|
| 44 |
|
| 45 |
|
| 46 |
def post_welcome_message() -> None:
|
| 47 |
-
url = f"{BASE}/groups/{GROUP_ID}/messages"
|
| 48 |
payload = {
|
| 49 |
-
"requestId": f"
|
| 50 |
-
"blocks": [
|
| 51 |
-
{"type": "text", "value": WELCOME_TEXT}
|
| 52 |
-
]
|
| 53 |
}
|
| 54 |
-
|
| 55 |
-
r.raise_for_status()
|
| 56 |
-
print("[INFO] 歓迎メッセージを送信しました")
|
| 57 |
|
| 58 |
|
| 59 |
-
def
|
| 60 |
-
url =
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
|
| 67 |
# ===== ロジック =====
|
| 68 |
-
def
|
| 69 |
messages = fetch_messages()
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
if
|
| 76 |
-
join_logs.append(msg)
|
| 77 |
-
|
| 78 |
-
if not join_logs:
|
| 79 |
-
return
|
| 80 |
-
|
| 81 |
-
# 599642 の最新メッセージ時刻
|
| 82 |
-
latest_welcome_sender_time = max(
|
| 83 |
-
(int(m.get("createdAt") or 0)
|
| 84 |
-
for m in messages
|
| 85 |
-
if str(m.get("personId")) == WELCOME_SENDER_ID),
|
| 86 |
-
default=0
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# 599642 の発言「以降」にある join を対象にする
|
| 90 |
-
target_joins = [
|
| 91 |
-
m for m in join_logs
|
| 92 |
-
if int(m.get("createdAt") or 0) > latest_welcome_sender_time
|
| 93 |
]
|
| 94 |
|
| 95 |
-
|
| 96 |
-
return
|
| 97 |
-
|
| 98 |
-
# 招待対象 personId を重複排除
|
| 99 |
-
invite_person_ids = {
|
| 100 |
-
str(m.get("personId"))
|
| 101 |
-
for m in target_joins
|
| 102 |
-
if m.get("personId")
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
if not invite_person_ids:
|
| 106 |
-
return
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
for pid in invite_person_ids:
|
| 113 |
-
for gid in INVITE_GROUPS:
|
| 114 |
-
invite_person(gid, pid)
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
def main():
|
| 118 |
-
print("[INFO] Bot 起動(10秒ごとにチェック)")
|
| 119 |
-
while True:
|
| 120 |
-
try:
|
| 121 |
-
process()
|
| 122 |
-
except Exception as e:
|
| 123 |
-
print(f"[ERROR] 処理中に例外: {e}")
|
| 124 |
-
time.sleep(CHECK_INTERVAL)
|
| 125 |
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 11 |
if not X_ACCOUNT:
|
| 12 |
raise RuntimeError("環境変数 dmsendertoken が設定されていません")
|
| 13 |
|
| 14 |
+
BASE_MESSAGES = (
|
| 15 |
+
"https://desk-api.channel.io/desk/channels/200605/groups/463667/messages"
|
| 16 |
+
)
|
| 17 |
+
PARAMS = {
|
| 18 |
+
"sortOrder": "desc",
|
| 19 |
+
"limit": 34,
|
| 20 |
+
"logFolded": "false",
|
| 21 |
+
}
|
| 22 |
|
| 23 |
HEADERS = {
|
| 24 |
"accept": "application/json",
|
|
|
|
| 29 |
|
| 30 |
WELCOME_TEXT = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
|
| 31 |
|
| 32 |
+
BOT_PERSON_ID = "599642"
|
| 33 |
|
| 34 |
+
INVITE_GROUPS = [
|
| 35 |
+
519217,
|
| 36 |
+
536194,
|
| 37 |
+
534868,
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
# ===== API操作 =====
|
| 41 |
def fetch_messages() -> List[Dict[str, Any]]:
|
| 42 |
+
r = requests.get(BASE_MESSAGES, headers=HEADERS, params=PARAMS, timeout=20)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
r.raise_for_status()
|
| 44 |
return r.json().get("messages", [])
|
| 45 |
|
| 46 |
|
| 47 |
def post_welcome_message() -> None:
|
|
|
|
| 48 |
payload = {
|
| 49 |
+
"requestId": f"welcome-{int(time.time() * 1000)}",
|
| 50 |
+
"blocks": [{"type": "text", "value": WELCOME_TEXT}],
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
+
requests.post(BASE_MESSAGES, headers=HEADERS, json=payload, timeout=20)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
+
def invite_to_group(group_id: int, manager_id: str) -> None:
|
| 56 |
+
url = (
|
| 57 |
+
f"https://desk-api.channel.io/desk/channels/200605/"
|
| 58 |
+
f"groups/{group_id}/invite?managerIds={manager_id}"
|
| 59 |
+
)
|
| 60 |
+
requests.post(url, headers=HEADERS, timeout=20)
|
| 61 |
|
| 62 |
|
| 63 |
# ===== ロジック =====
|
| 64 |
+
def process_once() -> None:
|
| 65 |
messages = fetch_messages()
|
| 66 |
|
| 67 |
+
# 599642 の発言時刻一覧(新しい順)
|
| 68 |
+
bot_message_times = [
|
| 69 |
+
int(m.get("createdAt") or 0)
|
| 70 |
+
for m in messages
|
| 71 |
+
if str(m.get("personId")) == BOT_PERSON_ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
]
|
| 73 |
|
| 74 |
+
target_join_persons: List[str] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
for msg in messages:
|
| 77 |
+
log = msg.get("log") or {}
|
| 78 |
+
if log.get("action") != "join":
|
| 79 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
join_created_at = int(msg.get("createdAt") or 0)
|
| 82 |
+
join_person_id = str(msg.get("personId", ""))
|
| 83 |
|
| 84 |
+
# この join より新しい bot 発言があるか
|
| 85 |
+
has_newer_
|