izuemon commited on
Commit
989948d
·
verified ·
1 Parent(s): 9527160

Update dmsender.py

Browse files
Files changed (1) hide show
  1. dmsender.py +33 -50
dmsender.py CHANGED
@@ -3,14 +3,12 @@ import requests
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
 
12
  HEADERS = {
13
- "Content-Type": "application/json",
14
  "accept-language": "ja",
15
  "x-account": os.getenv("dmsendertoken")
16
  }
@@ -55,7 +53,7 @@ def post_message(chat_id, body):
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()
@@ -82,30 +80,28 @@ 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():
@@ -115,8 +111,7 @@ def main_loop():
115
  sessions = data.get("sessions", [])
116
 
117
  for session in sessions:
118
- unread = session.get("unread", 0)
119
- if unread < 1:
120
  continue
121
 
122
  chat_id = session.get("chatId")
@@ -130,43 +125,31 @@ def main_loop():
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:
 
3
  import os
4
 
5
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
6
+ MEDIA_BASE_URL = "https://media.channel.io/cht/v1"
7
 
 
8
  TARGET_FORWARD_CHAT_ID = "463667"
9
  SKIP_PERSON_ID = 604730
10
 
11
  HEADERS = {
 
12
  "accept-language": "ja",
13
  "x-account": os.getenv("dmsendertoken")
14
  }
 
53
  return r.json()
54
 
55
 
56
+ def post_group_message(chat_id, body):
57
  url = f"{BASE_URL}/groups/{chat_id}/messages"
58
  r = requests.post(url, headers=HEADERS, json=body)
59
  r.raise_for_status()
 
80
  return max(messages, key=lambda m: m.get("updatedAt", 0))
81
 
82
 
83
+ def fetch_original_file(chat_id, file_key):
 
84
  url = (
85
+ f"{MEDIA_BASE_URL}/desk/channels/200605/"
86
+ f"direct-chats/{chat_id}/messages/file"
87
  )
88
+ r = requests.get(url, headers=HEADERS, params={"key": file_key})
 
89
  r.raise_for_status()
90
+ return r.content
91
 
92
 
93
+ def upload_file_to_group(file_name, file_bytes):
94
  url = (
95
+ f"{MEDIA_BASE_URL}/pri-file/200605/"
96
+ f"groups/{TARGET_FORWARD_CHAT_ID}/message/{file_name}"
97
  )
98
+ headers = {
99
+ "x-account": os.getenv("dmsendertoken"),
100
+ "Content-Type": "application/octet-stream"
101
+ }
102
+ r = requests.post(url, headers=headers, data=file_bytes)
103
  r.raise_for_status()
104
+ return r.json()
105
 
106
 
107
  def main_loop():
 
111
  sessions = data.get("sessions", [])
112
 
113
  for session in sessions:
114
+ if session.get("unread", 0) < 1:
 
115
  continue
116
 
117
  chat_id = session.get("chatId")
 
125
 
126
  latest = find_latest_message(messages)
127
 
128
+ if latest.get("personId") == SKIP_PERSON_ID:
 
129
  mark_messages_read(chat_id)
130
  continue
131
 
132
  # ① 送信しました
133
  post_message(chat_id, SEND_BODY_TEMPLATE)
134
 
135
+ # ファイル処理
136
+ uploaded_files = []
137
+ for f in latest.get("files", []) or []:
138
+ original_bytes = fetch_original_file(chat_id, f["key"])
139
+ uploaded_file = upload_file_to_group(
140
+ f["name"],
141
+ original_bytes
142
+ )
143
+ uploaded_files.append(uploaded_file)
144
+
145
+ # 転送
 
 
 
 
 
 
 
 
 
 
 
146
  forward_body = FORWARD_BODY_TEMPLATE.copy()
147
+ forward_body["blocks"] = latest.get("blocks")
148
+ forward_body["files"] = uploaded_files if uploaded_files else None
149
 
150
+ post_group_message(TARGET_FORWARD_CHAT_ID, forward_body)
151
 
152
+ # 既読
153
  mark_messages_read(chat_id)
154
 
155
  except Exception as e: