Spaces:
Sleeping
Sleeping
Update join.py
Browse files
join.py
CHANGED
|
@@ -4,19 +4,21 @@
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
import requests
|
| 7 |
-
from typing import Dict, Any
|
| 8 |
|
| 9 |
-
#
|
| 10 |
X_ACCOUNT = os.getenv("dmsendertoken")
|
| 11 |
if not X_ACCOUNT:
|
| 12 |
-
raise RuntimeError("環境変数 dmsendertoken
|
| 13 |
|
| 14 |
GROUP_ID = 463667
|
| 15 |
-
WATCH_PERSON_ID = "599642"
|
| 16 |
-
BASE_MESSAGES = f"https://desk-api.channel.io/desk/channels/200605/groups/{GROUP_ID}/messages"
|
| 17 |
|
| 18 |
INVITE_GROUPS = [519217, 536194, 534868]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
PARAMS = {
|
| 21 |
"sortOrder": "desc",
|
| 22 |
"limit": 34,
|
|
@@ -30,96 +32,80 @@ HEADERS = {
|
|
| 30 |
"x-account": X_ACCOUNT,
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
def
|
| 38 |
-
|
|
|
|
| 39 |
r.raise_for_status()
|
| 40 |
return r.json().get("messages", [])
|
| 41 |
|
| 42 |
|
| 43 |
def post_message(text: str) -> None:
|
|
|
|
| 44 |
payload = {
|
| 45 |
"requestId": f"desk-web-{int(time.time() * 1000)}",
|
| 46 |
"blocks": [{"type": "text", "value": text}],
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
r.raise_for_status()
|
| 50 |
-
print("[INFO] ウェルカムメッセージ送信")
|
| 51 |
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
url = (
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
f"?managerIds={manager_id}"
|
| 58 |
-
)
|
| 59 |
-
r = requests.post(url, headers=HEADERS, timeout=20)
|
| 60 |
-
r.raise_for_status()
|
| 61 |
-
print(f"[INFO] group {group_id} に personId={manager_id} を招待")
|
| 62 |
|
| 63 |
|
| 64 |
-
#
|
| 65 |
def process():
|
| 66 |
-
messages =
|
| 67 |
-
|
| 68 |
-
# personId=599642 の最新メッセージ時刻
|
| 69 |
-
latest_watch_created_at = max(
|
| 70 |
-
(int(m.get("createdAt") or 0)
|
| 71 |
-
for m in messages
|
| 72 |
-
if str(m.get("personId", "")) == WATCH_PERSON_ID),
|
| 73 |
-
default=0
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
join_events = []
|
| 77 |
-
|
| 78 |
-
for msg in messages:
|
| 79 |
-
created_at = int(msg.get("createdAt") or 0)
|
| 80 |
-
logs = msg.get("logs") or []
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
str(m.get("personId", "")) == WATCH_PERSON_ID
|
| 91 |
-
and int(m.get("createdAt") or 0) > created_at
|
| 92 |
-
for m in messages
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
"person_id": join_person_id,
|
| 98 |
-
"has_watch_after": has_watch_after,
|
| 99 |
-
})
|
| 100 |
|
| 101 |
-
if not
|
| 102 |
return
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if need_welcome:
|
| 110 |
-
post_message(WELCOME_MESSAGE)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
for
|
| 114 |
-
|
|
|
|
| 115 |
continue
|
| 116 |
-
|
| 117 |
for gid in INVITE_GROUPS:
|
| 118 |
-
|
| 119 |
|
| 120 |
|
| 121 |
-
def
|
| 122 |
-
print("[INFO]
|
| 123 |
while True:
|
| 124 |
try:
|
| 125 |
process()
|
|
@@ -129,4 +115,4 @@ def main_loop():
|
|
| 129 |
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
-
|
|
|
|
| 4 |
import os
|
| 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 |
GROUP_ID = 463667
|
| 15 |
+
WATCH_PERSON_ID = "599642"
|
|
|
|
| 16 |
|
| 17 |
INVITE_GROUPS = [519217, 536194, 534868]
|
| 18 |
|
| 19 |
+
BASE_MESSAGES = "https://desk-api.channel.io/desk/channels/200605/groups/{group_id}/messages"
|
| 20 |
+
BASE_INVITE = "https://desk-api.channel.io/desk/channels/200605/groups/{group_id}/invite"
|
| 21 |
+
|
| 22 |
PARAMS = {
|
| 23 |
"sortOrder": "desc",
|
| 24 |
"limit": 34,
|
|
|
|
| 32 |
"x-account": X_ACCOUNT,
|
| 33 |
}
|
| 34 |
|
| 35 |
+
WELCOME_TEXT = "こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。"
|
| 36 |
|
| 37 |
|
| 38 |
+
# ===== API =====
|
| 39 |
+
def get_messages() -> List[Dict[str, Any]]:
|
| 40 |
+
url = BASE_MESSAGES.format(group_id=GROUP_ID)
|
| 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_message(text: str) -> None:
|
| 47 |
+
url = BASE_MESSAGES.format(group_id=GROUP_ID)
|
| 48 |
payload = {
|
| 49 |
"requestId": f"desk-web-{int(time.time() * 1000)}",
|
| 50 |
"blocks": [{"type": "text", "value": text}],
|
| 51 |
}
|
| 52 |
+
requests.post(url, headers=HEADERS, json=payload, timeout=20)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
+
def invite(group_id: int, person_id: str) -> None:
|
| 56 |
+
url = BASE_INVITE.format(group_id=group_id)
|
| 57 |
+
params = {"managerIds": person_id}
|
| 58 |
+
requests.post(url, headers=HEADERS, params=params, timeout=20)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
+
# ===== ロジック =====
|
| 62 |
def process():
|
| 63 |
+
messages = get_messages()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# personId == 599642 の最新メッセージ時刻
|
| 66 |
+
latest_watch_created = max(
|
| 67 |
+
(int(m.get("createdAt") or 0) for m in messages if str(m.get("personId")) == WATCH_PERSON_ID),
|
| 68 |
+
default=0,
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# join ログ抽出
|
| 72 |
+
join_logs = []
|
| 73 |
+
for m in messages:
|
| 74 |
+
log = m.get("log") or {}
|
| 75 |
+
if log.get("action") == "join":
|
| 76 |
+
join_logs.append({
|
| 77 |
+
"createdAt": int(m.get("createdAt") or 0),
|
| 78 |
+
"personId": str(m.get("personId") or ""),
|
| 79 |
+
})
|
| 80 |
|
| 81 |
+
if not join_logs:
|
| 82 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# 599642 以降の join
|
| 85 |
+
targets = [j for j in join_logs if j["createdAt"] > latest_watch_created]
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
if not targets:
|
| 88 |
return
|
| 89 |
|
| 90 |
+
# 先頭 join に対してのみ挨拶判定
|
| 91 |
+
should_send_welcome = latest_watch_created == 0 or (
|
| 92 |
+
targets and targets[0]["createdAt"] > latest_watch_created
|
| 93 |
+
)
|
| 94 |
|
| 95 |
+
if should_send_welcome:
|
| 96 |
+
post_message(WELCOME_TEXT)
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# invite は全 join 対象に実行
|
| 99 |
+
for j in targets:
|
| 100 |
+
pid = j["personId"]
|
| 101 |
+
if not pid:
|
| 102 |
continue
|
|
|
|
| 103 |
for gid in INVITE_GROUPS:
|
| 104 |
+
invite(gid, pid)
|
| 105 |
|
| 106 |
|
| 107 |
+
def main():
|
| 108 |
+
print("[INFO] bot start (10秒間隔)")
|
| 109 |
while True:
|
| 110 |
try:
|
| 111 |
process()
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
main()
|