Update app.py
Browse files
app.py
CHANGED
@@ -147,40 +147,15 @@ async def handleTelegramUpdate(update):
|
|
147 |
prefix = GROUP_SETTINGS.get(chatId, {}).get('prefix', '无')
|
148 |
await sendTelegramMessage(chatId, f'当前群组触发前缀为: {prefix}')
|
149 |
return
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
if 0 <= temp <= 2:
|
155 |
-
USER_SETTINGS.setdefault(fromUserId, {}).update({'temperature': temp})
|
156 |
-
await sendTelegramMessage(chatId, f'已设置AI回复温度为: {temp}')
|
157 |
-
else:
|
158 |
-
await sendTelegramMessage(chatId, '温度设置无效,请输入0到2之间的数字。')
|
159 |
-
except ValueError:
|
160 |
-
await sendTelegramMessage(chatId, '温度设置无效,请输入0到2之间的数字。')
|
161 |
-
return
|
162 |
-
if command == 'gettemp':
|
163 |
-
temp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
164 |
-
await sendTelegramMessage(chatId, f'当前AI回复温度为: {temp}')
|
165 |
-
return
|
166 |
-
if userMessage.startswith('/promat '):
|
167 |
-
try:
|
168 |
-
index = int(userMessage[len('/promat '):].strip())
|
169 |
-
if index in PROMPT_TEMPLATES:
|
170 |
-
global CURRENT_PROMPT_INDEX
|
171 |
-
CURRENT_PROMPT_INDEX = index
|
172 |
-
await sendTelegramMessage(chatId, f'已切换到提示词 {index}。')
|
173 |
-
else:
|
174 |
-
await sendTelegramMessage(chatId, '提示词索引无效。请使用 /getpromat 查看可用的索引。')
|
175 |
-
except ValueError:
|
176 |
-
await sendTelegramMessage(chatId, '提示词索引无效。请使用 /getpromat 查看可用的索引。')
|
177 |
-
return
|
178 |
-
if command == 'getpromat':
|
179 |
-
await sendTelegramMessage(chatId, f'当前使用的提示词索引是: {CURRENT_PROMPT_INDEX}')
|
180 |
return
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
184 |
return
|
185 |
if isGroupChat:
|
186 |
if chatId not in GROUP_SETTINGS:
|
@@ -206,10 +181,49 @@ def parseCommand(userMessage):
|
|
206 |
command = command.split('@')[0]
|
207 |
return command[1:]
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
async def processAiMessage(chatId, userMessage, fromUserId):
|
210 |
history = chatHistories.get(chatId, [])
|
211 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
212 |
-
|
|
|
213 |
history.append({'role': 'user', 'content': userMessage})
|
214 |
|
215 |
if len(history) > MAX_HISTORY_LENGTH:
|
@@ -292,7 +306,7 @@ def getHelpMessage():
|
|
292 |
/setprefix <prefix> - 设置群组中触发AI回复的前缀,例如:/setprefix @bot。
|
293 |
/getprefix - 获取当前群组的触发前缀。
|
294 |
|
295 |
-
|
296 |
/settemp <温度值> - 设置AI回复的温度 (0-2),例如:/settemp 1.0。
|
297 |
/gettemp - 获取当前AI回复的温度。
|
298 |
/resetuser - 重置你的个人设置。
|
|
|
147 |
prefix = GROUP_SETTINGS.get(chatId, {}).get('prefix', '无')
|
148 |
await sendTelegramMessage(chatId, f'当前群组触发前缀为: {prefix}')
|
149 |
return
|
150 |
+
|
151 |
+
# 在群组中处理私聊指令
|
152 |
+
if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
|
153 |
+
await handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
return
|
155 |
+
else:
|
156 |
+
# 私聊中处理所有指令
|
157 |
+
if userMessage.startswith('/settemp ') or userMessage.startswith('/promat ') or command in ['gettemp', 'getpromat', 'resetuser']:
|
158 |
+
await handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat)
|
159 |
return
|
160 |
if isGroupChat:
|
161 |
if chatId not in GROUP_SETTINGS:
|
|
|
181 |
command = command.split('@')[0]
|
182 |
return command[1:]
|
183 |
|
184 |
+
|
185 |
+
async def handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat):
|
186 |
+
command = parseCommand(userMessage)
|
187 |
+
if userMessage.startswith('/settemp '):
|
188 |
+
try:
|
189 |
+
temp = float(userMessage[len('/settemp '):].strip())
|
190 |
+
if 0 <= temp <= 2:
|
191 |
+
USER_SETTINGS.setdefault(fromUserId, {}).update({'temperature': temp})
|
192 |
+
await sendTelegramMessage(chatId, f'已设置AI回复温度为: {temp}')
|
193 |
+
else:
|
194 |
+
await sendTelegramMessage(chatId, '温度设置无效,请输入0到2之间的数字。')
|
195 |
+
except ValueError:
|
196 |
+
await sendTelegramMessage(chatId, '温度设置无效,请输入0到2之间的数字。')
|
197 |
+
return
|
198 |
+
if command == 'gettemp':
|
199 |
+
temp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
200 |
+
await sendTelegramMessage(chatId, f'当前AI回复温度为: {temp}')
|
201 |
+
return
|
202 |
+
if userMessage.startswith('/promat '):
|
203 |
+
try:
|
204 |
+
index = int(userMessage[len('/promat '):].strip())
|
205 |
+
if index in PROMPT_TEMPLATES:
|
206 |
+
USER_SETTINGS.setdefault(fromUserId, {}).update({'prompt_index': index})
|
207 |
+
await sendTelegramMessage(chatId, f'已切换到提示词 {index}。')
|
208 |
+
else:
|
209 |
+
await sendTelegramMessage(chatId, '提示词索引无效。请使用 /getpromat 查看可用的索引。')
|
210 |
+
except ValueError:
|
211 |
+
await sendTelegramMessage(chatId, '提示词索引无效。请使用 /getpromat 查看可用的索引。')
|
212 |
+
return
|
213 |
+
if command == 'getpromat':
|
214 |
+
index = USER_SETTINGS.get(fromUserId, {}).get('prompt_index', CURRENT_PROMPT_INDEX)
|
215 |
+
await sendTelegramMessage(chatId, f'当前使用的提示词索引是: {index}')
|
216 |
+
return
|
217 |
+
if command == 'resetuser':
|
218 |
+
USER_SETTINGS.pop(fromUserId, None)
|
219 |
+
await sendTelegramMessage(chatId, '已重置您的个人设置。')
|
220 |
+
return
|
221 |
+
|
222 |
async def processAiMessage(chatId, userMessage, fromUserId):
|
223 |
history = chatHistories.get(chatId, [])
|
224 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
225 |
+
userPromptIndex = USER_SETTINGS.get(fromUserId, {}).get('prompt_index', CURRENT_PROMPT_INDEX)
|
226 |
+
currentPrompt = PROMPT_TEMPLATES.get(userPromptIndex, "")
|
227 |
history.append({'role': 'user', 'content': userMessage})
|
228 |
|
229 |
if len(history) > MAX_HISTORY_LENGTH:
|
|
|
306 |
/setprefix <prefix> - 设置群组中触发AI回复的前缀,例如:/setprefix @bot。
|
307 |
/getprefix - 获取当前群组的触发前缀。
|
308 |
|
309 |
+
私聊指令 (在群组中也可以使用):
|
310 |
/settemp <温度值> - 设置AI回复的温度 (0-2),例如:/settemp 1.0。
|
311 |
/gettemp - 获取当前AI回复的温度。
|
312 |
/resetuser - 重置你的个人设置。
|