File size: 2,990 Bytes
8565a6f
 
 
937f26c
8565a6f
 
 
 
 
8db210d
8565a6f
 
937f26c
8565a6f
 
 
937f26c
 
8565a6f
 
937f26c
 
8565a6f
 
 
937f26c
 
8565a6f
 
 
 
 
 
 
937f26c
 
 
 
 
 
 
 
 
 
 
 
 
8565a6f
 
 
937f26c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8565a6f
 
 
 
 
 
 
 
 
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
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()