What is OneBot?
OneBot is a standardized chatbot protocol designed to provide a unified API interface for different chat platforms. NapCat implements the OneBot 11 standard to expose QQ bot functionality through HTTP, WebSocket, and other network protocols.OneBot 11 is the most widely supported version, compatible with most bot frameworks and libraries in the ecosystem.
Architecture
NapCatOneBot11Adapter
The OneBot implementation is packaged as an adapter that wrapsNapCatCore:
packages/napcat-onebot/index.ts:69
Key components:
- Core: Access to NapCat’s native QQ APIs
- APIs: OneBot-specific API implementations
- NetworkManager: Manages all network adapters (HTTP/WS/etc)
- Actions: Maps OneBot actions to handler functions
Component Diagram
Action & Event Model
Actions (Requests)
Actions are requests sent to the bot to perform operations. They follow this structure:Events (Push)
Events are pushed from the bot when something happens. Event types:packages/napcat-onebot/event/OneBotEvent.ts:3
Event Base Structure
time: Unix timestampself_id: Bot’s QQ numberpost_type: Event category
packages/napcat-onebot/event/OneBotEvent.ts:11
Message Format
OneBot 11 supports two message formats:Array Format (Recommended)
String Format (CQ Code)
Message Elements
Common message element types:Text
Text
At
At
Image
Image
Face
Face
Record (Voice)
Record (Voice)
Video
Video
Action Implementation
Actions are implemented using a registry pattern:packages/napcat-onebot/action/index.ts
Action Handler Example
Here’s how an action handler is structured:Event Handling
Message Events
Incoming messages are processed and converted to OneBot format:packages/napcat-onebot/index.ts:309
Notice Events
System events like group member changes, kicks, recalls:Request Events
Friend and group requests:packages/napcat-onebot/index.ts:436
Network Manager
TheOB11NetworkManager handles event distribution to all active adapters:
packages/napcat-onebot/network/index.ts:15
Network Adapters
Supported adapter types:HTTP Server
Listens for POST requests with actions
WebSocket Server
Bidirectional communication, client connects to bot
HTTP Client
Posts events to remote URL (reverse HTTP)
WebSocket Client
Connects to remote WS server (reverse WS)
Configuration
OneBot configuration is loaded viaOB11ConfigLoader:
packages/napcat-onebot/config/config.ts
Example Configuration
Common Actions
send_msg - Send message
send_msg - Send message
delete_msg - Recall message
delete_msg - Recall message
get_group_info - Get group info
get_group_info - Get group info
set_group_ban - Ban group member
set_group_ban - Ban group member
get_login_info - Get bot info
get_login_info - Get bot info
Extension: Quick Actions
NapCat extends OneBot with quick action support for rapid responses:packages/napcat-onebot/api/quick-action.ts
Related
Adapters
Configure HTTP and WebSocket adapters
Architecture
Understand the core architecture
API Reference
Full action and event reference
