Update app.py
Browse files
app.py
CHANGED
@@ -102,7 +102,6 @@ TOOL_DEFINITIONS = [
|
|
102 |
},
|
103 |
]
|
104 |
|
105 |
-
|
106 |
class EventEmitter:
|
107 |
def __init__(self, event_emitter):
|
108 |
self.event_emitter = event_emitter
|
@@ -175,7 +174,6 @@ def parse_command(user_message):
|
|
175 |
command = command.split("@")[0]
|
176 |
return command[1:]
|
177 |
|
178 |
-
|
179 |
async def handle_telegram_update(update: Update, context):
|
180 |
if not update.message:
|
181 |
if update.callback_query:
|
@@ -287,7 +285,6 @@ async def handle_telegram_update(update: Update, context):
|
|
287 |
else:
|
288 |
await process_ai_message(chat_id, user_message, from_user_id, context)
|
289 |
|
290 |
-
|
291 |
async def process_ai_message(chat_id, user_message, from_user_id, context):
|
292 |
history = chat_histories.get(chat_id, [])
|
293 |
user_temp = USER_SETTINGS.get(from_user_id, {}).get("temperature", DEFAULT_TEMP)
|
@@ -337,7 +334,6 @@ async def process_ai_message(chat_id, user_message, from_user_id, context):
|
|
337 |
logging.error(f"处理消息时发生错误: {error}")
|
338 |
await send_telegram_message(chat_id=chat_id, text="处理消息时发生错误,请稍后再试", context=context)
|
339 |
|
340 |
-
|
341 |
async def handle_ai_response(ai_data, chat_id, history, event_emitter, context):
|
342 |
if ai_data and ai_data.get("choices") and len(ai_data["choices"]) > 0:
|
343 |
choice = ai_data["choices"][0]
|
@@ -387,7 +383,6 @@ async def handle_ai_response(ai_data, chat_id, history, event_emitter, context):
|
|
387 |
return "AI 返回了无法识别的格式"
|
388 |
return "AI 返回了无法识别的格式"
|
389 |
|
390 |
-
|
391 |
async def execute_tool_call(tool_call, event_emitter):
|
392 |
name = tool_call["function"]["name"]
|
393 |
args = tool_call["function"].get("arguments", {})
|
@@ -484,7 +479,6 @@ async def execute_tool_call(tool_call, event_emitter):
|
|
484 |
"content": "未知的工具调用",
|
485 |
}
|
486 |
|
487 |
-
|
488 |
async def handle_callback_query(callback_query, context):
|
489 |
chat_id = callback_query.message.chat.id
|
490 |
data = callback_query.data
|
@@ -502,14 +496,12 @@ async def handle_callback_query(callback_query, context):
|
|
502 |
context=context
|
503 |
)
|
504 |
|
505 |
-
|
506 |
async def send_event_message(chat_id, event, context):
|
507 |
if event["type"] == "status":
|
508 |
await send_telegram_message(chat_id=chat_id, text=f"状态更新: {event['data']['description']}", context=context)
|
509 |
elif event["type"] == "citation":
|
510 |
await send_telegram_message(chat_id=chat_id, text=f"引用信息: {event['data']['metadata'][0]['name']}", context=context)
|
511 |
|
512 |
-
|
513 |
async def send_telegram_message(chat_id, text, context, options=None):
|
514 |
url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
515 |
json_data = {"chat_id": chat_id, "text": text}
|
@@ -524,7 +516,6 @@ async def send_telegram_message(chat_id, text, context, options=None):
|
|
524 |
except Exception as error:
|
525 |
logging.error(f"发送 Telegram 消息时发生错误: {error}")
|
526 |
|
527 |
-
|
528 |
def get_help_message():
|
529 |
return f"""
|
530 |
可用指令:
|
@@ -554,7 +545,6 @@ def get_help_message():
|
|
554 |
- 机器人具有攻击性,请谨慎使用。
|
555 |
"""
|
556 |
|
557 |
-
|
558 |
async def main():
|
559 |
logging.basicConfig(level=logging.INFO)
|
560 |
|
@@ -591,9 +581,12 @@ async def main():
|
|
591 |
# Message handler for all text messages
|
592 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_telegram_update))
|
593 |
|
|
|
|
|
594 |
# Start the bot
|
595 |
-
await application.
|
|
|
|
|
596 |
|
597 |
if __name__ == "__main__":
|
598 |
-
|
599 |
-
loop.run_until_complete(main())
|
|
|
102 |
},
|
103 |
]
|
104 |
|
|
|
105 |
class EventEmitter:
|
106 |
def __init__(self, event_emitter):
|
107 |
self.event_emitter = event_emitter
|
|
|
174 |
command = command.split("@")[0]
|
175 |
return command[1:]
|
176 |
|
|
|
177 |
async def handle_telegram_update(update: Update, context):
|
178 |
if not update.message:
|
179 |
if update.callback_query:
|
|
|
285 |
else:
|
286 |
await process_ai_message(chat_id, user_message, from_user_id, context)
|
287 |
|
|
|
288 |
async def process_ai_message(chat_id, user_message, from_user_id, context):
|
289 |
history = chat_histories.get(chat_id, [])
|
290 |
user_temp = USER_SETTINGS.get(from_user_id, {}).get("temperature", DEFAULT_TEMP)
|
|
|
334 |
logging.error(f"处理消息时发生错误: {error}")
|
335 |
await send_telegram_message(chat_id=chat_id, text="处理消息时发生错误,请稍后再试", context=context)
|
336 |
|
|
|
337 |
async def handle_ai_response(ai_data, chat_id, history, event_emitter, context):
|
338 |
if ai_data and ai_data.get("choices") and len(ai_data["choices"]) > 0:
|
339 |
choice = ai_data["choices"][0]
|
|
|
383 |
return "AI 返回了无法识别的格式"
|
384 |
return "AI 返回了无法识别的格式"
|
385 |
|
|
|
386 |
async def execute_tool_call(tool_call, event_emitter):
|
387 |
name = tool_call["function"]["name"]
|
388 |
args = tool_call["function"].get("arguments", {})
|
|
|
479 |
"content": "未知的工具调用",
|
480 |
}
|
481 |
|
|
|
482 |
async def handle_callback_query(callback_query, context):
|
483 |
chat_id = callback_query.message.chat.id
|
484 |
data = callback_query.data
|
|
|
496 |
context=context
|
497 |
)
|
498 |
|
|
|
499 |
async def send_event_message(chat_id, event, context):
|
500 |
if event["type"] == "status":
|
501 |
await send_telegram_message(chat_id=chat_id, text=f"状态更新: {event['data']['description']}", context=context)
|
502 |
elif event["type"] == "citation":
|
503 |
await send_telegram_message(chat_id=chat_id, text=f"引用信息: {event['data']['metadata'][0]['name']}", context=context)
|
504 |
|
|
|
505 |
async def send_telegram_message(chat_id, text, context, options=None):
|
506 |
url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
507 |
json_data = {"chat_id": chat_id, "text": text}
|
|
|
516 |
except Exception as error:
|
517 |
logging.error(f"发送 Telegram 消息时发生错误: {error}")
|
518 |
|
|
|
519 |
def get_help_message():
|
520 |
return f"""
|
521 |
可用指令:
|
|
|
545 |
- 机器人具有攻击性,请谨慎使用。
|
546 |
"""
|
547 |
|
|
|
548 |
async def main():
|
549 |
logging.basicConfig(level=logging.INFO)
|
550 |
|
|
|
581 |
# Message handler for all text messages
|
582 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_telegram_update))
|
583 |
|
584 |
+
# Initialize the bot
|
585 |
+
await application.initialize()
|
586 |
# Start the bot
|
587 |
+
await application.start()
|
588 |
+
# Start polling updates from Telegram
|
589 |
+
await application.updater.start_polling()
|
590 |
|
591 |
if __name__ == "__main__":
|
592 |
+
asyncio.run(main())
|
|