Skip to main content

基于nonebot的会话活跃检测插件,只对活跃会话推送信息

Project description

logo

✨ nonebot-plugin-activity-tracker ✨

license pypi python ruff uv pre-commit

📖 介绍

一个基于 NoneBot2 的会话活跃度追踪插件,用于监测和记录用户与机器人的交互活跃度。该插件可以帮助你:

  • 🔍 实时追踪会话活跃状态
  • 📊 统计会话交互次数
  • ⏰ 记录最后活跃时间
  • 💾 支持 Redis 和内存缓存
  • 📋 提供完整的数据持久化

适用于需要根据用户活跃度进行消息推送、统计分析等场景。

💿 安装

使用 nb-cli 安装 在 nonebot2 项目的根目录下打开命令行, 输入以下指令即可安装
nb plugin install nonebot-plugin-activity-tracker --upgrade

使用 pypi 源安装

nb plugin install nonebot-plugin-activity-tracker --upgrade -i "https://pypi.org/simple"

使用清华源安装

nb plugin install nonebot-plugin-activity-tracker --upgrade -i "https://pypi.tuna.tsinghua.edu.cn/simple"
使用包管理器安装 在 nonebot2 项目的插件目录下, 打开命令行, 根据你使用的包管理器, 输入相应的安装命令
uv
uv add nonebot-plugin-activity-tracker

安装仓库 master 分支

uv add git+https://github.com/zifox666/nonebot-plugin-activity-tracker@master
pdm
pdm add nonebot-plugin-activity-tracker

安装仓库 master 分支

pdm add git+https://github.com/zifox666/nonebot-plugin-activity-tracker@master
poetry
poetry add nonebot-plugin-activity-tracker

安装仓库 master 分支

poetry add git+https://github.com/zifox666/nonebot-plugin-activity-tracker@master

打开 nonebot2 项目根目录下的 pyproject.toml 文件, 在 [tool.nonebot] 部分追加写入

plugins = ["nonebot_plugin_activity_tracker"]
使用 nbr 安装(使用 uv 管理依赖可用)

nbr 是一个基于 uv 的 nb-cli,可以方便地管理 nonebot2

nbr plugin install nonebot-plugin-activity-tracker

使用 pypi 源安装

nbr plugin install nonebot-plugin-activity-tracker -i "https://pypi.org/simple"

⚙️ 配置

在 nonebot2 项目的.env文件中添加下表中的配置

配置项 必填 默认值 说明
DEFAULT_ACTIVE_DAYS 7 默认活跃天(缓存天数)
REDIS_HOST Redis 主机(留空使用内存缓存)
REDIS_PORT 6379 Redis 端口(HOST为空时无效)

配置示例

DEFAULT_ACTIVE_DAYS=7

🎉 使用

检查会话活跃状态

from nonebot_plugin_uninfo import Uninfo
from nonebot_plugin_activity_tracker import session_active

@matcher.handle()
async def handle(uninfo: Uninfo):
    activity_data = await session_active(uninfo.adapter.name, uninfo.scene.type.name, uninfo.scene.id)
    if activity_data:
        await matcher.send(activity_data)
    else:
        await matcher.send("会话未活跃")

使用依赖注入获取活跃数据

from nonebot.params import Depends
from nonebot_plugin_activity_tracker import get_session_activity, ActivityData

@matcher.handle()
async def handle(activity_data: ActivityData = Depends(get_session_activity)):
    if activity_data:
        await matcher.send(activity_data)
    else:
        await matcher.send("当前会话无活跃数据")

获取所有活跃会话

from nonebot_plugin_activity_tracker import get_all_active_sessions

@matcher.handle()
async def handle():
    sessions = await get_all_active_sessions()
    count = len(sessions)
    await matcher.send(f"当前活跃会话数: {count}\n{sessions}")

🔧 API 参考

ActivityData 模型

属性 类型 含义
adapter str 适配器名称
scene_type str 场景类型
scene_id str 场景 ID
count int 活跃次数
last_session_activity int 最后会话活跃时间
last_bot_activity datetime | None 最后机器人活跃时间

主要函数

函数名 描述 参数 返回值
query_session_active 检查指定会话活跃状态 adapter: str, scene_type: str, scene_id: str Optional[ActivityData]
get_all_active_sessions 获取所有活跃会话 List[ActivityData]
get_session_activity 获取当前会话活跃状态 uninfo: Uninfo Optional[ActivityData]
record_active_session 记录用户活跃 自动触发
record_bot_active_session 记录机器人活跃 自动触发

Project details


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_activity_tracker-0.1.1.tar.gz (8.5 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_activity_tracker-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_activity_tracker-0.1.1.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_activity_tracker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 65238c8eb6fc3bddda80d7b8d53bfdd2048af133c4471ec71862a711c4e4f44e
MD5 a5ea85f076e5c2565fb024c19ab2971a
BLAKE2b-256 afa5e5f90b22ccae8b200730e1cb7a2c64fa94f0a074c1823ed0622486c700e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_activity_tracker-0.1.1.tar.gz:

Publisher: release.yml on zifox666/nonebot-plugin-activity-tracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_activity_tracker-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_activity_tracker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7e45b13765a93611b6d3a0bfc286b4d63f8587d9df06ef1892fe6f07735eb7c
MD5 9f75b1561905f1ff7942f40166a6f5cb
BLAKE2b-256 49e8a240981c146463b7f07cb05cd47036e6ea2a95a9fb0d195472f3ad1066cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_activity_tracker-0.1.1-py3-none-any.whl:

Publisher: release.yml on zifox666/nonebot-plugin-activity-tracker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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