<div align="center">
<a href="https://nonebot.dev/">
<img src="https://nonebot.dev/logo.png" width="200" height="200" alt="nonebot">
</a>
# nonebot-plugin-chatrecorder
_✨ [Nonebot2](https://github.com/nonebot/nonebot2) 聊天记录插件 ✨_
<p align="center">
<img src="https://img.shields.io/github/license/noneplugin/nonebot-plugin-chatrecorder" alt="license">
<img src="https://img.shields.io/badge/python-3.8+-blue.svg" alt="Python">
<img src="https://img.shields.io/badge/nonebot-2.2.0+-red.svg" alt="NoneBot">
<a href="https://pypi.org/project/nonebot-plugin-chatrecorder">
<img src="https://badgen.net/pypi/v/nonebot-plugin-chatrecorder" alt="pypi">
</a>
</p>
</div>
将聊天消息存至数据库中,方便其他插件使用。
### 安装
- 使用 nb-cli
```shell
nb plugin install nonebot_plugin_chatrecorder
```
- 使用 pip
```shell
pip install nonebot_plugin_chatrecorder
```
### 配置项
> 以下配置项可在 `.env.*` 文件中设置,具体参考 [NoneBot 配置方式](https://nonebot.dev/docs/appendices/config)
#### `chatrecorder_record_send_msg`
- 类型:`bool`
- 默认:`True`
- 说明:是否记录机器人自己发出的消息
### 使用
其他插件可使用本插件提供的接口获取消息记录
先在插件代码最前面声明依赖:
```python
from nonebot import require
require("nonebot_plugin_chatrecorder")
```
使用示例:
> [!NOTE]
>
> 插件依赖 [nonebot-plugin-session](https://github.com/noneplugin/nonebot-plugin-session) 插件来获取会话相关信息
>
> 会话相关字段如 `id1`、`id2`、`id3` 可以查看 `nonebot-plugin-session` 插件中的说明
- 获取当前群内成员 "12345" 和 "54321" 1天之内的消息记录
```python
from nonebot.adapters.onebot.v11 import GroupMessageEvent
from nonebot_plugin_chatrecorder import get_message_records
@matcher.handle()
async def _(event: GroupMessageEvent):
records = await get_message_records(
id1s=["12345", "54321"],
id2s=[str(event.group_id)],
time_start=datetime.utcnow() - timedelta(days=1),
)
```
> [!NOTE]
>
> `time_start` 和 `time_stop` 参数 传入的 `datetime` 对象必须为 [感知型对象](https://docs.python.org/zh-cn/3/library/datetime.html#determining-if-an-object-is-aware-or-naive)(即包含时区信息),或者确保其为 UTC 时间
- 获取当前会话成员 1 天之内的消息记录
```python
from nonebot_plugin_session import extract_session, SessionIdType
from nonebot_plugin_chatrecorder import get_message_records
@matcher.handle()
async def _(bot: Bot, event: Event):
session = extract_session(bot, event)
records = await get_message_records(
session=session,
time_start=datetime.utcnow() - timedelta(days=1),
)
```
> [!NOTE]
>
> 可以传入 [nonebot-plugin-session](https://github.com/noneplugin/nonebot-plugin-session) 插件获取的 `Session` 对象来筛选消息记录
>
> 传入 `Session` 时可以通过 `id_type` 来控制要筛选的会话级别
- 获取当前 群聊/私聊 除机器人发出的消息外,其他消息的纯本文形式
```python
from nonebot_plugin_session import extract_session, SessionIdType
from nonebot_plugin_chatrecorder import get_messages_plain_text
@matcher.handle()
async def _(bot: Bot, event: Event):
session = extract_session(bot, event)
msgs = await get_messages_plain_text(
session=session,
id_type=SessionIdType.GROUP,
types=["message"],
)
```
详细参数及说明见代码注释
### 旧版本聊天记录迁移
#### `0.1.x` -> `0.2.x`
从 `0.1.x` 版本升级到 `0.2.x` 版本时,需要添加如下配置项以完成迁移
> #### `chatrecorder_record_migration_bot_id`
>
> - 类型:`Optional[str]`
> - 默认:`None`
> - 说明:在旧版本(0.1.x) 时使用的机器人账号(机器人qq号),用于数据库迁移;若使用过此插件的旧版本则必须配置,数据库迁移完成后可删除;未使用过旧版本可不配置
#### `0.2.x` -> `0.3.x`
从 `0.2.x` 版本升级到 `0.3.x` ~ `0.4.x` 版本时,会自动运行迁移脚本,或运行 `nb datastore upgrade` 进行迁移
若聊天记录很多,迁移可能会花费较长时间,在迁移过程中不要关闭程序
#### `0.4.x` -> `0.5.x`
从 `0.4.x` 版本升级到 `0.5.x` 版本时,插件数据库依赖由 [nonebot-plugin-datastore](https://github.com/he0119/nonebot-plugin-datastore) 迁移至 [nonebot-plugin-orm](https://github.com/nonebot/plugin-orm)
要迁移聊天记录,需要同时安装 `nonebot-plugin-datastore` 和 `nonebot-plugin-orm`,运行 `nb orm upgrade` 进行迁移
若聊天记录很多,迁移可能会花费较长时间,在迁移过程中不要关闭程序
#### `0.2.x` -> `0.5.x`
若要从 `0.2.x` 版本直接升级到 `0.5.x`,需要先升级到 `0.4.x` 版本,运行 `nb datastore upgrade` 完成迁移后,再继续升级
### 其他说明
> [!NOTE]
>
> 由于在 OneBot V11 适配器中,机器人**发送的消息**中可能存在 base64 形式的图片、语音等,
>
> 为避免消息记录文件体积过大,本插件会将 base64 形式的图片、语音等存成文件,并在消息记录中以文件路径替代。
>
> 这些文件会放置在 [nonebot-plugin-localstore](https://github.com/nonebot/plugin-localstore) 插件设置的缓存目录,**建议定期清理**
### 支持的 adapter
- [x] OneBot v11
- [x] OneBot v12
- [x] Console
- [x] Kaiheila
- [x] Telegram
- [x] Feishu
- [x] RedProtocol
- [x] Discord
- [x] DoDo
- [x] Satori
- [x] QQ
### 鸣谢
- [nonebot-plugin-send-anything-anywhere](https://github.com/felinae98/nonebot-plugin-send-anything-anywhere) 项目的灵感来源以及部分实现的参考
- [uy/sun](https://github.com/he0119) 感谢歪日佬的技术支持
Raw data
{
"_id": null,
"home_page": "https://github.com/noneplugin/nonebot-plugin-chatrecorder",
"name": "nonebot_plugin_chatrecorder",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "meetwq",
"author_email": "meetwq@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ef/7b/c84b296b73a1c5ea38ea2d1d4e282539dd2be8edc4a6f2cefa06174dbc22/nonebot_plugin_chatrecorder-0.6.0.tar.gz",
"platform": null,
"description": "<div align=\"center\">\n\n <a href=\"https://nonebot.dev/\">\n <img src=\"https://nonebot.dev/logo.png\" width=\"200\" height=\"200\" alt=\"nonebot\">\n </a>\n\n# nonebot-plugin-chatrecorder\n\n_\u2728 [Nonebot2](https://github.com/nonebot/nonebot2) \u804a\u5929\u8bb0\u5f55\u63d2\u4ef6 \u2728_\n\n<p align=\"center\">\n <img src=\"https://img.shields.io/github/license/noneplugin/nonebot-plugin-chatrecorder\" alt=\"license\">\n <img src=\"https://img.shields.io/badge/python-3.8+-blue.svg\" alt=\"Python\">\n <img src=\"https://img.shields.io/badge/nonebot-2.2.0+-red.svg\" alt=\"NoneBot\">\n <a href=\"https://pypi.org/project/nonebot-plugin-chatrecorder\">\n <img src=\"https://badgen.net/pypi/v/nonebot-plugin-chatrecorder\" alt=\"pypi\">\n </a>\n</p>\n\n</div>\n\n\u5c06\u804a\u5929\u6d88\u606f\u5b58\u81f3\u6570\u636e\u5e93\u4e2d\uff0c\u65b9\u4fbf\u5176\u4ed6\u63d2\u4ef6\u4f7f\u7528\u3002\n\n### \u5b89\u88c5\n\n- \u4f7f\u7528 nb-cli\n\n```shell\nnb plugin install nonebot_plugin_chatrecorder\n```\n\n- \u4f7f\u7528 pip\n\n```shell\npip install nonebot_plugin_chatrecorder\n```\n\n### \u914d\u7f6e\u9879\n\n> \u4ee5\u4e0b\u914d\u7f6e\u9879\u53ef\u5728 `.env.*` \u6587\u4ef6\u4e2d\u8bbe\u7f6e\uff0c\u5177\u4f53\u53c2\u8003 [NoneBot \u914d\u7f6e\u65b9\u5f0f](https://nonebot.dev/docs/appendices/config)\n\n#### `chatrecorder_record_send_msg`\n\n- \u7c7b\u578b\uff1a`bool`\n- \u9ed8\u8ba4\uff1a`True`\n- \u8bf4\u660e\uff1a\u662f\u5426\u8bb0\u5f55\u673a\u5668\u4eba\u81ea\u5df1\u53d1\u51fa\u7684\u6d88\u606f\n\n### \u4f7f\u7528\n\n\u5176\u4ed6\u63d2\u4ef6\u53ef\u4f7f\u7528\u672c\u63d2\u4ef6\u63d0\u4f9b\u7684\u63a5\u53e3\u83b7\u53d6\u6d88\u606f\u8bb0\u5f55\n\n\u5148\u5728\u63d2\u4ef6\u4ee3\u7801\u6700\u524d\u9762\u58f0\u660e\u4f9d\u8d56\uff1a\n\n```python\nfrom nonebot import require\nrequire(\"nonebot_plugin_chatrecorder\")\n```\n\n\u4f7f\u7528\u793a\u4f8b\uff1a\n\n> [!NOTE]\n>\n> \u63d2\u4ef6\u4f9d\u8d56 [nonebot-plugin-session](https://github.com/noneplugin/nonebot-plugin-session) \u63d2\u4ef6\u6765\u83b7\u53d6\u4f1a\u8bdd\u76f8\u5173\u4fe1\u606f\n>\n> \u4f1a\u8bdd\u76f8\u5173\u5b57\u6bb5\u5982 `id1`\u3001`id2`\u3001`id3` \u53ef\u4ee5\u67e5\u770b `nonebot-plugin-session` \u63d2\u4ef6\u4e2d\u7684\u8bf4\u660e\n\n- \u83b7\u53d6\u5f53\u524d\u7fa4\u5185\u6210\u5458 \"12345\" \u548c \"54321\" 1\u5929\u4e4b\u5185\u7684\u6d88\u606f\u8bb0\u5f55\n\n```python\nfrom nonebot.adapters.onebot.v11 import GroupMessageEvent\nfrom nonebot_plugin_chatrecorder import get_message_records\n\n@matcher.handle()\nasync def _(event: GroupMessageEvent):\n records = await get_message_records(\n id1s=[\"12345\", \"54321\"],\n id2s=[str(event.group_id)],\n time_start=datetime.utcnow() - timedelta(days=1),\n )\n```\n\n> [!NOTE]\n>\n> `time_start` \u548c `time_stop` \u53c2\u6570 \u4f20\u5165\u7684 `datetime` \u5bf9\u8c61\u5fc5\u987b\u4e3a [\u611f\u77e5\u578b\u5bf9\u8c61](https://docs.python.org/zh-cn/3/library/datetime.html#determining-if-an-object-is-aware-or-naive)\uff08\u5373\u5305\u542b\u65f6\u533a\u4fe1\u606f\uff09\uff0c\u6216\u8005\u786e\u4fdd\u5176\u4e3a UTC \u65f6\u95f4\n\n- \u83b7\u53d6\u5f53\u524d\u4f1a\u8bdd\u6210\u5458 1 \u5929\u4e4b\u5185\u7684\u6d88\u606f\u8bb0\u5f55\n\n```python\nfrom nonebot_plugin_session import extract_session, SessionIdType\nfrom nonebot_plugin_chatrecorder import get_message_records\n\n@matcher.handle()\nasync def _(bot: Bot, event: Event):\n session = extract_session(bot, event)\n records = await get_message_records(\n session=session,\n time_start=datetime.utcnow() - timedelta(days=1),\n )\n```\n\n> [!NOTE]\n>\n> \u53ef\u4ee5\u4f20\u5165 [nonebot-plugin-session](https://github.com/noneplugin/nonebot-plugin-session) \u63d2\u4ef6\u83b7\u53d6\u7684 `Session` \u5bf9\u8c61\u6765\u7b5b\u9009\u6d88\u606f\u8bb0\u5f55\n>\n> \u4f20\u5165 `Session` \u65f6\u53ef\u4ee5\u901a\u8fc7 `id_type` \u6765\u63a7\u5236\u8981\u7b5b\u9009\u7684\u4f1a\u8bdd\u7ea7\u522b\n\n- \u83b7\u53d6\u5f53\u524d \u7fa4\u804a/\u79c1\u804a \u9664\u673a\u5668\u4eba\u53d1\u51fa\u7684\u6d88\u606f\u5916\uff0c\u5176\u4ed6\u6d88\u606f\u7684\u7eaf\u672c\u6587\u5f62\u5f0f\n\n```python\nfrom nonebot_plugin_session import extract_session, SessionIdType\nfrom nonebot_plugin_chatrecorder import get_messages_plain_text\n\n@matcher.handle()\nasync def _(bot: Bot, event: Event):\n session = extract_session(bot, event)\n msgs = await get_messages_plain_text(\n session=session,\n id_type=SessionIdType.GROUP,\n types=[\"message\"],\n )\n```\n\n\u8be6\u7ec6\u53c2\u6570\u53ca\u8bf4\u660e\u89c1\u4ee3\u7801\u6ce8\u91ca\n\n### \u65e7\u7248\u672c\u804a\u5929\u8bb0\u5f55\u8fc1\u79fb\n\n#### `0.1.x` -> `0.2.x`\n\n\u4ece `0.1.x` \u7248\u672c\u5347\u7ea7\u5230 `0.2.x` \u7248\u672c\u65f6\uff0c\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\u9879\u4ee5\u5b8c\u6210\u8fc1\u79fb\n\n> #### `chatrecorder_record_migration_bot_id`\n>\n> - \u7c7b\u578b\uff1a`Optional[str]`\n> - \u9ed8\u8ba4\uff1a`None`\n> - \u8bf4\u660e\uff1a\u5728\u65e7\u7248\u672c(0.1.x) \u65f6\u4f7f\u7528\u7684\u673a\u5668\u4eba\u8d26\u53f7(\u673a\u5668\u4ebaqq\u53f7)\uff0c\u7528\u4e8e\u6570\u636e\u5e93\u8fc1\u79fb\uff1b\u82e5\u4f7f\u7528\u8fc7\u6b64\u63d2\u4ef6\u7684\u65e7\u7248\u672c\u5219\u5fc5\u987b\u914d\u7f6e\uff0c\u6570\u636e\u5e93\u8fc1\u79fb\u5b8c\u6210\u540e\u53ef\u5220\u9664\uff1b\u672a\u4f7f\u7528\u8fc7\u65e7\u7248\u672c\u53ef\u4e0d\u914d\u7f6e\n\n#### `0.2.x` -> `0.3.x`\n\n\u4ece `0.2.x` \u7248\u672c\u5347\u7ea7\u5230 `0.3.x` ~ `0.4.x` \u7248\u672c\u65f6\uff0c\u4f1a\u81ea\u52a8\u8fd0\u884c\u8fc1\u79fb\u811a\u672c\uff0c\u6216\u8fd0\u884c `nb datastore upgrade` \u8fdb\u884c\u8fc1\u79fb\n\n\u82e5\u804a\u5929\u8bb0\u5f55\u5f88\u591a\uff0c\u8fc1\u79fb\u53ef\u80fd\u4f1a\u82b1\u8d39\u8f83\u957f\u65f6\u95f4\uff0c\u5728\u8fc1\u79fb\u8fc7\u7a0b\u4e2d\u4e0d\u8981\u5173\u95ed\u7a0b\u5e8f\n\n#### `0.4.x` -> `0.5.x`\n\n\u4ece `0.4.x` \u7248\u672c\u5347\u7ea7\u5230 `0.5.x` \u7248\u672c\u65f6\uff0c\u63d2\u4ef6\u6570\u636e\u5e93\u4f9d\u8d56\u7531 [nonebot-plugin-datastore](https://github.com/he0119/nonebot-plugin-datastore) \u8fc1\u79fb\u81f3 [nonebot-plugin-orm](https://github.com/nonebot/plugin-orm)\n\n\u8981\u8fc1\u79fb\u804a\u5929\u8bb0\u5f55\uff0c\u9700\u8981\u540c\u65f6\u5b89\u88c5 `nonebot-plugin-datastore` \u548c `nonebot-plugin-orm`\uff0c\u8fd0\u884c `nb orm upgrade` \u8fdb\u884c\u8fc1\u79fb\n\n\u82e5\u804a\u5929\u8bb0\u5f55\u5f88\u591a\uff0c\u8fc1\u79fb\u53ef\u80fd\u4f1a\u82b1\u8d39\u8f83\u957f\u65f6\u95f4\uff0c\u5728\u8fc1\u79fb\u8fc7\u7a0b\u4e2d\u4e0d\u8981\u5173\u95ed\u7a0b\u5e8f\n\n#### `0.2.x` -> `0.5.x`\n\n\u82e5\u8981\u4ece `0.2.x` \u7248\u672c\u76f4\u63a5\u5347\u7ea7\u5230 `0.5.x`\uff0c\u9700\u8981\u5148\u5347\u7ea7\u5230 `0.4.x` \u7248\u672c\uff0c\u8fd0\u884c `nb datastore upgrade` \u5b8c\u6210\u8fc1\u79fb\u540e\uff0c\u518d\u7ee7\u7eed\u5347\u7ea7\n\n### \u5176\u4ed6\u8bf4\u660e\n\n> [!NOTE]\n>\n> \u7531\u4e8e\u5728 OneBot V11 \u9002\u914d\u5668\u4e2d\uff0c\u673a\u5668\u4eba**\u53d1\u9001\u7684\u6d88\u606f**\u4e2d\u53ef\u80fd\u5b58\u5728 base64 \u5f62\u5f0f\u7684\u56fe\u7247\u3001\u8bed\u97f3\u7b49\uff0c\n>\n> \u4e3a\u907f\u514d\u6d88\u606f\u8bb0\u5f55\u6587\u4ef6\u4f53\u79ef\u8fc7\u5927\uff0c\u672c\u63d2\u4ef6\u4f1a\u5c06 base64 \u5f62\u5f0f\u7684\u56fe\u7247\u3001\u8bed\u97f3\u7b49\u5b58\u6210\u6587\u4ef6\uff0c\u5e76\u5728\u6d88\u606f\u8bb0\u5f55\u4e2d\u4ee5\u6587\u4ef6\u8def\u5f84\u66ff\u4ee3\u3002\n>\n> \u8fd9\u4e9b\u6587\u4ef6\u4f1a\u653e\u7f6e\u5728 [nonebot-plugin-localstore](https://github.com/nonebot/plugin-localstore) \u63d2\u4ef6\u8bbe\u7f6e\u7684\u7f13\u5b58\u76ee\u5f55\uff0c**\u5efa\u8bae\u5b9a\u671f\u6e05\u7406**\n\n### \u652f\u6301\u7684 adapter\n\n- [x] OneBot v11\n- [x] OneBot v12\n- [x] Console\n- [x] Kaiheila\n- [x] Telegram\n- [x] Feishu\n- [x] RedProtocol\n- [x] Discord\n- [x] DoDo\n- [x] Satori\n- [x] QQ\n\n### \u9e23\u8c22\n\n- [nonebot-plugin-send-anything-anywhere](https://github.com/felinae98/nonebot-plugin-send-anything-anywhere) \u9879\u76ee\u7684\u7075\u611f\u6765\u6e90\u4ee5\u53ca\u90e8\u5206\u5b9e\u73b0\u7684\u53c2\u8003\n- [uy/sun](https://github.com/he0119) \u611f\u8c22\u6b6a\u65e5\u4f6c\u7684\u6280\u672f\u652f\u6301\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "\u9002\u7528\u4e8e Nonebot2 \u7684\u804a\u5929\u8bb0\u5f55\u63d2\u4ef6",
"version": "0.6.0",
"project_urls": {
"Homepage": "https://github.com/noneplugin/nonebot-plugin-chatrecorder",
"Repository": "https://github.com/noneplugin/nonebot-plugin-chatrecorder"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4d36c596ae6a5c3c861ec85b26f5cc596866cf030b466c532dcbb4325309375c",
"md5": "fea472a77ac8130f0d5e2d37d3c75603",
"sha256": "855a5a445da9c5c4247499cba55bb4c1c10de5cf024d1af1f756cf4d3a84fa82"
},
"downloads": -1,
"filename": "nonebot_plugin_chatrecorder-0.6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fea472a77ac8130f0d5e2d37d3c75603",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 29420,
"upload_time": "2024-03-01T02:09:06",
"upload_time_iso_8601": "2024-03-01T02:09:06.819616Z",
"url": "https://files.pythonhosted.org/packages/4d/36/c596ae6a5c3c861ec85b26f5cc596866cf030b466c532dcbb4325309375c/nonebot_plugin_chatrecorder-0.6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ef7bc84b296b73a1c5ea38ea2d1d4e282539dd2be8edc4a6f2cefa06174dbc22",
"md5": "fecfe813a84b8de91b1ee9d94e657004",
"sha256": "a80acbbfe2388a60177effc62857fa09154a38871643e14c7eb7352226c03138"
},
"downloads": -1,
"filename": "nonebot_plugin_chatrecorder-0.6.0.tar.gz",
"has_sig": false,
"md5_digest": "fecfe813a84b8de91b1ee9d94e657004",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 17598,
"upload_time": "2024-03-01T02:09:08",
"upload_time_iso_8601": "2024-03-01T02:09:08.564664Z",
"url": "https://files.pythonhosted.org/packages/ef/7b/c84b296b73a1c5ea38ea2d1d4e282539dd2be8edc4a6f2cefa06174dbc22/nonebot_plugin_chatrecorder-0.6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-01 02:09:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "noneplugin",
"github_project": "nonebot-plugin-chatrecorder",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "nonebot_plugin_chatrecorder"
}