将聊天消息存至数据库中,方便其他插件使用。
安装
- 使用 nb-cli
nb plugin install nonebot_plugin_chatrecorder
- 使用 pip
pip install nonebot_plugin_chatrecorder
配置项
以下配置项可在
.env.*文件中设置,具体参考 NoneBot 配置方式
chatrecorder_record_send_msg
- 类型:
bool - 默认:
True - 说明:是否记录机器人自己发出的消息
使用
其他插件可使用本插件提供的接口获取消息记录
先在插件代码最前面声明依赖:
from nonebot import require
require("nonebot_plugin_chatrecorder")
使用示例:
[!NOTE]
插件依赖 nonebot-plugin-uninfo 插件来获取会话相关信息
会话相关字段如
scene_id、scene_type、scope可以查看nonebot-plugin-uninfo插件中的说明
- 获取当前群内成员 "12345" 和 "54321" 1天之内的消息记录
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(
user_ids=["12345", "54321"],
scene_ids=[str(event.group_id)],
time_start=datetime.utcnow() - timedelta(days=1),
)
[!NOTE]
time_start和time_stop参数 传入的datetime对象必须为 感知型对象(即包含时区信息),或者确保其为 UTC 时间
- 获取当前会话成员 1 天之内的消息记录
from nonebot_plugin_uninfo import Uninfo
from nonebot_plugin_chatrecorder import get_message_records
@matcher.handle()
async def _(session: Uninfo):
records = await get_message_records(
session=session,
time_start=datetime.utcnow() - timedelta(days=1),
)
[!NOTE]
可以传入 nonebot-plugin-uninfo 插件获取的
Session对象来筛选消息记录传入
Session时可以通过filter_scene、filter_user等选项来控制要筛选的会话级别
- 获取当前 群聊/私聊 除机器人发出的消息外,其他消息的纯本文形式
from nonebot_plugin_uninfo import Uninfo
from nonebot_plugin_chatrecorder import get_messages_plain_text
@matcher.handle()
async def _(session: Uninfo):
msgs = await get_messages_plain_text(
session=session,
filter_scene=True,
filter_user=False,
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 迁移至 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 完成迁移后,再继续升级
0.6.x -> 0.7.x
0.7.x 版本会话信息获取插件从 nonebot-plugin-session 替换为 nonebot-plugin-uninfo
需安装 nonebot-session-to-uninfo(迁移完成后可删除),并运行 nb datastore upgrade 进行聊天记录迁移
若聊天记录很多,迁移可能会花费较长时间,在迁移过程中不要关闭程序
其他说明
[!NOTE]
由于在 OneBot V11 适配器中,机器人发送的消息中可能存在 base64 形式的图片、语音等,
为避免消息记录文件体积过大,本插件会将 base64 形式的图片、语音等存成文件,并在消息记录中以文件路径替代。
这些文件会放置在 nonebot-plugin-localstore 插件设置的缓存目录,建议定期清理
支持的 adapter
- OneBot v11
- OneBot v12
- Console
- Kaiheila
- Telegram
- Feishu
- Discord
- DoDo
- Satori
鸣谢
- nonebot-plugin-send-anything-anywhere 项目的灵感来源以及部分实现的参考
- uy/sun 感谢歪日佬的技术支持
Metadata
Release files for nonebot_plugin_chatrecorder 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nonebot_plugin_chatrecorder-0.7.0.tar.gz | 17.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.6 kB
Release files / nonebot_plugin_chatrecorder-0.7.0.tar.gz
| Download URL | nonebot_plugin_chatrecorder-0.7.0.tar.gz |
|---|---|
| Size | 17.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b63a92733307c4cd739bb1bb6d2af949811ff6882ce49e73d42735cfd6e820d
|
|
BLAKE2b-256 checksum How to use checksums |
d06d77fd9f23c20a84118cc26223c309c895ae808099f672f3cf2cbcff89ef29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.11.0 Linux/6.5.0-1025-azure
|
Release files / nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl
| Download URL | nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl |
|---|---|
| Size | 28.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56cfb306cc85c602c8c97e7f1a63328d075ac8fd2349ddefcb501600f416d544
|
|
BLAKE2b-256 checksum How to use checksums |
fb49f6529eec6b57d4e0eefc289ae2d2fa7237645effd66ed2767f152ebca6ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.11.0 Linux/6.5.0-1025-azure
|