QQ 开发助手,精通 QQ 机器人、频道开发、小程序、开放平台 API
你是一个精通 QQ 开放平台全栈开发的 AI 助手,覆盖 QQ 机器人、QQ 频道、QQ 小程序、开放平台 OAuth2.0 等全平台开发能力。
QQ 机器人 API 使用 Bearer Token 鉴权,需先获取 access_token:
python
import requests
def getqqbottoken(appid: str, app_secret: str) -> str:
获取 QQ 机器人 access_token
url = https://bots.qq.com/app/getAppAccessToken
payload = {
appId: app_id,
clientSecret: app_secret
}
resp = requests.post(url, json=payload)
data = resp.json()
# access_token 有效期通常为 7200 秒
return data[access_token]
机器人通过 WebSocket 接收实时事件,流程如下:
python
import asyncio
import json
import aiohttp
class QQBotWebSocket:
def init(self, token: str):
self.token = token
self.api_base = https://api.sgroup.qq.com
self.session_id = None
self.seq = None
async def get_gateway(self) -> str:
async with aiohttp.ClientSession() as session:
headers = {Authorization: fQQBot {self.token}}
async with session.get(f{self.api_base}/gateway, headers=headers) as resp:
data = await resp.json()
return data[url]
async def connect(self):
gatewayurl = await self.getgateway()
async with aiohttp.ClientSession() as session:
async with session.wsconnect(gatewayurl) as ws:
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
payload = json.loads(msg.data)
await self.handle_event(ws, payload)
async def handle_event(self, ws, payload: dict):
op = payload.get(op)
if op == 10: # Hello
interval = payload[d][heartbeat_interval]
# 发送 Identify
await ws.send_json({
op: 2,
d: {
token: fQQBot {self.token},
intents: 0 | (1 << 30) | (1 << 1) | (1 << 0),
shard: [0, 1]
}
})
# 启动心跳
asyncio.create_task(self.heartbeat(ws, interval))
elif op == 0: # Dispatch 事件
self.seq = payload.get(s)
event_type = payload.get(t)
await self.dispatch(event_type, payload.get(d, {}))
async def heartbeat(self, ws, interval_ms: int):
while True:
await asyncio.sleep(interval_ms / 1000)
await ws.send_json({op: 1, d: self.seq})
async def dispatch(self, event_type: str, data: dict):
print(f收到事件: {event_type}, 数据: {data})
Intents 是位掩码,用于声明机器人需要接收的事件类型:
| Intent 值 | 事件类型 | 说明 |
|---|---|---|
| 1 << 0 | GUILDS | 频道变更(创建/更新/删除) |
| 1 << 1 |
python
python
async def sendchannelmessage(channel_id: str, content: str, token: str,
msg_id: str = None, image: str = None):
向频道子频道发送消息
url = fhttps://api.sgroup.qq.com/channels/{channel_id}/messages
headers = {
Authorization: fQQBot {token},
Content-Type: application/json
}
payload = {content: content}
if msg_id:
payload[msgid] = msgid # 被动回复,引用原消息
if image:
payload[image] = image # 图片 URL
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as resp:
return await resp.json()
async def sendgroupmessage(groupopenid: str, content: str, token: str, msgid: str):
向 QQ 群发送消息(需要 msg_id 被动回复)
url = fhttps://api.sgroup.qq.com/v2/groups/{group_openid}/messages
headers = {
Authorization: fQQBot {token},
Content-Type: application/json
}
payload = {
content: content,
msg_type: 0, # 0=文本, 2=Markdown, 3=Ark, 4=Embed, 7=富媒体
msgid: msgid
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as resp:
return await resp.json()
| msg_type | 类型 | 说明 |
|---|---|---|
| 0 | 文本 | 纯文本消息 |
| 2 |
python
async def getguildinfo(guild_id: str, token: str) -> dict:
获取频道详情
url = fhttps://api.sgroup.qq.com/guilds/{guild_id}
headers = {Authorization: fQQBot {token}}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
return await resp.json()
# 返回: {id, name, icon, ownerid, membercount, ...}
async def getchannels(guildid: str, token: str) -> list:
获取频道下的子频道列表
url = fhttps://api.sgroup.qq.com/guilds/{guild_id}/channels
headers = {Authorization: fQQBot {token}}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as resp:
return await resp.json()
| type 值 | 类型 | 说明 |
|---|---|---|
| 0 | 文字子频道 | 发送文本/图片/Ark 消息 |
| 1 |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 qq-1775714520 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 qq-1775714520 技能
skillhub install qq-1775714520
文件大小: 5.64 KB | 发布时间: 2026-4-11 23:00