← Back to Plugins
Tools

Http Chat

zuohl By zuohl 👁 6 views ▲ 0 votes

HTTP Chat plugin for OpenClaw - Sync HTTP chat interface for miniprogram, App, and Web clients

GitHub

Install

npm install

#

Configuration Example

{
  "channels": {
    "http-chat": {
      "enabled": true,
      "port": 3001,
      "secret": "your-secret-key"
    }
  }
}

README

# OpenClaw HTTP Chat Plugin

通用 HTTP 同步对话插件,支持小程序、App、Web 等任意 HTTP 客户端。

## 特点

- 🌐 **通用**:任何能发 HTTP POST 的客户端都能用
- ⚡ **简单**:一个接口完成对话
- 🔄 **同步**:发消息后直接返回 AI 回复
- 🔐 **安全**:支持签名校验

## 安装

### 从本地安装

```bash
openclaw plugin install ./openclaw-http-chat
```

### 从 GitHub 安装

```bash
openclaw plugin install github:zuohl/openclaw-http-chat
```

## 配置

在 `openclaw.json` 中添加:

```json
{
  "channels": {
    "http-chat": {
      "enabled": true,
      "port": 3001,
      "secret": "your-secret-key"
    }
  }
}
```

| 参数 | 说明 | 默认值 |
|------|------|--------|
| `enabled` | 是否启用 | `true` |
| `port` | HTTP 服务端口 | `3001` |
| `secret` | 签名密钥(可选) | - |

## 使用方式

### 请求格式

```bash
POST http://localhost:3001/chat
Content-Type: application/json

{
  "openid": "user_123",
  "message": "你好"
}
```

| 参数 | 说明 | 必填 |
|------|------|------|
| `openid` | 用户唯一标识 | ✅ |
| `message` | 用户消息 | ✅ |

### 响应格式

```json
{
  "ok": true,
  "reply": "你好!有什么可以帮你的?",
  "messageId": "msg_1743887862000"
}
```

### 错误响应

```json
{
  "ok": false,
  "error": "缺少 openid 或 message"
}
```

## 客户端示例

### 微信小程序

```javascript
Page({
  async sendMessage() {
    const res = await wx.request({
      url: "https://your-server:3001/chat",
      method: "POST",
      data: {
        openid: getApp().globalData.openid,
        message: "你好"
      }
    });
    
    if (res.data.ok) {
      console.log(res.data.reply);
    }
  }
});
```

### Web 前端

```javascript
async function chat(message) {
  const res = await fetch("http://localhost:3001/chat", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      openid: "user_123",
      message
    })
  });
  
  const data = await res.json();
  return data.reply;
}
```

### App (React Native / Flutter / Native)

任何能发 HTTP POST 的客户端都可以调用。

## 安全建议

### 1. 启用签名校验

设置 `secret` 后,客户端需要在请求头带上签名:

```javascript
import crypto from 'crypto';

const body = { openid: "user_123", message: "你好" };
const signature = crypto
  .createHmac("sha256", "your-secret-key")
  .update(JSON.stringify(body))
  .digest("hex");

fetch("http://localhost:3001/chat", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-signature": signature
  },
  body: JSON.stringify(body)
});
```

### 2. 使用 HTTPS

生产环境建议通过 Nginx 或其他反向代理启用 HTTPS。

### 3. 加中间层 BFF

建议在 OpenClaw 和业务系统之间加一层 BFF:

```
客户端 → BFF(身份/权限/审计)→ OpenClaw HTTP Chat
```

BFF 负责:
- 用户身份转换(openid → 业务 userId)
- 权限校验
- 审计日志
- 数据隔离

## 健康检查

```bash
curl http://localhost:3001/health
# {"ok":true,"running":true,"port":3001}
```

## 架构

```
┌─────────────┐
│ 小程序/App  │
│   /Web      │
└──────┬──────┘
       │ HTTP POST
       ▼
┌─────────────────────────────────┐
│  openclaw-http-chat 插件         │
│  (端口 3001)                     │
│  - 接收消息                       │
│  - 调用 OpenClaw agent           │
│  - 同步返回回复                   │
└─────────────────────────────────┘
       │
       ▼
┌─────────────┐
│  OpenClaw   │
│  Agent      │
└─────────────┘
```

## 开发

```bash
# 克隆仓库
git clone https://github.com/zuohl/openclaw-http-chat.git

# 安装依赖
cd openclaw-http-chat
npm install

# 本地测试
openclaw plugin install .
```

## License

MIT

## 作者

龙虾同学 🦞
tools

Comments

Sign in to leave a comment

Loading comments...