Skip to content

Commit e0d84a4

Browse files
committed
feat(AI功能): 添加AI功能开关配置及处理逻辑
在config.py和r.py中添加AI功能开关配置,并在bot_client.py中根据开关状态调整消息处理逻辑。当AI功能关闭时,使用简单回复代替AI聊天功能。
1 parent b922d94 commit e0d84a4

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

bot_client.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from handlers.bus import query_bus
2121
from handlers.classroom import query_empty_classroom
2222

23-
from config import APPID, SECRET
23+
from config import APPID, SECRET, AI_GROUP_ENABLED, AI_DIRECT_ENABLED
2424

2525
_log = botpy.logging.get_logger()
2626

@@ -73,11 +73,14 @@ async def on_ready(self):
7373

7474
async def on_c2c_message_create(self, message: DirectMessage):
7575
"""私聊消息处理"""
76-
try:
77-
if await direct_chat_with_clawdbot(api=self.api, message=message):
78-
return
79-
except Exception as e:
80-
# 出错时回退到简单回复
76+
if AI_DIRECT_ENABLED:
77+
try:
78+
if await direct_chat_with_clawdbot(api=self.api, message=message):
79+
return
80+
except Exception as e:
81+
content = message.content
82+
await message.reply(content=content)
83+
else:
8184
content = message.content
8285
await message.reply(content=content)
8386

@@ -87,21 +90,30 @@ async def on_group_at_message_create(self, message: GroupMessage):
8790
if await handler(api=self.api, message=message):
8891
return
8992

90-
# 如果没有处理器处理,尝试使用 group_chat_with_clawdbot
91-
try:
92-
if await group_chat_with_clawdbot(api=self.api, message=message):
93-
return
94-
except Exception as e:
95-
# 出错时回退到群组查找
93+
if AI_GROUP_ENABLED:
94+
try:
95+
if await group_chat_with_clawdbot(api=self.api, message=message):
96+
return
97+
except Exception as e:
98+
user_input = message.content.strip().replace("群", "")
99+
if user_input:
100+
try:
101+
await internal_find_group(api=self.api, message=message, search_key=user_input)
102+
return
103+
except Exception as find_error:
104+
await message.reply(content=f"调用出错: {str(find_error)}")
105+
else:
106+
await message.reply(content=f"调用出错: {str(e)}")
107+
else:
96108
user_input = message.content.strip().replace("群", "")
97109
if user_input:
98110
try:
99111
await internal_find_group(api=self.api, message=message, search_key=user_input)
100112
return
101-
except Exception as find_error:
102-
await message.reply(content=f"调用出错: {str(find_error)}")
113+
except Exception as e:
114+
await message.reply(content=f"调用出错: {str(e)}")
103115
else:
104-
await message.reply(content=f"调用出错: {str(e)}")
116+
await message.reply(content=f"不明白你在说什么哦(๑• . •๑)")
105117

106118
async def on_group_add_robot(self, message: GroupManageEvent):
107119
"""机器人被添加到群组事件"""

config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@
5353

5454
# 三角洲行动API配置
5555
DELTAFORCE_API_TOKEN = r.deltaforce_api_token
56-
CLASS_API_KEY = r.class_api_token
56+
CLASS_API_KEY = r.class_api_token
57+
58+
# AI功能开关
59+
AI_GROUP_ENABLED = r.ai_group_enabled
60+
AI_DIRECT_ENABLED = r.ai_direct_enabled

r.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ def update_env_variable(key, value):
124124
# 查询空教室API配置
125125
class_api_token = os.getenv("CLASS_API_KEY")
126126
if class_api_token is None:
127-
raise Exception('Missing "CLASS_API_KEY" environment variable for CLASS_API_KEY')
127+
raise Exception('Missing "CLASS_API_KEY" environment variable for CLASS_API_KEY')
128+
129+
# AI功能开关配置
130+
ai_group_enabled = os.getenv("AI_GROUP_ENABLED", "false").lower() == "true"
131+
ai_direct_enabled = os.getenv("AI_DIRECT_ENABLED", "false").lower() == "true"

0 commit comments

Comments
 (0)