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 记录机器人活跃 自动触发

📝 示例

from nonebot import on_command
from nonebot.params import Depends
from nonebot_plugin_uninfo import Uninfo
from nonebot_plugin_activity_tracker import (
    session_active, 
    get_last_active_time, 
    get_all_active_sessions,
    ActivityData
)

# 检查当前会话活跃度
check_active = on_command("活跃度", priority=1)

@check_active.handle()
async def handle_check(uninfo: Uninfo):
    activity_data = await session_active(uninfo)
    if activity_data:
        last_time = await get_last_active_time(uninfo)
        await check_active.send(
            f"会话活跃度: {activity_data.count}\n"
            f"最后活跃: {last_time.strftime('%Y-%m-%d %H:%M:%S') if last_time else '未知'}"
        )
    else:
        await check_active.send("当前会话暂无活跃记录")

# 获取所有活跃会话统计
stats = on_command("活跃统计", priority=1)

@stats.handle()
async def handle_stats():
    sessions = await get_all_active_sessions()
    total_count = sum(session.count for session in sessions)
    await stats.send(
        f"活跃会话数: {len(sessions)}\n"
        f"总交互次数: {total_count}"
    )

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.0.tar.gz (8.9 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.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for nonebot_plugin_activity_tracker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5f1e3940d889c51d82072c8dcb5f6b039c0c20b6bebcd3f806e4b5031e6003a4
MD5 230b9cef9c2fa5d21c38f116387886d9
BLAKE2b-256 4173bd80de6f98259530b7fbaeaeda8dd85d30d0ac62980e13e44522dfdcc34f

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_activity_tracker-0.1.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_activity_tracker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ce35bbe6fc543036be92296663fe77e65657b3b065a8061ed54f72089639023
MD5 f1e99e7e8effdf2a01523108cff85da6
BLAKE2b-256 ded89c0db5dbe1ad35bf04395abeb289a421addafe662a33f16f7b0c0c24926a

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_activity_tracker-0.1.0-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