← Back to Plugins
Integration

Onebot

Chimarya By Chimarya 👁 368 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...