izuemon commited on
Commit
1145bc0
·
verified ·
1 Parent(s): ad80d01

Update ngwords.py

Browse files
Files changed (1) hide show
  1. ngwords.py +74 -66
ngwords.py CHANGED
@@ -4,7 +4,10 @@ import requests
4
  from datetime import datetime
5
 
6
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
7
- SOURCE_GROUP_ID = "536194"
 
 
 
8
  DEST_GROUP_ID = "536281"
9
 
10
  TOKEN = os.getenv("dmsendertoken")
@@ -14,7 +17,6 @@ HEADERS = {
14
  "Content-Type": "application/json"
15
  }
16
 
17
- GET_URL = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages"
18
  POST_URL = f"{BASE_URL}/groups/{DEST_GROUP_ID}/messages"
19
 
20
 
@@ -27,75 +29,81 @@ def contains_ng_word(text, ng_words):
27
  return any(word in text for word in ng_words)
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def main():
31
  ng_words = load_ng_words()
32
 
33
  while True:
34
  try:
35
- resp = requests.get(
36
- GET_URL,
37
- headers=HEADERS,
38
- params={
39
- "sortOrder": "desc",
40
- "limit": 34,
41
- "logFolded": "false"
42
- }
43
- )
44
- resp.raise_for_status()
45
- data = resp.json()
46
-
47
- messages = data.get("messages", [])
48
-
49
- for i, msg in enumerate(messages):
50
- plain_text = msg.get("plainText", "")
51
- if not plain_text:
52
- continue
53
-
54
- if not contains_ng_word(plain_text, ng_words):
55
- continue
56
-
57
- person_id = msg.get("personId")
58
- created_at = msg.get("createdAt")
59
- blocks = msg.get("blocks") or []
60
- files = msg.get("files")
61
-
62
- # 先頭に送信者情報ブロックを追加
63
- info_block = {
64
- "type": "text",
65
- "value": f"送信者:{person_id}、送信エポックミリ秒:{created_at}"
66
- }
67
-
68
- new_blocks = [info_block] + blocks
69
-
70
- post_body = {
71
- "requestId": f"desk-web-{int(time.time() * 1000)}",
72
- "blocks": new_blocks,
73
- "buttons": None,
74
- "form": None,
75
- "webPage": None,
76
- "files": files if files else None,
77
- "customPayload": None
78
- }
79
-
80
- # POST
81
- post_resp = requests.post(
82
- POST_URL,
83
- headers=HEADERS,
84
- json=post_body
85
- )
86
- post_resp.raise_for_status()
87
-
88
- # DELETE 元メッセージ
89
- msg_id = msg.get("id")
90
- delete_url = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages/{msg_id}"
91
-
92
- del_resp = requests.delete(
93
- delete_url,
94
- headers=HEADERS
95
- )
96
- del_resp.raise_for_status()
97
-
98
- print(f"処理完了: message_id={msg_id}")
99
 
100
  except Exception as e:
101
  print("エラー:", e)
 
4
  from datetime import datetime
5
 
6
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
7
+
8
+ # 監視対象グループID(配列で指定可能)
9
+ SOURCE_GROUP_IDS = ["536194", "463667"]
10
+
11
  DEST_GROUP_ID = "536281"
12
 
13
  TOKEN = os.getenv("dmsendertoken")
 
17
  "Content-Type": "application/json"
18
  }
19
 
 
20
  POST_URL = f"{BASE_URL}/groups/{DEST_GROUP_ID}/messages"
21
 
22
 
 
29
  return any(word in text for word in ng_words)
30
 
31
 
32
+ def process_group(source_group_id, ng_words):
33
+ get_url = f"{BASE_URL}/groups/{source_group_id}/messages"
34
+
35
+ resp = requests.get(
36
+ get_url,
37
+ headers=HEADERS,
38
+ params={
39
+ "sortOrder": "desc",
40
+ "limit": 34,
41
+ "logFolded": "false"
42
+ }
43
+ )
44
+ resp.raise_for_status()
45
+ data = resp.json()
46
+
47
+ messages = data.get("messages", [])
48
+
49
+ for msg in messages:
50
+ plain_text = msg.get("plainText", "")
51
+ if not plain_text:
52
+ continue
53
+
54
+ if not contains_ng_word(plain_text, ng_words):
55
+ continue
56
+
57
+ person_id = msg.get("personId")
58
+ created_at = msg.get("createdAt")
59
+ blocks = msg.get("blocks") or []
60
+ files = msg.get("files")
61
+
62
+ info_block = {
63
+ "type": "text",
64
+ "value": f"送信者:{person_id}、送信エポックミリ秒:{created_at}"
65
+ }
66
+
67
+ new_blocks = [info_block] + blocks
68
+
69
+ post_body = {
70
+ "requestId": f"desk-web-{int(time.time() * 1000)}",
71
+ "blocks": new_blocks,
72
+ "buttons": None,
73
+ "form": None,
74
+ "webPage": None,
75
+ "files": files if files else None,
76
+ "customPayload": None
77
+ }
78
+
79
+ # POST
80
+ post_resp = requests.post(
81
+ POST_URL,
82
+ headers=HEADERS,
83
+ json=post_body
84
+ )
85
+ post_resp.raise_for_status()
86
+
87
+ # DELETE 元メッセージ
88
+ msg_id = msg.get("id")
89
+ delete_url = f"{BASE_URL}/groups/{source_group_id}/messages/{msg_id}"
90
+
91
+ del_resp = requests.delete(
92
+ delete_url,
93
+ headers=HEADERS
94
+ )
95
+ del_resp.raise_for_status()
96
+
97
+ print(f"処理完了: group_id={source_group_id}, message_id={msg_id}")
98
+
99
+
100
  def main():
101
  ng_words = load_ng_words()
102
 
103
  while True:
104
  try:
105
+ for group_id in SOURCE_GROUP_IDS:
106
+ process_group(group_id, ng_words)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  except Exception as e:
109
  print("エラー:", e)