yangtb24 commited on
Commit
3ca5e84
·
verified ·
1 Parent(s): cafed63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -14
app.py CHANGED
@@ -83,12 +83,10 @@ BAN_TRIGGER_PHRASES = [
83
  ]
84
  UNBAN_PHRASE = "close username"
85
 
86
- # 获取 WebDAV 环境变量
87
  WEBDAV_URL = os.environ.get('WEBDAV_URL')
88
  WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
89
  WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
90
 
91
- # 初始化数据结构 (这里假设这些变量已经定义,你需要根据你的实际代码进行调整)
92
  chatHistories = {}
93
  GROUP_SETTINGS = {}
94
  USER_SETTINGS = {}
@@ -98,7 +96,6 @@ GROUP_INFO = {}
98
  BANNED_USERS = {}
99
 
100
  def save_data_to_webdav():
101
- # 检查是否有 WebDAV 配置
102
  if not all([WEBDAV_URL, WEBDAV_USERNAME, WEBDAV_PASSWORD]):
103
  print("WebDAV 配置不完整,跳过保存操作")
104
  return
@@ -137,7 +134,6 @@ def save_data_to_webdav():
137
  def load_data_from_webdav():
138
  global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
139
 
140
- # 检查是否有 WebDAV 配置
141
  if not all([WEBDAV_URL, WEBDAV_USERNAME, WEBDAV_PASSWORD]):
142
  print("WebDAV 配置不完整,跳过加载操作")
143
  return
@@ -151,7 +147,6 @@ def load_data_from_webdav():
151
  if response.status_code == 200:
152
  data = json.loads(response.text)
153
 
154
- # 将chatHistories的键转换为整数
155
  chatHistories = {int(k): v for k, v in data.get('chatHistories', {}).items()}
156
  GROUP_SETTINGS = {int(k): v for k, v in data.get('GROUP_SETTINGS', {}).items()}
157
  USER_SETTINGS = {int(k): v for k, v in data.get('USER_SETTINGS', {}).items()}
@@ -168,17 +163,14 @@ def load_data_from_webdav():
168
  print(f"加载数据时发生错误: {e}")
169
  print("使用初始值")
170
 
171
-
172
  def periodic_save():
173
  print("定期保存线程已启动")
174
  while True:
175
  time.sleep(10)
176
  save_data_to_webdav()
177
 
178
- # 初始加载数据
179
  load_data_from_webdav()
180
 
181
- # 启动定时保存线程
182
  save_thread = threading.Thread(target=periodic_save)
183
  save_thread.daemon = True
184
  save_thread.start()
@@ -300,17 +292,15 @@ async def handleTelegramUpdate(update):
300
  return
301
 
302
  if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
303
- # 群组中,私聊指令只用于设置群组的
304
  await handleGroupCommand(chatId, userMessage, fromUserId, isGroupChat)
305
  return
306
  else:
307
  if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
308
- # 私聊时,使用用户的设置
309
  await handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat)
310
  return
311
  if isGroupChat:
312
  if chatId not in GROUP_SETTINGS:
313
- GROUP_SETTINGS[chatId] = {'aiEnabled': True, 'prefix': None, 'temperature': DEFAULT_TEMP, 'prompt_index': CURRENT_PROMPT_INDEX} # 初始化群组设置
314
  print(f'群组 {chatId} 首次检测到,默认启用 AI。')
315
 
316
  groupSettings = GROUP_SETTINGS[chatId]
@@ -322,12 +312,12 @@ async def handleTelegramUpdate(update):
322
 
323
  messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
324
  if messageContent:
325
- await processAiMessage(chatId, messageContent, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName, isGroupChat) # 传递 isGroupChat
326
  else:
327
  if userMessage == UNBAN_PHRASE:
328
  await unbanUser(chatId, fromUserId)
329
  else:
330
- await processAiMessage(chatId, userMessage, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName, isGroupChat) # 传递 isGroupChat
331
 
332
  def parseCommand(userMessage):
333
  command = userMessage.split(' ')[0]
@@ -406,7 +396,6 @@ async def handleGroupCommand(chatId, userMessage, fromUserId, isGroupChat):
406
  await sendTelegramMessage(chatId, f'当前群组使用的提示词索引是: {index}')
407
  return
408
  if command == 'resetuser':
409
- # 群组中不应该重置用户的私聊设置
410
  await sendTelegramMessage(chatId, '群组设置中,该指令无效。')
411
  return
412
 
 
83
  ]
84
  UNBAN_PHRASE = "close username"
85
 
 
86
  WEBDAV_URL = os.environ.get('WEBDAV_URL')
87
  WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
88
  WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
89
 
 
90
  chatHistories = {}
91
  GROUP_SETTINGS = {}
92
  USER_SETTINGS = {}
 
96
  BANNED_USERS = {}
97
 
98
  def save_data_to_webdav():
 
99
  if not all([WEBDAV_URL, WEBDAV_USERNAME, WEBDAV_PASSWORD]):
100
  print("WebDAV 配置不完整,跳过保存操作")
101
  return
 
134
  def load_data_from_webdav():
135
  global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
136
 
 
137
  if not all([WEBDAV_URL, WEBDAV_USERNAME, WEBDAV_PASSWORD]):
138
  print("WebDAV 配置不完整,跳过加载操作")
139
  return
 
147
  if response.status_code == 200:
148
  data = json.loads(response.text)
149
 
 
150
  chatHistories = {int(k): v for k, v in data.get('chatHistories', {}).items()}
151
  GROUP_SETTINGS = {int(k): v for k, v in data.get('GROUP_SETTINGS', {}).items()}
152
  USER_SETTINGS = {int(k): v for k, v in data.get('USER_SETTINGS', {}).items()}
 
163
  print(f"加载数据时发生错误: {e}")
164
  print("使用初始值")
165
 
 
166
  def periodic_save():
167
  print("定期保存线程已启动")
168
  while True:
169
  time.sleep(10)
170
  save_data_to_webdav()
171
 
 
172
  load_data_from_webdav()
173
 
 
174
  save_thread = threading.Thread(target=periodic_save)
175
  save_thread.daemon = True
176
  save_thread.start()
 
292
  return
293
 
294
  if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
 
295
  await handleGroupCommand(chatId, userMessage, fromUserId, isGroupChat)
296
  return
297
  else:
298
  if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
 
299
  await handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat)
300
  return
301
  if isGroupChat:
302
  if chatId not in GROUP_SETTINGS:
303
+ GROUP_SETTINGS[chatId] = {'aiEnabled': True, 'prefix': None, 'temperature': DEFAULT_TEMP, 'prompt_index': CURRENT_PROMPT_INDEX}
304
  print(f'群组 {chatId} 首次检测到,默认启用 AI。')
305
 
306
  groupSettings = GROUP_SETTINGS[chatId]
 
312
 
313
  messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
314
  if messageContent:
315
+ await processAiMessage(chatId, messageContent, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName, isGroupChat)
316
  else:
317
  if userMessage == UNBAN_PHRASE:
318
  await unbanUser(chatId, fromUserId)
319
  else:
320
+ await processAiMessage(chatId, userMessage, fromUserId, message_id, fromUserFirstName, fromUserLastName, fromUserName, isGroupChat)
321
 
322
  def parseCommand(userMessage):
323
  command = userMessage.split(' ')[0]
 
396
  await sendTelegramMessage(chatId, f'当前群组使用的提示词索引是: {index}')
397
  return
398
  if command == 'resetuser':
 
399
  await sendTelegramMessage(chatId, '群组设置中,该指令无效。')
400
  return
401