izuemon commited on
Commit
937f26c
·
verified ·
1 Parent(s): 8f064ef

Update ngwords.py

Browse files
Files changed (1) hide show
  1. ngwords.py +70 -67
ngwords.py CHANGED
@@ -1,98 +1,101 @@
1
  import os
2
  import time
3
- import json
4
  import requests
 
5
 
6
- # ===== 設定 =====
7
  BASE_URL = "https://desk-api.channel.io/desk/channels/200605"
8
  SOURCE_GROUP_ID = "536194"
9
  DEST_GROUP_ID = "536281"
10
 
11
- GET_MESSAGES_URL = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages"
12
- POST_MESSAGE_URL = f"{BASE_URL}/groups/{DEST_GROUP_ID}/messages"
13
- DELETE_MESSAGE_URL = f"{BASE_URL}/groups/{SOURCE_GROUP_ID}/messages"
14
 
15
  HEADERS = {
16
- "x-account": os.getenv("channeliotokenbot2"),
17
  "Content-Type": "application/json"
18
  }
19
 
20
- PARAMS = {
21
- "sortOrder": "desc",
22
- "limit": 34,
23
- "logFolded": "false"
24
- }
25
-
26
- NG_WORDS_FILE = "ngwords.txt"
27
 
28
 
29
- # ===== NGワード読み込み =====
30
- def load_ng_words():
31
- with open(NG_WORDS_FILE, encoding="utf-8") as f:
32
  return [line.strip() for line in f if line.strip()]
33
 
34
 
35
- # ===== メッセージ取得 =====
36
- def fetch_messages():
37
- r = requests.get(GET_MESSAGES_URL, headers=HEADERS, params=PARAMS)
38
- r.raise_for_status()
39
- return r.json().get("messages", [])
40
-
41
-
42
- # ===== メッセージ転送 =====
43
- def forward_message(msg, index):
44
- blocks = msg.get("blocks")
45
- files = msg.get("blocks") if blocks else None
46
-
47
- info_block = {
48
- "type": "text",
49
- "value": f"送信者:{msg.get('personId')}、送信エポックミリ秒:{msg.get('createdAt')}"
50
- }
51
-
52
- if blocks:
53
- new_blocks = [info_block] + blocks
54
- else:
55
- new_blocks = [info_block]
56
 
57
- body = {
58
- "requestId": f"desk-web-{int(time.time() * 1000)}",
59
- "blocks": new_blocks,
60
- "buttons": None,
61
- "form": None,
62
- "webPage": None,
63
- "files": files,
64
- "customPayload": None
65
- }
66
 
67
- r = requests.post(POST_MESSAGE_URL, headers=HEADERS, json=body)
68
- r.raise_for_status()
69
-
70
-
71
- # ===== メッセージ削除 =====
72
- def delete_message(message_id):
73
- url = f"{DELETE_MESSAGE_URL}/{message_id}"
74
- r = requests.delete(url, headers=HEADERS)
75
- r.raise_for_status()
76
-
77
-
78
- # ===== メインループ =====
79
  def main():
80
  ng_words = load_ng_words()
81
- print("監視開始…")
82
 
83
  while True:
84
  try:
85
- messages = fetch_messages()
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  for i, msg in enumerate(messages):
88
  plain_text = msg.get("plainText", "")
89
- message_id = msg.get("id")
90
-
91
- if any(ng in plain_text for ng in ng_words):
92
- print(f"NG検出(index={i}, id={message_id})")
93
-
94
- forward_message(msg, i)
95
- delete_message(message_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  except Exception as e:
98
  print("エラー:", e)
 
1
  import os
2
  import time
 
3
  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("channeliotokenbot2")
 
 
11
 
12
  HEADERS = {
13
+ "x-account": TOKEN,
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
 
21
+ def load_ng_words(path="ngwords.txt"):
22
+ with open(path, encoding="utf-8") as f:
 
23
  return [line.strip() for line in f if line.strip()]
24
 
25
 
26
+ 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)