Skip to main content
nonebot

nonebot-plugin-session

✨ Nonebot2 会话信息提取与会话 id 定义插件 ✨

license Python NoneBot pypi

本插件提供了一个统一的会话模型 Session,可以从不同适配器的 Bot 和 Event 中提取与会话相关的属性

具体定义如下:

属性 类型 含义 备注
bot_id str 机器人 id
bot_type str 机器人类型(适配器名称)
platform str 平台 未知平台用 unknown 表示
level IntEnum 会话等级 目前分为 LEVEL0(无用户)、LEVEL1(单用户)、LEVEL2(单级群组)、LEVEL3(两级群组) 四类
id1 str 1 级 id 通常为 user_id
id2 str 2 级 id 通常为 单级群组中的 group_id,两级群组中的 channel_id
id3 str 3 级 id 通常为 两级群组中的 guild_id

同时,本插件提供了获取会话 id 的函数,可以按照不同的类型获取会话id,方便不同场景下的使用

Nonebot 适配器基类中也提供了 get_session_id 函数,但通常是 用户 id、群组 id 的组合,属于 “用户级别” 的 id,很多插件中需要用到不同级别的会话 id,如词云、词库等等

安装

  • 使用 nb-cli
nb plugin install nonebot_plugin_session
  • 使用 pip
pip install nonebot_plugin_session

使用

获取 Session:

from nonebot_plugin_session import extract_session

@matcher.handle()
async def handle(bot: Bot, event: Event):
    session = extract_session(bot, event)

或使用依赖注入的形式:

from nonebot_plugin_session import EventSession

@matcher.handle()
async def handle(session: EventSession):
    ...

获取 session id:

from nonebot_plugin_session import extract_session, SessionIdType

@matcher.handle()
async def handle(bot: Bot, event: Event):
    session = extract_session(bot, event)
    session_id = session.get_id(SessionIdType.GROUP)  # 获取 “群组级别” 的 session id

或使用依赖注入的形式:

from nonebot_plugin_session import SessionId, SessionIdType

@matcher.handle()
async def handle(session_id: str = SessionId(SessionIdType.GROUP)):
    ...

不同的 “会话级别” 与 “会话id类型” 下返回的 id 如下表所示:(不包含 bot_id 等属性的情况)

LEVEL0
(无用户)
LEVEL1
(单用户)
LEVEL2
(单级群组)
LEVEL3
(两级群组)
TYPE0 (GLOBAL) "" "" "" ""
TYPE1 (USER) "" "id1" "id1" "id1"
TYPE2 "" "id1" "id2" "id2"
TYPE3 "" "id1" "id2_id1" "id2_id1"
TYPE4 "" "id1" "id2" "id3"
TYPE5 "" "id1" "id2_id1" "id3_id1"
TYPE6 (GROUP) "" "id1" "id2" "id3_id2"
TYPE7 (GROUP_USER) "" "id1" "id2_id1" "id3_id2_id1"

支持的 adapter

  • OneBot v11
  • OneBot v12
  • Console
  • Kaiheila
  • Telegram
  • Feishu
  • RedProtocol
  • Discord
  • QQ
  • Satori
  • DoDo

相关插件

鸣谢

Metadata

Release files for nonebot_plugin_session 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonebot_plugin_session 0.3.2
File Size Uploaded
nonebot_plugin_session-0.3.2.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nonebot_plugin_session 0.3.2
File Interpreter ABI Platform
nonebot_plugin_session-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.2 kB

Release files / nonebot_plugin_session-0.3.2.tar.gz

Download URL nonebot_plugin_session-0.3.2.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
da0dabe9108151052a6e83d9923068d2ec43a93810ad37b264ed65aee2c69f93
BLAKE2b-256 checksum
How to use checksums
091a95a465074f566a1552c00a0045d9e67fea41401eaf6af3f7c88b644da0f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.0 Linux/6.5.0-1025-azure

Release files / nonebot_plugin_session-0.3.2-py3-none-any.whl

Download URL nonebot_plugin_session-0.3.2-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
785e74ff656e46d84c4dbbac125f9a9adebcc4b9ff7db700527e6e47d47aabd6
BLAKE2b-256 checksum
How to use checksums
b28107c38c920dc6bab000a12d79af78482dcf0695f8a6e0b7b85fd993ef7483
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.0 Linux/6.5.0-1025-azure
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page