Spaces:
Running
Running
| #!/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_MESSAGES = ( | |
| "https://desk-api.channel.io/desk/channels/200605/groups/463667/messages" | |
| ) | |
| PARAMS = { | |
| "sortOrder": "desc", | |
| "limit": 34, | |
| "logFolded": "false", | |
| } | |
| HEADERS = { | |
| "accept": "application/json", | |
| "accept-language": "ja", | |
| "content-type": "application/json", | |
| "x-account": X_ACCOUNT, | |
| } | |
| TXT_WELCOME = """ようこそ""" | |
| """こんにちは。ITRSAにようこそ。自動でいくつかの部屋に招待しています。<b>使い方:</b>左側のメニューから、部屋(グループ)を開くことができます。\n・Youtubeダウンローダーは、URLを送ると自動でYoutubeをダウンロードしてくれる部屋\n・チャッピーは、ChatGPTと話せる部屋\n・幹部と裁判所からの報告はここを管理する幹部や裁判所から通知が来る部屋\n・{get_group_title(200605)}""" | |
| def get_group_title(group_id: int) -> str | None: | |
| url = f"https://desk-api.channel.io/desk/channels/{group_id}/groups?limit=1000" | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = response.json() | |
| groups = data.get("groups", []) | |
| for group in groups: | |
| if group.get("id") == group_id: | |
| return group.get("title") | |
| return None | |
| # 文字列内の {get_group_title(...)} を置換する関数 | |
| def replace_titles_in_text(text: str) -> str: | |
| pattern = r"\{get_group_title\((\d+)\)\}" | |
| def replacer(match): | |
| group_id = int(match.group(1)) | |
| title = get_group_title(group_id) | |
| return title if title else "タイトルなし" | |
| return re.sub(pattern, replacer, text) | |
| BOT_PERSON_ID = "599642" | |
| INVITE_GROUPS = [ | |
| 519217, | |
| 536194, | |
| 534868, | |
| 521995, | |
| 530062 | |
| ] | |
| # ===== API操作 ===== | |
| def fetch_messages() -> List[Dict[str, Any]]: | |
| r = requests.get(BASE_MESSAGES, headers=HEADERS, params=PARAMS, timeout=20) | |
| r.raise_for_status() | |
| return r.json().get("messages", []) | |
| def post_welcome_message() -> None: | |
| WELCOME_TEXT = replace_titles_in_text(TXT_WELCOME) | |
| payload = { | |
| "requestId": f"welcome-{int(time.time() * 1000)}", | |
| "blocks": [{"type": "text", "value": WELCOME_TEXT}], | |
| } | |
| requests.post(BASE_MESSAGES, headers=HEADERS, json=payload, timeout=20) | |
| def invite_to_group(group_id: int, manager_id: str) -> None: | |
| url = ( | |
| f"https://desk-api.channel.io/desk/channels/200605/" | |
| f"groups/{group_id}/invite?managerIds={manager_id}" | |
| ) | |
| requests.post(url, headers=HEADERS, timeout=20) | |
| # ===== ロジック ===== | |
| def process_once() -> None: | |
| messages = fetch_messages() | |
| # 599642 の発言時刻一覧(新しい順) | |
| bot_message_times = [ | |
| int(m.get("createdAt") or 0) | |
| for m in messages | |
| if str(m.get("personId")) == BOT_PERSON_ID | |
| ] | |
| target_join_persons: List[str] = [] | |
| for msg in messages: | |
| log = msg.get("log") or {} | |
| if log.get("action") != "join": | |
| continue | |
| join_created_at = int(msg.get("createdAt") or 0) | |
| join_person_id = str(msg.get("personId", "")) | |
| # この join より新しい bot 発言があるか | |
| has_newer_ | |