Spaces:
Running
Running
Update dmsender.py
Browse files- dmsender.py +54 -12
dmsender.py
CHANGED
|
@@ -3,6 +3,9 @@ import requests
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
|
|
|
|
|
|
|
|
|
|
| 6 |
TARGET_FORWARD_CHAT_ID = "463667"
|
| 7 |
SKIP_PERSON_ID = 604730
|
| 8 |
|
|
@@ -51,12 +54,14 @@ def post_message(chat_id, body):
|
|
| 51 |
r.raise_for_status()
|
| 52 |
return r.json()
|
| 53 |
|
|
|
|
| 54 |
def post_message2(chat_id, body):
|
| 55 |
url = f"{BASE_URL}/groups/{chat_id}/messages"
|
| 56 |
r = requests.post(url, headers=HEADERS, json=body)
|
| 57 |
r.raise_for_status()
|
| 58 |
return r.json()
|
| 59 |
-
|
|
|
|
| 60 |
def get_messages(chat_id):
|
| 61 |
url = (
|
| 62 |
f"{BASE_URL}/direct-chats/{chat_id}/messages"
|
|
@@ -74,12 +79,35 @@ def mark_messages_read(chat_id):
|
|
| 74 |
|
| 75 |
|
| 76 |
def find_latest_message(messages):
|
| 77 |
-
"""
|
| 78 |
-
messages 配列の中から updatedAt が最大の message を返す
|
| 79 |
-
"""
|
| 80 |
return max(messages, key=lambda m: m.get("updatedAt", 0))
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def main_loop():
|
| 84 |
while True:
|
| 85 |
try:
|
|
@@ -95,41 +123,55 @@ def main_loop():
|
|
| 95 |
if not chat_id:
|
| 96 |
continue
|
| 97 |
|
| 98 |
-
# messages を取得
|
| 99 |
msg_data = get_messages(chat_id)
|
| 100 |
messages = msg_data.get("messages", [])
|
| 101 |
if not messages:
|
| 102 |
continue
|
| 103 |
|
| 104 |
-
# 最新メッセージ取得
|
| 105 |
latest = find_latest_message(messages)
|
| 106 |
|
| 107 |
-
# personId が 604730 の場合はスキップ
|
| 108 |
person_id = latest.get("personId")
|
| 109 |
if person_id == SKIP_PERSON_ID:
|
| 110 |
mark_messages_read(chat_id)
|
| 111 |
continue
|
| 112 |
|
| 113 |
-
# ①
|
| 114 |
post_message(chat_id, SEND_BODY_TEMPLATE)
|
| 115 |
|
| 116 |
blocks = latest.get("blocks")
|
| 117 |
files = latest.get("files")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
#
|
| 120 |
forward_body = FORWARD_BODY_TEMPLATE.copy()
|
| 121 |
forward_body["blocks"] = blocks
|
| 122 |
-
forward_body["files"] =
|
| 123 |
|
| 124 |
post_message2(TARGET_FORWARD_CHAT_ID, forward_body)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
mark_messages_read(chat_id)
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
print("Error:", e)
|
| 131 |
|
| 132 |
-
# 5秒待機
|
| 133 |
time.sleep(5)
|
| 134 |
|
| 135 |
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
|
| 6 |
+
MEDIA_BASE = "https://media.channel.io/cht/v1"
|
| 7 |
+
|
| 8 |
+
CHANNEL_ID = "200605"
|
| 9 |
TARGET_FORWARD_CHAT_ID = "463667"
|
| 10 |
SKIP_PERSON_ID = 604730
|
| 11 |
|
|
|
|
| 54 |
r.raise_for_status()
|
| 55 |
return r.json()
|
| 56 |
|
| 57 |
+
|
| 58 |
def post_message2(chat_id, body):
|
| 59 |
url = f"{BASE_URL}/groups/{chat_id}/messages"
|
| 60 |
r = requests.post(url, headers=HEADERS, json=body)
|
| 61 |
r.raise_for_status()
|
| 62 |
return r.json()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
def get_messages(chat_id):
|
| 66 |
url = (
|
| 67 |
f"{BASE_URL}/direct-chats/{chat_id}/messages"
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
def find_latest_message(messages):
|
|
|
|
|
|
|
|
|
|
| 82 |
return max(messages, key=lambda m: m.get("updatedAt", 0))
|
| 83 |
|
| 84 |
|
| 85 |
+
# ===== ファイル関連 =====
|
| 86 |
+
def download_file(group_chat_id, file_key):
|
| 87 |
+
url = (
|
| 88 |
+
f"{MEDIA_BASE}/desk/channels/{CHANNEL_ID}"
|
| 89 |
+
f"/groups/{group_chat_id}/messages/file"
|
| 90 |
+
)
|
| 91 |
+
params = {"key": file_key}
|
| 92 |
+
r = requests.get(url, headers=HEADERS, params=params)
|
| 93 |
+
r.raise_for_status()
|
| 94 |
+
return r.content # バイナリ
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def upload_file_to_target(file_name, file_content):
|
| 98 |
+
url = (
|
| 99 |
+
f"{MEDIA_BASE}/pri-file/{CHANNEL_ID}"
|
| 100 |
+
f"/groups/{TARGET_FORWARD_CHAT_ID}/message/{file_name}"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
headers = HEADERS.copy()
|
| 104 |
+
headers.pop("Content-Type", None)
|
| 105 |
+
|
| 106 |
+
r = requests.post(url, headers=headers, data=file_content)
|
| 107 |
+
r.raise_for_status()
|
| 108 |
+
return r.json() # files にそのまま使う
|
| 109 |
+
|
| 110 |
+
|
| 111 |
def main_loop():
|
| 112 |
while True:
|
| 113 |
try:
|
|
|
|
| 123 |
if not chat_id:
|
| 124 |
continue
|
| 125 |
|
|
|
|
| 126 |
msg_data = get_messages(chat_id)
|
| 127 |
messages = msg_data.get("messages", [])
|
| 128 |
if not messages:
|
| 129 |
continue
|
| 130 |
|
|
|
|
| 131 |
latest = find_latest_message(messages)
|
| 132 |
|
|
|
|
| 133 |
person_id = latest.get("personId")
|
| 134 |
if person_id == SKIP_PERSON_ID:
|
| 135 |
mark_messages_read(chat_id)
|
| 136 |
continue
|
| 137 |
|
| 138 |
+
# ① 送信しました
|
| 139 |
post_message(chat_id, SEND_BODY_TEMPLATE)
|
| 140 |
|
| 141 |
blocks = latest.get("blocks")
|
| 142 |
files = latest.get("files")
|
| 143 |
+
new_files = []
|
| 144 |
+
|
| 145 |
+
# ===== ファイル転送処理 =====
|
| 146 |
+
if files:
|
| 147 |
+
source_group_id = latest.get("chatId")
|
| 148 |
+
|
| 149 |
+
for f in files:
|
| 150 |
+
file_key = f.get("key")
|
| 151 |
+
file_name = f.get("name")
|
| 152 |
+
|
| 153 |
+
if not file_key or not file_name:
|
| 154 |
+
continue
|
| 155 |
+
|
| 156 |
+
file_content = download_file(source_group_id, file_key)
|
| 157 |
+
uploaded_file_json = upload_file_to_target(
|
| 158 |
+
file_name, file_content
|
| 159 |
+
)
|
| 160 |
+
new_files.append(uploaded_file_json)
|
| 161 |
|
| 162 |
+
# ===== 固定IDへ転送 =====
|
| 163 |
forward_body = FORWARD_BODY_TEMPLATE.copy()
|
| 164 |
forward_body["blocks"] = blocks
|
| 165 |
+
forward_body["files"] = new_files if new_files else None
|
| 166 |
|
| 167 |
post_message2(TARGET_FORWARD_CHAT_ID, forward_body)
|
| 168 |
|
| 169 |
+
# 既読
|
| 170 |
mark_messages_read(chat_id)
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
print("Error:", e)
|
| 174 |
|
|
|
|
| 175 |
time.sleep(5)
|
| 176 |
|
| 177 |
|