tencent-connect/botpy: QQ频道机器人PythonSDKtencent-connect/botpy | DeepWiki2026-02-23 13:03:09,652 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill web_skill from directory skills\web_skill. 2026-02-23 13:03:09,653 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill ue_skill from directory skills\ue_skill. 2026-02-23 13:03:09,653 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill blenderskill from directory skills\blender_skill. 启动 QQ 机器人... 使用模型: 杜野りんせ 机器人已准备就绪等待消息... [INFO] (client.py:162)_bot_login [botpy] 登录机器人账号中... [INFO] (robot.py:65)update_access_token [botpy] access_token expires_in 6263 e:\code\my_python_server\.venv\lib\site-packages\botpy\http.py:153: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated. connectorTCPConnector(limit500, sslSSLContext(), force_closeTrue), e:\code\my_python_server\.venv\lib\site-packages\botpy\http.py:153: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated connectorTCPConnector(limit500, sslSSLContext(), force_closeTrue), [INFO] (client.py:181)_bot_init [botpy] 程序启动... [INFO] (connection.py:60)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 [INFO] (client.py:242)bot_connect [botpy] 会话启动中... [INFO] (gateway.py:115)ws_connect [botpy] 启动中... e:\code\my_python_server\.venv\lib\site-packages\botpy\gateway.py:121: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated. async with ClientSession(connectorTCPConnector(limit10, sslSSLContext())) as session: e:\code\my_python_server\.venv\lib\site-packages\botpy\gateway.py:121: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated async with ClientSession(connectorTCPConnector(limit10, sslSSLContext())) as session: [INFO] (gateway.py:142)ws_identify [botpy] 鉴权中... [INFO] (gateway.py:85)on_message [botpy] 机器人「杜野凛世-测试中」启动成功 [INFO] (gateway.py:223)_send_heart [botpy] 心跳维持启动... (.venv) PS E:\code\my_python_server e:; cd e:\code\my_python_server; e:\code\my_python_server\.venv\Scripts\python.exe c:\Users\njsgcs\.lingma\extensions\ms-python.debugpy-2025.19.2026021801-win32-x64\bundled\libs\debugpy\launcher 65027 -- E:\code\my_python_server\front\qq\qqbot.py 2026-02-23 13:09:29,053 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill web_skill from directory skills\web_skill. 2026-02-23 13:09:29,054 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill ue_skill from directory skills\ue_skill. 2026-02-23 13:09:29,054 | INFO | _toolkit:register_agent_skill:1104 - Registered agent skill blenderskill from directory skills\blender_skill. 启动 QQ 机器人... 使用模型: 杜野りんせ 支持频道消息和好友私聊 ⏳ 正在连接... [INFO] (client.py:162)_bot_login [botpy] 登录机器人账号中... [INFO] (robot.py:65)update_access_token [botpy] access_token expires_in 5883 e:\code\my_python_server\.venv\lib\site-packages\botpy\http.py:153: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated. connectorTCPConnector(limit500, sslSSLContext(), force_closeTrue), e:\code\my_python_server\.venv\lib\site-packages\botpy\http.py:153: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated connectorTCPConnector(limit500, sslSSLContext(), force_closeTrue), [INFO] (client.py:181)_bot_init [botpy] 程序启动... [INFO] (connection.py:60)multi_run [botpy] 最大并发连接数: 1, 启动会话数: 1 [INFO] (client.py:242)bot_connect [botpy] 会话启动中... [INFO] (gateway.py:115)ws_connect [botpy] 启动中... e:\code\my_python_server\.venv\lib\site-packages\botpy\gateway.py:121: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated. async with ClientSession(connectorTCPConnector(limit10, sslSSLContext())) as session: e:\code\my_python_server\.venv\lib\site-packages\botpy\gateway.py:121: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated async with ClientSession(connectorTCPConnector(limit10, sslSSLContext())) as session: [INFO] (gateway.py:142)ws_identify [botpy] 鉴权中... [INFO] (gateway.py:85)on_message [botpy] 机器人「杜野凛世-测试中」启动成功 [INFO] (gateway.py:223)_send_heart [botpy] 心跳维持启动... [2026-02-23 13:09:29] [on_ready] 机器人已上线 ✅ 支持频道消息和好友私聊 [2026-02-23 13:09:38] [C2C消息] 你好 杜野りんせ: こんにちは何かお手伝いできることがありますか [2026-02-23 13:09:38] [C2C回复] こんにちは何かお手伝いできることがあります かllmchat方法自己实现接受一个输入返回一个输出就行import sys import os sys.path.append(os.getcwd()) import botpy from botpy.message import Message, C2CMessage import asyncio from typing import Callable from datetime import datetime # 导入你的 LLM 聊天函数 from agent_scope.chat_llm import llm_chat from share_config.api_key import qq_appid, qq_secret class QQBotClient(botpy.Client): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.chat_handler: Callable[[str], str] None def set_chat_handler(self, handler: Callable[[str], str]): 注册聊天处理函数 if not callable(handler): raise ValueError(handler 必须是可调用对象) self.chat_handler handler async def _call_handler(self, text: str) - str: 统一调用同步/异步 handler if asyncio.iscoroutinefunction(self.chat_handler): return await self.chat_handler(text) else: return self.chat_handler(text) async def on_ready(self): 机器人启动完成回调 print(f[{datetime.now().strftime(%Y-%m-%d %H:%M:%S)}] [on_ready] 机器人已上线) print(✅ 支持频道消息和好友私聊) async def on_at_message_create(self, message: Message): 频道消息处理 if not self.chat_handler: await message.reply(content聊天服务未初始化) return # 获取用户消息内容 user_text message.content or timestamp datetime.now().strftime(%Y-%m-%d %H:%M:%S) print(f[{timestamp}] [频道消息] {user_text}) try: # 调用你的 LLM 处理函数 reply_text await self._call_handler(user_text) print(f[{timestamp}] [频道回复] {reply_text[:100]}{... if len(reply_text) 100 else }) # 如果回复内容过长分段发送 if len(reply_text) 1000: chunks [reply_text[i:i1000] for i in range(0, len(reply_text), 1000)] for chunk in chunks: await message.reply(contentchunk) else: await message.reply(contentreply_text) except Exception as e: error_msg f处理消息时出错: {str(e)} print(f[{timestamp}] [频道错误] {error_msg}) await message.reply(contenterror_msg) async def on_c2c_message_create(self, message: C2CMessage): C2C好友私聊消息处理 if not self.chat_handler: await self.api.post_c2c_message( openidmessage.author.user_openid, content聊天服务未初始化 ) return # 获取用户消息内容 user_text message.content or timestamp datetime.now().strftime(%Y-%m-%d %H:%M:%S) print(f[{timestamp}] [C2C消息] {user_text}) try: # 调用你的 LLM 处理函数 reply_text await self._call_handler(user_text) print(f[{timestamp}] [C2C回复] {reply_text[:100]}{... if len(reply_text) 100 else }) # 发送 C2C 回复 await self.api.post_c2c_message( openidmessage.author.user_openid, contentreply_text ) except Exception as e: error_msg f处理消息时出错: {str(e)} print(f[{timestamp}] [C2C错误] {error_msg}) await self.api.post_c2c_message( openidmessage.author.user_openid, contenterror_msg ) def main(): # 创建机器人实例 - 开启公域 C2C/群消息 intents botpy.Intents(public_messagesTrue) client QQBotClient(intentsintents) # 注册你的 LLM 聊天处理函数 client.set_chat_handler(llm_chat) # 启动机器人 print( 启动 QQ 机器人...) print(f 使用模型: 杜野りんせ) print( 支持频道消息和好友私聊) print(⏳ 正在连接...) client.run(appidqq_appid, secretqq_secret) if __name__ __main__: main()