Skip to main content

Overview

NapCat provides comprehensive message handling capabilities through the NTQQMsgApi 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 from msg.ts:1-314
  • OneBot Layer (OneBotMsgApi): OneBot 11 standard compatibility from napcat-onebot/api/msg.ts

Sending Messages

Basic Send Message

From msg.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

OneBot format:

Images

Images are processed through createValidSendPicElement which handles local files, URLs, and base64 data.
OneBot format:

Video

OneBot format:

Voice (PTT)

OneBot format:

Reply Messages

From napcat-onebot/api/msg.ts:642-682:
OneBot format:

Face Emojis

OneBot format:

Market Face (Stickers)

Files

OneBot format:

JSON Cards

Markdown

Receiving Messages

Parse Received Messages

From napcat-onebot/api/msg.ts:1042-1098:

Message Event Structure

Forward Messages

Forward Single Message

From msg.ts:270-272:

Multi-Forward Messages

From msg.ts:274-308:

Message History

Get Message History

From msg.ts:192-195:

Get Specific Message

Message Operations

Recall Message

From msg.ts:197-207:

Mark as Read

Emoji Reactions

CQ Code Encoding/Decoding

From cqcode.ts:34-86:

Decode CQ Code

Encode to CQ Code

Complete Example

Large media files (images, videos, voice) may require longer timeouts. Calculate timeout based on file size: 10000 + (fileSize / 1024 / 256 * 1000) milliseconds.

Best Practices

  1. Always set appropriate timeouts for send operations based on message content
  2. Handle file paths correctly - support URLs, local paths, and base64 data
  3. Clean up temporary files after sending to save disk space
  4. Use message IDs for tracking rather than sequence numbers when possible
  5. Parse messages carefully - handle all segment types your bot needs
  6. 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