izuemon commited on
Commit
b3bc0d4
·
verified ·
1 Parent(s): c3334da

Update dmsender.py

Browse files
Files changed (1) hide show
  1. dmsender.py +44 -25
dmsender.py CHANGED
@@ -4,6 +4,7 @@ import os
4
 
5
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
6
  TARGET_FORWARD_CHAT_ID = "463667"
 
7
 
8
  HEADERS = {
9
  "Content-Type": "application/json",
@@ -61,6 +62,12 @@ def get_messages(chat_id):
61
  return r.json()
62
 
63
 
 
 
 
 
 
 
64
  def find_latest_message(messages):
65
  """
66
  messages 配列の中から updatedAt が最大の message を返す
@@ -76,31 +83,43 @@ def main_loop():
76
 
77
  for session in sessions:
78
  unread = session.get("unread", 0)
79
- if unread >= 1:
80
- chat_id = session.get("chatId")
81
- if not chat_id:
82
- continue
83
-
84
- # ① unread があるチャットに「送信しました」を送信
85
- post_message(chat_id, SEND_BODY_TEMPLATE)
86
-
87
- # そのチャットの messages を取得
88
- msg_data = get_messages(chat_id)
89
- messages = msg_data.get("messages", [])
90
- if not messages:
91
- continue
92
-
93
- # updatedAt が最大の message を取得
94
- latest = find_latest_message(messages)
95
- blocks = latest.get("blocks")
96
- files = latest.get("files")
97
-
98
- # ④ 固定チャットID(463667)へ転送
99
- forward_body = FORWARD_BODY_TEMPLATE.copy()
100
- forward_body["blocks"] = blocks
101
- forward_body["files"] = files if files else None
102
-
103
- post_message(TARGET_FORWARD_CHAT_ID, forward_body)
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  except Exception as e:
106
  print("Error:", e)
 
4
 
5
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
6
  TARGET_FORWARD_CHAT_ID = "463667"
7
+ SKIP_PERSON_ID = 604730
8
 
9
  HEADERS = {
10
  "Content-Type": "application/json",
 
62
  return r.json()
63
 
64
 
65
+ def mark_messages_read(chat_id):
66
+ url = f"{BASE_URL}/direct-chats/{chat_id}/messages/read"
67
+ r = requests.put(url, headers=HEADERS)
68
+ r.raise_for_status()
69
+
70
+
71
  def find_latest_message(messages):
72
  """
73
  messages 配列の中から updatedAt が最大の message を返す
 
83
 
84
  for session in sessions:
85
  unread = session.get("unread", 0)
86
+ if unread < 1:
87
+ continue
88
+
89
+ chat_id = session.get("chatId")
90
+ if not chat_id:
91
+ continue
92
+
93
+ # messages を取得
94
+ msg_data = get_messages(chat_id)
95
+ messages = msg_data.get("messages", [])
96
+ if not messages:
97
+ continue
98
+
99
+ # 最新メッセージ取得
100
+ latest = find_latest_message(messages)
101
+
102
+ # personId が 604730 の場合はスキップ
103
+ person_id = latest.get("personId")
104
+ if person_id == SKIP_PERSON_ID:
105
+ mark_messages_read(chat_id)
106
+ continue
107
+
108
+ # unread があるチャットに「送信しました」を送信
109
+ post_message(chat_id, SEND_BODY_TEMPLATE)
110
+
111
+ blocks = latest.get("blocks")
112
+ files = latest.get("files")
113
+
114
+ # ② 固定チャットIDへ転送
115
+ forward_body = FORWARD_BODY_TEMPLATE.copy()
116
+ forward_body["blocks"] = blocks
117
+ forward_body["files"] = files if files else None
118
+
119
+ post_message(TARGET_FORWARD_CHAT_ID, forward_body)
120
+
121
+ # ③ 元チャットを既読にする
122
+ mark_messages_read(chat_id)
123
 
124
  except Exception as e:
125
  print("Error:", e)