Update app.py
Browse files
app.py
CHANGED
@@ -105,6 +105,7 @@ async def handleTelegramUpdate(update):
|
|
105 |
userMessage = update['message'].get('text', '')
|
106 |
isGroupChat = update['message']['chat']['type'] in ['group', 'supergroup']
|
107 |
fromUserId = update['message']['from']['id']
|
|
|
108 |
|
109 |
if not userMessage:
|
110 |
return
|
@@ -171,9 +172,9 @@ async def handleTelegramUpdate(update):
|
|
171 |
|
172 |
messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
|
173 |
if messageContent:
|
174 |
-
await processAiMessage(chatId, messageContent, fromUserId)
|
175 |
else:
|
176 |
-
await processAiMessage(chatId, userMessage, fromUserId)
|
177 |
|
178 |
def parseCommand(userMessage):
|
179 |
command = userMessage.split(' ')[0]
|
@@ -219,7 +220,7 @@ async def handlePrivateCommand(chatId, userMessage, fromUserId, isGroupChat):
|
|
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)
|
@@ -233,6 +234,9 @@ async def processAiMessage(chatId, userMessage, fromUserId):
|
|
233 |
{'role': 'system', 'content': currentPrompt},
|
234 |
*history
|
235 |
]
|
|
|
|
|
|
|
236 |
|
237 |
try:
|
238 |
ai_response = requests.post(AI_API_ENDPOINT, headers=AI_API_HEADERS, json={
|
@@ -248,13 +252,14 @@ async def processAiMessage(chatId, userMessage, fromUserId):
|
|
248 |
history.append({'role': 'assistant', 'content': ai_reply})
|
249 |
chatHistories[chatId] = history
|
250 |
|
251 |
-
await
|
252 |
except requests.exceptions.RequestException as e:
|
253 |
print(f'AI API 响应失败: {e}')
|
254 |
-
await
|
255 |
except Exception as error:
|
256 |
print(f'处理消息时发生错误: {error}')
|
257 |
-
await
|
|
|
258 |
|
259 |
async def handleAiResponse(ai_data, chatId, history):
|
260 |
if ai_data and ai_data.get('choices') and len(ai_data['choices']) > 0:
|
@@ -290,8 +295,28 @@ async def sendTelegramMessage(chatId, text, options={}):
|
|
290 |
try:
|
291 |
response = requests.post(url, headers={'Content-Type': 'application/json'}, json=data)
|
292 |
response.raise_for_status()
|
|
|
293 |
except requests.exceptions.RequestException as e:
|
294 |
print(f'发送 Telegram 消息失败: {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
|
296 |
def getHelpMessage():
|
297 |
return f"""
|
|
|
105 |
userMessage = update['message'].get('text', '')
|
106 |
isGroupChat = update['message']['chat']['type'] in ['group', 'supergroup']
|
107 |
fromUserId = update['message']['from']['id']
|
108 |
+
message_id = update['message'].get('message_id')
|
109 |
|
110 |
if not userMessage:
|
111 |
return
|
|
|
172 |
|
173 |
messageContent = userMessage[len(prefix):].strip() if prefix else userMessage
|
174 |
if messageContent:
|
175 |
+
await processAiMessage(chatId, messageContent, fromUserId, message_id)
|
176 |
else:
|
177 |
+
await processAiMessage(chatId, userMessage, fromUserId, message_id)
|
178 |
|
179 |
def parseCommand(userMessage):
|
180 |
command = userMessage.split(' ')[0]
|
|
|
220 |
await sendTelegramMessage(chatId, '已重置您的个人设置。')
|
221 |
return
|
222 |
|
223 |
+
async def processAiMessage(chatId, userMessage, fromUserId, message_id):
|
224 |
history = chatHistories.get(chatId, [])
|
225 |
userTemp = USER_SETTINGS.get(fromUserId, {}).get('temperature', DEFAULT_TEMP)
|
226 |
userPromptIndex = USER_SETTINGS.get(fromUserId, {}).get('prompt_index', CURRENT_PROMPT_INDEX)
|
|
|
234 |
{'role': 'system', 'content': currentPrompt},
|
235 |
*history
|
236 |
]
|
237 |
+
|
238 |
+
thinking_message = await sendTelegramMessage(chatId, "正在思考,请等待...", reply_to_message_id=message_id)
|
239 |
+
thinking_message_id = thinking_message.get('result', {}).get('message_id')
|
240 |
|
241 |
try:
|
242 |
ai_response = requests.post(AI_API_ENDPOINT, headers=AI_API_HEADERS, json={
|
|
|
252 |
history.append({'role': 'assistant', 'content': ai_reply})
|
253 |
chatHistories[chatId] = history
|
254 |
|
255 |
+
await editTelegramMessage(chatId, thinking_message_id, ai_reply)
|
256 |
except requests.exceptions.RequestException as e:
|
257 |
print(f'AI API 响应失败: {e}')
|
258 |
+
await editTelegramMessage(chatId, thinking_message_id, 'AI API 响应失败,请稍后再试')
|
259 |
except Exception as error:
|
260 |
print(f'处理消息时发生错误: {error}')
|
261 |
+
await editTelegramMessage(chatId, thinking_message_id, '处理消息时发生错误,请稍后再试')
|
262 |
+
|
263 |
|
264 |
async def handleAiResponse(ai_data, chatId, history):
|
265 |
if ai_data and ai_data.get('choices') and len(ai_data['choices']) > 0:
|
|
|
295 |
try:
|
296 |
response = requests.post(url, headers={'Content-Type': 'application/json'}, json=data)
|
297 |
response.raise_for_status()
|
298 |
+
return response.json()
|
299 |
except requests.exceptions.RequestException as e:
|
300 |
print(f'发送 Telegram 消息失败: {e}')
|
301 |
+
return {}
|
302 |
+
|
303 |
+
async def editTelegramMessage(chatId, message_id, text, options={}):
|
304 |
+
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/editMessageText"
|
305 |
+
if PHP_PROXY_URL:
|
306 |
+
method_name = url.split('/')[-1]
|
307 |
+
url = f"{PHP_PROXY_URL}{method_name}"
|
308 |
+
data = {
|
309 |
+
'chat_id': chatId,
|
310 |
+
'message_id': message_id,
|
311 |
+
'text': text,
|
312 |
+
**options
|
313 |
+
}
|
314 |
+
try:
|
315 |
+
response = requests.post(url, headers={'Content-Type': 'application/json'}, json=data)
|
316 |
+
response.raise_for_status()
|
317 |
+
except requests.exceptions.RequestException as e:
|
318 |
+
print(f'编辑 Telegram 消息失败: {e}')
|
319 |
+
|
320 |
|
321 |
def getHelpMessage():
|
322 |
return f"""
|