File size: 2,744 Bytes
8565a6f
 
 
937f26c
8565a6f
 
1145bc0
 
 
 
8565a6f
 
8db210d
8565a6f
 
937f26c
8565a6f
 
 
937f26c
8565a6f
 
937f26c
 
8565a6f
 
 
937f26c
 
8565a6f
 
1145bc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8565a6f
 
 
 
 
1145bc0
 
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
109
110
111
112
113
114
115
116
import os
import time
import requests
from datetime import datetime

BASE_URL = "https://desk-api.channel.io/desk/channels/200605"

# 監視対象グループID(配列で指定可能)
SOURCE_GROUP_IDS = ["536194", "463667"]

DEST_GROUP_ID = "536281"

TOKEN = os.getenv("dmsendertoken")

HEADERS = {
    "x-account": TOKEN,
    "Content-Type": "application/json"
}

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 process_group(source_group_id, ng_words):
    get_url = f"{BASE_URL}/groups/{source_group_id}/messages"

    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 msg in 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"処理完了: group_id={source_group_id}, message_id={msg_id}")


def main():
    ng_words = load_ng_words()

    while True:
        try:
            for group_id in SOURCE_GROUP_IDS:
                process_group(group_id, ng_words)

        except Exception as e:
            print("エラー:", e)

        time.sleep(10)


if __name__ == "__main__":
    main()