Metadata-Version: 2.4
Name: nonebot-adapter-yunhu
Version: 0.2.0
Summary: A NoneBot adapter for YunHu
License: MIT
License-File: LICENSE
Keywords: yunhu,adapter,nonebot2
Author: molanp
Author-email: molanpp@outlook.com
Requires-Python: >=3.10,<4.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Robot Framework
Classifier: Framework :: Robot Framework :: Library
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Requires-Dist: nonebot2 (>=2.3.0)
Requires-Dist: puremagic (>=1.20)
Requires-Dist: pydantic (>=1.10.0,<3.0.0,!=2.5.0,!=2.5.1)
Requires-Dist: typing-extensions (>=4.3.0)
Project-URL: Homepage, https://github.com/molanp/nonebot-adapter-yunhu
Project-URL: Repository, https://github.com/molanp/nonebot-adapter-yunhu
Description-Content-Type: text/markdown

<p align="center">
  <a href="https://nonebot.dev/"><img src="https://nonebot.dev/logo.png" width="200" height="200" alt="nonebot"></a>
</p>

<div align="center">
NoneBot-Adapter-YunHu

_✨ YunHu adapter for NoneBot2 ✨_

[![PyPI](https://img.shields.io/pypi/v/nonebot-adapter-yunhu)](https://pypi.org/project/nonebot-adapter-yunhu/)
![GitHub](https://img.shields.io/github/license/molanp/nonebot-adapter-yunhu)

</div>

## 支持情况

### 事件支持情况

- [x] 基础消息事件 / 指令消息事件
- [x] 关注 / 取关机器人事件
- [x] 加入 / 退出群事件
- [x] 按钮事件上报接收
- [x] 快捷菜单事件 (`bot.shortcut.menu`)
- [x] 机器人设置事件 (`bot.setting`)
- [x] A2UI 表单 / 按钮事件 (`a2ui.button.report`)
- [x] 按钮发送
- [x] 表单接收

### 事件接收方式

- [x] Webhook 上报 (需要公网地址)
- [x] WebSocket 订阅 (无需公网地址，断线自动重连)

### 支持的消息元素

| 元素              | 支持情况 |
| ----------------- | -------- |
| 文本 Text         | ✅       |
| 图片 Image        | ✅       |
| 提及用户 At(user) | ✅       |
| 按钮 Buttons      | ✅       |
| 表单 Form         | ⬇️       |
| 表情包 expression | ⬇️       |
| 语音 Audio        | ⬇️       |
| 视频 Video        | ✅       |
| 文件 File         | ✅       |
| HTML HTML         | ✅       |
| 文章 Post         | ⬇️       |
| Markdown          | ✅       |
| 提示信息 Tip      | ⬇️       |
| 回复 Reply        | ✅       |

### 支持的消息操作

| 操作              | 支持情况 |
| ----------------- | -------- |
| 发送 Send         | ✅       |
| 撤回 Recall       | ✅       |
| 编辑 Edit         | ✅       |
| 表情响应 Reaction | 🚫       |

## 简介

`nonebot-adapter-yunhu` 是为 [NoneBot2](https://github.com/nonebot/nonebot2) 设计的云湖(YunHu)平台适配器，支持开发云湖机器人，提供完整的消息和服务支持。

## 安装

### 使用 pip 安装

```bash
pip install nonebot-adapter-yunhu
```

### 使用 nb-cli 安装

```bash
nb adapter install nonebot-adapter-yunhu
```

### 使用 poetry 安装

```bash
poetry add nonebot-adapter-yunhu
```

## 配置

在您的 NoneBot 项目配置文件 `.env` 中添加以下配置。适配器支持两种事件接收方式，可在同一项目中混用。

| 配置项               | 必填            | 默认值                          | 说明                                                  |
| -------------------- | --------------- | ------------------------------- | ----------------------------------------------------- |
| `token`              | 是              |                                 | 机器人 Token                                          |
| `app_id`             | 否              |                                 | 机器人 ID，留空时通过 token 自动获取                  |
| `connection_type`    | 否              | `websocket`                     | 事件接收方式：`websocket` / `webhook`                 |
| `ws_url`             | 否              | `wss://ws.jwzhd.com/subscribe`  | WebSocket 订阅地址，token 会自动拼接，一般无需修改    |
| `reconnect_interval` | 否              | `3.0`                           | WebSocket 断线重连初始间隔(秒)，失败时指数退避至 60 秒 |
| `use_stream`         | 否              | `false`                         | 是否使用流式回复(仅 text / markdown)                  |

### 方式一：WebSocket 订阅（默认，无需公网地址）

适配器会主动连接 `wss://ws.jwzhd.com/subscribe?token={token}` 接收事件，只需要填写 `token`：

```env
DRIVER=~httpx+~websockets

YUNHU_BOTS='[{
    "token": "xxx"
}]'
```

> WebSocket 模式需要支持 WebSocket 客户端的驱动器，如 `~websockets` 或 `~aiohttp`。
> 若不需要同时开启 webhook，可以不使用 `~fastapi` 等 ASGI 驱动器。

### 方式二：Webhook 上报

> [!warning]
>
> 自 WebSocket 支持起，`connection_type` 默认值为 `websocket`。旧版本使用 webhook 的配置需显式添加 `"connection_type": "webhook"`。

```env
DRIVER=~fastapi+~httpx

YUNHU_BOTS='[{
    "token": "xxx",
    "connection_type": "webhook"
}]'

HOST=0.0.0.0
PORT=8080
```

在云湖控制台，上报地址填写为 `http(s)://{HOST}:{PORT}/yunhu/{app_id}`，其中 `app_id` 为机器人 ID（启动日志会打印实际地址）。

> `app_id` 会在启动时通过 token 自动获取；若启动时无法访问云湖接口，可手动填写 `"app_id": "123456"` 跳过该步骤。

### 注册适配器

在 `bot.py` 中注册适配器：

```python
import nonebot
from nonebot.adapters.yunhu import Adapter as YunhuAdapter

nonebot.init()

driver = nonebot.get_driver()
driver.register_adapter(YunhuAdapter)
```

## 使用方法

> [!tip]
> 
> 建议使用 `nonebot-plugin-alconna` 和 `nonebot-plugin-uninfo` 等插件，来提高插件可移植性，避免直接与适配器底层交互

### 基本用法

```python
from nonebot import on_command
from nonebot.adapters.yunhu import Bot, Event

echo = on_command("echo")

@echo.handle()
async def handle_echo(bot: Bot, event: Event):
    await echo.finish(event.get_message())
```

### 发送不同类型的消息

具体类型参考 `message.py`

```python
from nonebot import on_command
from nonebot.adapters.yunhu import Bot, Event, MessageSegment

test = on_command("test")

@test.handle()
async def handle_send_image(bot: Bot, event: Event):
    # 发送文本
    await test.send(MessageSegment.text("Hello World"))
    # 发送图片, 也可以直接传参 raw=bytes 或 raw="imageKey"
    await test.send(MessageSegment.image(url="xxxxx"))
    # @某人
    await test.send(MessageSegment.at("user_id"))
```

### 其他 API

除消息收发外，`Bot` 还封装了官方开放平台的以下接口：

| 方法                                                     | 说明                                  |
| -------------------------------------------------------- | ------------------------------------- |
| `get_self_info()`                                        | 获取当前机器人信息(停用/封禁状态)     |
| `get_bot_groups(page, size)`                             | 分页获取机器人所在群                  |
| `get_group_detail(group_id)`                             | 获取群信息及机器人在群内的管理权限    |
| `get_group_members(group_id, page, size)`                | 分页获取群成员(需群内授权)            |
| `batch_send_msg(type, ids, content, content_type)`       | 批量发送消息                          |
| `get_msgs` / `get_msg` / `edit_msg` / `delete_msg`       | 消息列表 / 编辑 / 撤回                |
| `gag_member(group_id, user_id, duration)`                | 禁言(0 解除，-1 永久)                 |
| `remove_group_member(group_id, user_id)`                 | 移除群成员                            |
| `set_group_msg_type_limit(group_id, types)`              | 群内消息类型控制                      |
| `get_group_tags` / `create_group_tag` / `edit_group_tag` / `delete_group_tag` | 群标签管理       |
| `add_user_tag` / `remove_user_tag`                       | 为群成员添加 / 移除标签               |
| `set_*_board` / `dismiss_*_board`                        | 看板管理                              |

## 获取帮助

<img alt="image" width="300" src="https://github.com/user-attachments/assets/b133281f-58d2-4974-bee3-77b520b0864f" />

- 加入云湖群聊【NoneBot 云湖适配器交流群】: [链接](https://yhfx.jwznb.com/share?key=85HNqkjNINWc&ts=1762393601)
- 群 ID: 519215204

## 相关链接

- [云湖官方开放平台文档](https://www.yhchat.com/document-v2/)
- [适配器帮助文档](https://yunhu.awkchan.top/)
- [云湖第三方文档 1](https://yh-api.yyyyt.top/api/v1/msg.html#%E6%89%B9%E9%87%8F%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF)
- [云湖第三方文档 2](https://fly1919.github.io/adapter-yunhupro/markdown/dev/yunhu-official/400/7.html)
- [云湖第三方文档 3](https://www.yhchat.top/#/yunhu-bot-dev/msg-type-examples)

