Overview
NapCat provides comprehensive message handling capabilities through theNTQQMsgApi and OneBotMsgApi layers. This guide covers sending messages, receiving messages, and working with different message segment types.
Message Architecture
NapCat uses a two-layer message system:- Raw Message Layer (
NTQQMsgApi): Direct NT QQ protocol messages frommsg.ts:1-314 - OneBot Layer (
OneBotMsgApi): OneBot 11 standard compatibility fromnapcat-onebot/api/msg.ts
Sending Messages
Basic Send Message
Frommsg.ts:232-264:
Generate Unique Message ID
Before sending, generate a unique message ID:Send with Timeout
Message Segments
NapCat supports various message segment types (CQ codes in OneBot format).Text Messages
At Mentions
Images
Images are processed through
createValidSendPicElement which handles local files, URLs, and base64 data.Video
Voice (PTT)
Reply Messages
Fromnapcat-onebot/api/msg.ts:642-682:
Face Emojis
Market Face (Stickers)
Files
JSON Cards
Markdown
Receiving Messages
Parse Received Messages
Fromnapcat-onebot/api/msg.ts:1042-1098:
Message Event Structure
Forward Messages
Forward Single Message
Frommsg.ts:270-272:
Multi-Forward Messages
Frommsg.ts:274-308:
Message History
Get Message History
Frommsg.ts:192-195:
Get Specific Message
Message Operations
Recall Message
Frommsg.ts:197-207:
Mark as Read
Emoji Reactions
CQ Code Encoding/Decoding
Fromcqcode.ts:34-86:
Decode CQ Code
Encode to CQ Code
Complete Example
Best Practices
- Always set appropriate timeouts for send operations based on message content
- Handle file paths correctly - support URLs, local paths, and base64 data
- Clean up temporary files after sending to save disk space
- Use message IDs for tracking rather than sequence numbers when possible
- Parse messages carefully - handle all segment types your bot needs
- Implement error handling for network failures and invalid messages
Next Steps
Plugin Development
Build plugins to extend functionality
Packet Inspection
Monitor and analyze protocol packets
