Update app.py
Browse files
app.py
CHANGED
@@ -5,9 +5,6 @@ from flask import Flask, request, jsonify
|
|
5 |
from datetime import datetime, timedelta
|
6 |
import asyncio
|
7 |
import re
|
8 |
-
import webdavclient.client as wc # 修改这里
|
9 |
-
import time
|
10 |
-
import threading
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
@@ -16,11 +13,8 @@ AI_API_ENDPOINT = os.environ.get('AI_API_ENDPOINT')
|
|
16 |
AI_API_KEY = os.environ.get('AI_API_KEY')
|
17 |
AI_MODEL = os.environ.get('AI_MODEL')
|
18 |
PHP_PROXY_URL = os.environ.get('PHP_PROXY_URL')
|
19 |
-
WEBDAV_HOSTNAME = os.environ.get('WEBDAV_HOSTNAME')
|
20 |
-
WEBDAV_LOGIN = os.environ.get('WEBDAV_LOGIN')
|
21 |
-
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
22 |
|
23 |
-
if not all([TELEGRAM_BOT_TOKEN, AI_API_ENDPOINT, AI_API_KEY, AI_MODEL
|
24 |
raise ValueError("请设置所有必要的环境变量")
|
25 |
|
26 |
AI_API_HEADERS = {
|
@@ -86,53 +80,6 @@ BAN_TRIGGER_PHRASES = [
|
|
86 |
]
|
87 |
UNBAN_PHRASE = "close username"
|
88 |
|
89 |
-
webdav_options = {
|
90 |
-
'webdav_hostname': WEBDAV_HOSTNAME,
|
91 |
-
'webdav_login': WEBDAV_LOGIN,
|
92 |
-
'webdav_password': WEBDAV_PASSWORD
|
93 |
-
}
|
94 |
-
client = wc.Client(webdav_options)
|
95 |
-
|
96 |
-
def save_data_to_webdav():
|
97 |
-
data = {
|
98 |
-
'chatHistories': chatHistories,
|
99 |
-
'GROUP_SETTINGS': GROUP_SETTINGS,
|
100 |
-
'USER_SETTINGS': USER_SETTINGS,
|
101 |
-
'USER_LAST_ACTIVE': {str(k): v.isoformat() for k, v in USER_LAST_ACTIVE.items()},
|
102 |
-
'GROUP_ACTIVE_USERS': {str(k): list(v) for k, v in GROUP_ACTIVE_USERS.items()},
|
103 |
-
'GROUP_INFO': {str(k): {**v, 'last_active': v['last_active'].isoformat()} for k, v in GROUP_INFO.items()},
|
104 |
-
'BANNED_USERS': {str(k): v.isoformat() for k, v in BANNED_USERS.items()}
|
105 |
-
}
|
106 |
-
json_data = json.dumps(data)
|
107 |
-
try:
|
108 |
-
client.upload_to('/tg_bot/bot_data.json', json_data)
|
109 |
-
except Exception as e:
|
110 |
-
print(f"Error uploading data to WebDAV: {e}")
|
111 |
-
|
112 |
-
def load_data_from_webdav():
|
113 |
-
global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
|
114 |
-
try:
|
115 |
-
if client.check('/tg_bot/bot_data.json'):
|
116 |
-
json_data = client.download('/tg_bot/bot_data.json')
|
117 |
-
data = json.loads(json_data)
|
118 |
-
chatHistories = data.get('chatHistories', {})
|
119 |
-
GROUP_SETTINGS = data.get('GROUP_SETTINGS', {})
|
120 |
-
USER_SETTINGS = data.get('USER_SETTINGS', {})
|
121 |
-
USER_LAST_ACTIVE = {int(k): datetime.fromisoformat(v) for k, v in data.get('USER_LAST_ACTIVE', {}).items()}
|
122 |
-
GROUP_ACTIVE_USERS = {int(k): set(v) for k, v in data.get('GROUP_ACTIVE_USERS', {}).items()}
|
123 |
-
GROUP_INFO = {int(k): {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data.get('GROUP_INFO', {}).items()}
|
124 |
-
BANNED_USERS = {int(k): datetime.fromisoformat(v) for k, v in data.get('BANNED_USERS', {}).items()}
|
125 |
-
else:
|
126 |
-
print("No existing data found on WebDAV. Initializing with empty data.")
|
127 |
-
except Exception as e:
|
128 |
-
print(f"Error loading data from WebDAV: {e}")
|
129 |
-
|
130 |
-
def periodic_save():
|
131 |
-
while True:
|
132 |
-
time.sleep(60) # 等待1分钟
|
133 |
-
save_data_to_webdav()
|
134 |
-
print("Data saved to WebDAV")
|
135 |
-
|
136 |
def make_telegram_request(method, data=None):
|
137 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
138 |
if PHP_PROXY_URL:
|
@@ -525,9 +472,64 @@ async def unbanUser(chatId, userId):
|
|
525 |
await sendTelegramMessage(chatId, f"用户 {userId} 已被解禁。")
|
526 |
print(f"用户 {userId} 在群组 {chatId} 中被解禁。")
|
527 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
528 |
if __name__ == '__main__':
|
529 |
-
|
530 |
-
save_thread = threading.Thread(target=periodic_save)
|
531 |
-
save_thread.daemon = True
|
532 |
-
save_thread.start()
|
533 |
-
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
5 |
from datetime import datetime, timedelta
|
6 |
import asyncio
|
7 |
import re
|
|
|
|
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
|
|
13 |
AI_API_KEY = os.environ.get('AI_API_KEY')
|
14 |
AI_MODEL = os.environ.get('AI_MODEL')
|
15 |
PHP_PROXY_URL = os.environ.get('PHP_PROXY_URL')
|
|
|
|
|
|
|
16 |
|
17 |
+
if not all([TELEGRAM_BOT_TOKEN, AI_API_ENDPOINT, AI_API_KEY, AI_MODEL]):
|
18 |
raise ValueError("请设置所有必要的环境变量")
|
19 |
|
20 |
AI_API_HEADERS = {
|
|
|
80 |
]
|
81 |
UNBAN_PHRASE = "close username"
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def make_telegram_request(method, data=None):
|
84 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
85 |
if PHP_PROXY_URL:
|
|
|
472 |
await sendTelegramMessage(chatId, f"用户 {userId} 已被解禁。")
|
473 |
print(f"用户 {userId} 在群组 {chatId} 中被解禁。")
|
474 |
|
475 |
+
WEBDAV_HOSTNAME = os.environ.get('WEBDAV_HOSTNAME')
|
476 |
+
WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
|
477 |
+
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
478 |
+
|
479 |
+
webdav_options = {
|
480 |
+
'webdav_hostname': WEBDAV_HOSTNAME,
|
481 |
+
'webdav_login': WEBDAV_USERNAME,
|
482 |
+
'webdav_password': WEBDAV_PASSWORD
|
483 |
+
}
|
484 |
+
|
485 |
+
webdav_client = wc.Client(webdav_options)
|
486 |
+
|
487 |
+
def save_data_to_webdav():
|
488 |
+
data = {
|
489 |
+
'chatHistories': chatHistories,
|
490 |
+
'GROUP_SETTINGS': GROUP_SETTINGS,
|
491 |
+
'USER_SETTINGS': USER_SETTINGS,
|
492 |
+
'USER_LAST_ACTIVE': {k: v.isoformat() for k, v in USER_LAST_ACTIVE.items()},
|
493 |
+
'GROUP_ACTIVE_USERS': {k: list(v) for k, v in GROUP_ACTIVE_USERS.items()},
|
494 |
+
'GROUP_INFO': {k: {**v, 'last_active': v['last_active'].isoformat()} for k, v in GROUP_INFO.items()},
|
495 |
+
'BANNED_USERS': {k: v.isoformat() for k, v in BANNED_USERS.items()}
|
496 |
+
}
|
497 |
+
|
498 |
+
json_data = json.dumps(data)
|
499 |
+
webdav_client.upload_content(json_data, '/tg_bot/bot_data.json')
|
500 |
+
print("数据已保存到WebDAV")
|
501 |
+
|
502 |
+
def load_data_from_webdav():
|
503 |
+
global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
|
504 |
+
|
505 |
+
if webdav_client.check('/tg_bot/bot_data.json'):
|
506 |
+
json_data = webdav_client.download_content('/tg_bot/bot_data.json')
|
507 |
+
data = json.loads(json_data)
|
508 |
+
|
509 |
+
chatHistories = data['chatHistories']
|
510 |
+
GROUP_SETTINGS = data['GROUP_SETTINGS']
|
511 |
+
USER_SETTINGS = data['USER_SETTINGS']
|
512 |
+
USER_LAST_ACTIVE = {k: datetime.fromisoformat(v) for k, v in data['USER_LAST_ACTIVE'].items()}
|
513 |
+
GROUP_ACTIVE_USERS = {k: set(v) for k, v in data['GROUP_ACTIVE_USERS'].items()}
|
514 |
+
GROUP_INFO = {k: {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data['GROUP_INFO'].items()}
|
515 |
+
BANNED_USERS = {k: datetime.fromisoformat(v) for k, v in data['BANNED_USERS'].items()}
|
516 |
+
|
517 |
+
print("数据已从WebDAV加载")
|
518 |
+
else:
|
519 |
+
print("WebDAV中没有找到数据,使用初始值")
|
520 |
+
|
521 |
+
def periodic_save():
|
522 |
+
while True:
|
523 |
+
time.sleep(60) # 等待60秒
|
524 |
+
save_data_to_webdav()
|
525 |
+
|
526 |
+
# 在程序启动时加载数据
|
527 |
+
load_data_from_webdav()
|
528 |
+
|
529 |
+
# 启动定期保存的线程
|
530 |
+
save_thread = threading.Thread(target=periodic_save)
|
531 |
+
save_thread.daemon = True
|
532 |
+
save_thread.start()
|
533 |
+
|
534 |
if __name__ == '__main__':
|
535 |
+
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
|
|
|
|