import os import time import requests from datetime import datetime BASE_URL = "https://desk-api.channel.io/desk/channels/200605" SOURCE_GROUP_ID = "536194" DEST_GROUP_ID = "536281" TOKEN = os.getenv("dmsendertoken") HEADERS = { "x-account": TOKEN, "Content-Type": "application/json" } GET_URL = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages" POST_URL = f"{BASE_URL}/groups/{DEST_GROUP_ID}/messages" def load_ng_words(path="ngwords.txt"): with open(path, encoding="utf-8") as f: return [line.strip() for line in f if line.strip()] def contains_ng_word(text, ng_words): return any(word in text for word in ng_words) def main(): ng_words = load_ng_words() while True: try: resp = requests.get( GET_URL, headers=HEADERS, params={ "sortOrder": "desc", "limit": 34, "logFolded": "false" } ) resp.raise_for_status() data = resp.json() messages = data.get("messages", []) for i, msg in enumerate(messages): plain_text = msg.get("plainText", "") if not plain_text: continue if not contains_ng_word(plain_text, ng_words): continue person_id = msg.get("personId") created_at = msg.get("createdAt") blocks = msg.get("blocks") or [] files = msg.get("files") # 先頭に送信者情報ブロックを追加 info_block = { "type": "text", "value": f"送信者:{person_id}、送信エポックミリ秒:{created_at}" } new_blocks = [info_block] + blocks post_body = { "requestId": f"desk-web-{int(time.time() * 1000)}", "blocks": new_blocks, "buttons": None, "form": None, "webPage": None, "files": files if files else None, "customPayload": None } # POST post_resp = requests.post( POST_URL, headers=HEADERS, json=post_body ) post_resp.raise_for_status() # DELETE 元メッセージ msg_id = msg.get("id") delete_url = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages/{msg_id}" del_resp = requests.delete( delete_url, headers=HEADERS ) del_resp.raise_for_status() print(f"処理完了: message_id={msg_id}") except Exception as e: print("エラー:", e) time.sleep(10) if __name__ == "__main__": main()