Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,9 @@ import os
|
|
2 |
import json
|
3 |
import requests
|
4 |
from flask import Flask, request, jsonify
|
5 |
-
from datetime import datetime
|
6 |
import asyncio
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
@@ -48,12 +49,22 @@ BOT_COMMANDS = [
|
|
48 |
{"command": "resetuser", "description": "重置你的个人设置"},
|
49 |
{"command": "promat", "description": "切换提示词,例如: /promat 0, 1, 2"},
|
50 |
{"command": "getpromat", "description": "获取当前使用的提示词索引"},
|
51 |
-
{"command": "test", "description": "获取当前使用的提示词索引"},
|
52 |
]
|
53 |
DEFAULT_TEMP = 1.5
|
54 |
USER_LAST_ACTIVE = {}
|
55 |
GROUP_ACTIVE_USERS = {}
|
56 |
GROUP_INFO = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def make_telegram_request(method, data=None):
|
59 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
@@ -135,7 +146,6 @@ async def handleTelegramUpdate(update):
|
|
135 |
GROUP_INFO[chatId]['last_active'] = datetime.now()
|
136 |
|
137 |
|
138 |
-
|
139 |
if not userMessage:
|
140 |
return
|
141 |
|
@@ -146,9 +156,6 @@ async def handleTelegramUpdate(update):
|
|
146 |
chatHistories.pop(chatId, None)
|
147 |
await sendTelegramMessage(chatId, '聊天记录已清空。')
|
148 |
return
|
149 |
-
if command == 'test':
|
150 |
-
await sendTelegramMessage(chatId, '测试命令已执行')
|
151 |
-
return
|
152 |
if command == 'help':
|
153 |
await sendTelegramMessage(chatId, getHelpMessage())
|
154 |
return
|
@@ -199,9 +206,12 @@ async def handleTelegramUpdate(update):
|
|
199 |
|
200 |
messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
|
201 |
if messageContent:
|
202 |
-
|
203 |
else:
|
204 |
-
|
|
|
|
|
|
|
205 |
|
206 |
def parseCommand(userMessage):
|
207 |
command = userMessage.split(' ')[0]
|
@@ -247,6 +257,18 @@ async def handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat):
|
|
247 |
return
|
248 |
|
249 |
async def processAiMessage(chatId, userMessage, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
history = chatHistories.get(chatId, [])
|
251 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
252 |
userPromptIndex = USER_SETTINGS.get(fromUserId, {}).get('prompt_index', CURRENT_PROMPT_INDEX)
|
@@ -393,6 +415,8 @@ def getHelpMessage():
|
|
393 |
注意:
|
394 |
- 机器人会记住最近的 {MAX_HISTORY_LENGTH} 条对话。
|
395 |
- 机器人具有攻击性,请谨慎使用。
|
|
|
|
|
396 |
"""
|
397 |
|
398 |
@app.route('/update_commands', methods=['GET'])
|
@@ -432,5 +456,15 @@ async def getChatInfo(chatId):
|
|
432 |
print(f'获取群组信息失败: {e}')
|
433 |
return None
|
434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
if __name__ == '__main__':
|
436 |
-
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
2 |
import json
|
3 |
import requests
|
4 |
from flask import Flask, request, jsonify
|
5 |
+
from datetime import datetime, timedelta
|
6 |
import asyncio
|
7 |
+
import re
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
|
|
49 |
{"command": "resetuser", "description": "重置你的个人设置"},
|
50 |
{"command": "promat", "description": "切换提示词,例如: /promat 0, 1, 2"},
|
51 |
{"command": "getpromat", "description": "获取当前使用的提示词索引"},
|
|
|
52 |
]
|
53 |
DEFAULT_TEMP = 1.5
|
54 |
USER_LAST_ACTIVE = {}
|
55 |
GROUP_ACTIVE_USERS = {}
|
56 |
GROUP_INFO = {}
|
57 |
+
BANNED_USERS = {}
|
58 |
+
BAN_DURATION = timedelta(minutes=30)
|
59 |
+
BAN_TRIGGER_PHRASES = [
|
60 |
+
r"(?:傻|笨|蠢|弱|垃圾|废物|sb|煞笔|憨|呆|脑残|智障|白痴|低能|饭桶|草包|猪|狗|鸡|臭|烂|妈|爹|你妈|你爹).*(?:bot|机器人|AI|你)",
|
61 |
+
r"(?:你).*(?:去死|滚|爬|闭嘴|瞎说|胡说|放屁)",
|
62 |
+
r"(?:操|艹|肏|fuck|shit).*(?:你|bot|机器人|AI)",
|
63 |
+
r"(?:垃圾|废物).*(?:AI|bot|机器人|你)",
|
64 |
+
r"(?:弱智|脑残|智障).*(?:AI|bot|机器人|你)",
|
65 |
+
r"(?:傻|笨|蠢).*(?:AI|bot|机器人|你)",
|
66 |
+
]
|
67 |
+
UNBAN_PHRASE = "close username"
|
68 |
|
69 |
def make_telegram_request(method, data=None):
|
70 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
|
|
146 |
GROUP_INFO[chatId]['last_active'] = datetime.now()
|
147 |
|
148 |
|
|
|
149 |
if not userMessage:
|
150 |
return
|
151 |
|
|
|
156 |
chatHistories.pop(chatId, None)
|
157 |
await sendTelegramMessage(chatId, '聊天记录已清空。')
|
158 |
return
|
|
|
|
|
|
|
159 |
if command == 'help':
|
160 |
await sendTelegramMessage(chatId, getHelpMessage())
|
161 |
return
|
|
|
206 |
|
207 |
messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
|
208 |
if messageContent:
|
209 |
+
await processAiMessage(chatId, messageContent, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName)
|
210 |
else:
|
211 |
+
if userMessage == UNBAN_PHRASE:
|
212 |
+
await unbanUser(chatId, fromUserId)
|
213 |
+
else:
|
214 |
+
await processAiMessage(chatId, userMessage, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName)
|
215 |
|
216 |
def parseCommand(userMessage):
|
217 |
command = userMessage.split(' ')[0]
|
|
|
257 |
return
|
258 |
|
259 |
async def processAiMessage(chatId, userMessage, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName):
|
260 |
+
if fromUserId in BANNED_USERS and BANNED_USERS[fromUserId] > datetime.now():
|
261 |
+
remaining_time = BANNED_USERS[fromUserId] - datetime.now()
|
262 |
+
minutes = int(remaining_time.total_seconds() / 60)
|
263 |
+
await sendTelegramMessage(chatId, f"您已被禁用,剩余时间: {minutes} 分钟。", options={'reply_to_message_id': message_id})
|
264 |
+
return
|
265 |
+
|
266 |
+
for pattern in BAN_TRIGGER_PHRASES:
|
267 |
+
if re.search(pattern, userMessage, re.IGNORECASE):
|
268 |
+
await banUser(chatId, fromUserId)
|
269 |
+
await sendTelegramMessage(chatId, "检测到辱骂行为,您已被禁用。", options={'reply_to_message_id': message_id})
|
270 |
+
return
|
271 |
+
|
272 |
history = chatHistories.get(chatId, [])
|
273 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
274 |
userPromptIndex = USER_SETTINGS.get(fromUserId, {}).get('prompt_index', CURRENT_PROMPT_INDEX)
|
|
|
415 |
注意:
|
416 |
- 机器人会记住最近的 {MAX_HISTORY_LENGTH} 条对话。
|
417 |
- 机器人具有攻击性,请谨慎使用。
|
418 |
+
- 辱骂机器人将会被禁用 {int(BAN_DURATION.total_seconds() / 60)} 分钟。
|
419 |
+
- 发送 `close username` 可以解除禁用。
|
420 |
"""
|
421 |
|
422 |
@app.route('/update_commands', methods=['GET'])
|
|
|
456 |
print(f'获取群组信息失败: {e}')
|
457 |
return None
|
458 |
|
459 |
+
async def banUser(chatId, userId):
|
460 |
+
BANNED_USERS[userId] = datetime.now() + BAN_DURATION
|
461 |
+
print(f"用户 {userId} 在群组 {chatId} 中被禁用,直到 {BANNED_USERS[userId]}")
|
462 |
+
|
463 |
+
async def unbanUser(chatId, userId):
|
464 |
+
if userId in BANNED_USERS:
|
465 |
+
del BANNED_USERS[userId]
|
466 |
+
await sendTelegramMessage(chatId, f"用户 {userId} 已被解禁。")
|
467 |
+
print(f"用户 {userId} 在群组 {chatId} 中被解禁。")
|
468 |
+
|
469 |
if __name__ == '__main__':
|
470 |
+
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|