Skip to main content
nonebot

nonebot-plugin-chatrecorder

Nonebot2 聊天记录插件 ✨

license Python NoneBot pypi

将聊天消息存至数据库中,方便其他插件使用。

安装

  • 使用 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_idscene_typescope 可以查看 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_starttime_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_scenefilter_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-datastorenonebot-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
  • QQ

鸣谢

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_chatrecorder-0.7.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_chatrecorder-0.7.0.tar.gz.

File metadata

  • Download URL: nonebot_plugin_chatrecorder-0.7.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.0 Linux/6.5.0-1025-azure

File hashes

Hashes for nonebot_plugin_chatrecorder-0.7.0.tar.gz
Algorithm Hash digest
SHA256 5b63a92733307c4cd739bb1bb6d2af949811ff6882ce49e73d42735cfd6e820d
MD5 5f776b87a70f80d99aec49e01df76faf
BLAKE2b-256 d06d77fd9f23c20a84118cc26223c309c895ae808099f672f3cf2cbcff89ef29

See more details on using hashes here.

File details

Details for the file nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_chatrecorder-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56cfb306cc85c602c8c97e7f1a63328d075ac8fd2349ddefcb501600f416d544
MD5 b31797660f27b52ad5e6f418764d7a0a
BLAKE2b-256 fb49f6529eec6b57d4e0eefc289ae2d2fa7237645effd66ed2767f152ebca6ab

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page