File size: 4,278 Bytes
768c682
 
 
 
 
989948d
846dbd6
768c682
b3bc0d4
768c682
 
c3334da
 
768c682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20cca16
768c682
 
 
 
846dbd6
989948d
20cca16
 
 
 
846dbd6
 
768c682
 
 
 
 
 
 
 
 
 
b3bc0d4
 
 
 
 
 
768c682
 
 
 
989948d
846dbd6
989948d
 
846dbd6
989948d
846dbd6
989948d
846dbd6
 
989948d
846dbd6
989948d
 
846dbd6
989948d
 
 
 
 
846dbd6
989948d
846dbd6
 
768c682
 
 
 
 
 
 
989948d
b3bc0d4
 
 
 
 
 
 
 
 
 
 
 
 
989948d
b3bc0d4
 
 
846dbd6
b3bc0d4
 
989948d
 
 
 
 
 
 
 
 
 
 
b3bc0d4
989948d
 
b3bc0d4
989948d
b3bc0d4
989948d
b3bc0d4
768c682
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import time
import requests
import os

BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
MEDIA_BASE_URL = "https://media.channel.io/cht/v1"

TARGET_FORWARD_CHAT_ID = "463667"
SKIP_PERSON_ID = 604730

HEADERS = {
    "accept-language": "ja",
    "x-account": os.getenv("dmsendertoken")
}

SEND_BODY_TEMPLATE = {
    "requestId": "desk-web-1768737053326fv6n",
    "blocks": [
        {
            "type": "text",
            "value": "送信しました"
        }
    ],
    "buttons": None,
    "form": None,
    "webPage": None,
    "files": None,
    "customPayload": None
}

FORWARD_BODY_TEMPLATE = {
    "requestId": "desk-web-1768736747518jhqp",
    "blocks": None,
    "buttons": None,
    "form": None,
    "webPage": None,
    "files": None,
    "customPayload": None
}


def get_direct_chats():
    url = f"{BASE_URL}/direct-chats?limit=1200"
    r = requests.get(url, headers=HEADERS)
    r.raise_for_status()
    return r.json()


def post_message(chat_id, body):
    url = f"{BASE_URL}/direct-chats/{chat_id}/messages"
    r = requests.post(url, headers=HEADERS, json=body)
    r.raise_for_status()
    return r.json()


def post_group_message(chat_id, body):
    url = f"{BASE_URL}/groups/{chat_id}/messages"
    r = requests.post(url, headers=HEADERS, json=body)
    r.raise_for_status()
    return r.json()


def get_messages(chat_id):
    url = (
        f"{BASE_URL}/direct-chats/{chat_id}/messages"
        "?sortOrder=desc&limit=36&logFolded=false"
    )
    r = requests.get(url, headers=HEADERS)
    r.raise_for_status()
    return r.json()


def mark_messages_read(chat_id):
    url = f"{BASE_URL}/direct-chats/{chat_id}/messages/read"
    r = requests.put(url, headers=HEADERS)
    r.raise_for_status()


def find_latest_message(messages):
    return max(messages, key=lambda m: m.get("updatedAt", 0))


def fetch_original_file(chat_id, file_key):
    url = (
        f"{MEDIA_BASE_URL}/desk/channels/200605/"
        f"direct-chats/{chat_id}/messages/file"
    )
    r = requests.get(url, headers=HEADERS, params={"key": file_key})
    r.raise_for_status()
    return r.content


def upload_file_to_group(file_name, file_bytes):
    url = (
        f"{MEDIA_BASE_URL}/pri-file/200605/"
        f"groups/{TARGET_FORWARD_CHAT_ID}/message/{file_name}"
    )
    headers = {
        "x-account": os.getenv("dmsendertoken"),
        "Content-Type": "application/octet-stream"
    }
    r = requests.post(url, headers=headers, data=file_bytes)
    r.raise_for_status()
    return r.json()


def main_loop():
    while True:
        try:
            data = get_direct_chats()
            sessions = data.get("sessions", [])

            for session in sessions:
                if session.get("unread", 0) < 1:
                    continue

                chat_id = session.get("chatId")
                if not chat_id:
                    continue

                msg_data = get_messages(chat_id)
                messages = msg_data.get("messages", [])
                if not messages:
                    continue

                latest = find_latest_message(messages)

                if latest.get("personId") == SKIP_PERSON_ID:
                    mark_messages_read(chat_id)
                    continue

                # ① 送信しました
                post_message(chat_id, SEND_BODY_TEMPLATE)

                # ② ファイル処理
                uploaded_files = []
                for f in latest.get("files", []) or []:
                    original_bytes = fetch_original_file(chat_id, f["key"])
                    uploaded_file = upload_file_to_group(
                        f["name"],
                        original_bytes
                    )
                    uploaded_files.append(uploaded_file)

                # ③ 転送
                forward_body = FORWARD_BODY_TEMPLATE.copy()
                forward_body["blocks"] = latest.get("blocks")
                forward_body["files"] = uploaded_files if uploaded_files else None

                post_group_message(TARGET_FORWARD_CHAT_ID, forward_body)

                # ④ 既読
                mark_messages_read(chat_id)

        except Exception as e:
            print("Error:", e)

        time.sleep(5)


if __name__ == "__main__":
    main_loop()