← Back to Plugins
Integration

Onebot

Chimarya By Chimarya 👁 199 views ▲ 0 votes

OpenClaw OneBot Plugin for QQ Chat Integration

GitHub

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

Loading comments...