Integration
Onebot
OpenClaw OneBot Plugin for QQ Chat Integration
Install
npm install
```
README
# OpenClaw OneBot Plugin
[δΈζ](./README_zh.md) | English
OpenClaw OneBot Plugin is a plugin for QQ chat integration, supporting OneBot V11 protocol.
## Features
- β
Complete OneBot V11 protocol support
- β
HTTP API server
- β
WebSocket real-time communication
- β
Message routing system
- β
Session management
- β
Event handling
- β
Extension system
- β
Performance monitoring
- β
State management
- β
TypeScript support
## Installation
```bash
npm install
```
## Quick Start
```typescript
import { createOpenClawOneBot } from './src/index.js';
const bot = await createOpenClawOneBot({
config: {
port: 8080,
accessToken: 'your-secret-token',
selfId: 123456789,
enableHttp: true,
enableWs: true
}
});
await bot.initialize();
await bot.start();
// Add command handler
bot.getMessageRouter().addCommand('hello', async (ctx) => {
return `Hello, ${ctx.nickname}!`;
});
console.log('Bot started!');
```
## Configuration
### OneBotConfig
```typescript
interface OneBotConfig {
port: number; // HTTP/WebSocket port (default: 8080)
accessToken: string; // Access token
enableHttp: boolean; // Enable HTTP server (default: true)
enableWs: boolean; // Enable WebSocket server (default: true)
host?: string; // Host address (default: 0.0.0.0)
maxConnections?: number; // Max connections (default: 100)
selfId?: number; // Bot QQ number
}
```
## Usage Examples
### Message Routing
```typescript
// Command route
bot.getMessageRouter().addCommand('help', async (ctx) => {
return `Available commands:
/help - Show help
/weather <city> - Query weather
`;
});
// Keyword route
bot.getMessageRouter().addKeyword('weather', async (ctx) => {
return 'Today is sunny!';
});
// Regex route
bot.getMessageRouter().addRegex(/^(\d+)\+(\d+)$/, async (ctx) => {
const [a, b] = ctx.messageText.split('+').map(Number);
return `${a} + ${b} = ${a + b}`;
});
```
### Session Management
```typescript
// Create session
const session = bot.getSessionManager().getOrCreate(event);
session.data.username = 'test';
// Get session
const session = bot.getSessionManager().get('private_123456');
```
### Event Handling
```typescript
// Message event
bot.getEventManager().onMessage('my-handler', async (ctx) => {
console.log('Received message:', ctx.event.message);
});
// Notice event
bot.getEventManager().onNotice('group-notice', async (ctx) => {
console.log('Group notice:', ctx.event);
});
// Request event
bot.getEventManager().onRequest('friend-request', async (ctx) => {
return { approve: true }; // Auto approve
});
```
### Extension System
```typescript
const extension = {
name: 'my-extension',
version: '1.0.0',
description: 'My custom extension',
author: 'Chimarya',
initialize: async () => {
console.log('Extension initialized');
},
getCommands: () => [
{
name: 'test',
description: 'Test command',
usage: '/test',
handler: async (args) => 'Test response!'
}
]
};
await bot.getExtensionManager().register(extension);
```
## Project Structure
```
openclaw-onebot-plugin/
βββ src/
β βββ config.ts # Configuration management
β βββ protocol.ts # Protocol definitions
β βββ message-parser.ts # Message parsing
β βββ message-formatter.ts # Message formatting
β βββ request-validator.ts # Request validation
β βββ response-formatter.ts # Response formatting
β βββ http-server.ts # HTTP server
β βββ ws-server.ts # WebSocket server
β βββ api.ts # API interface
β βββ onebot-api.ts # OneBot HTTP API
β βββ onebot-ws.ts # OneBot WebSocket API
β βββ onebot-plugin.ts # Core plugin class
β βββ message-router.ts # Message routing
β βββ session-manager.ts # Session management
β βββ event-handler.ts # Event handling
β βββ event-manager.ts # Event management
β βββ openclaw-adapter.ts # OpenClaw adapter
β βββ plugin-config.ts # Plugin configuration
β βββ extensions.ts # Extension system
β βββ utils/
β β βββ state-manager.ts # State management
β β βββ performance.ts # Performance monitoring
β βββ index.ts # Main entry
βββ tests/
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
βββ docs/
β βββ API.md # API documentation
βββ package.json
βββ tsconfig.json
```
## Testing
```bash
npm test
```
## Building
```bash
npm run build
```
## License
MIT
## Author
Chimarya
## Contributing
Issues and Pull Requests are welcome!
integration
Comments
Sign in to leave a comment