Skip to main content

Nonebot2 会话目标提取与会话id定义

Project description

nonebot

nonebot-plugin-session

Nonebot2 会话目标提取与会话 id 定义插件 ✨

license Python NoneBot pypi

  • 这个插件可以做什么?

本插件提供了一个统一的会话模型 Session, 可以从不同适配器的 BotEvent 中提取与会话相关的 “平台”、“会话等级”(单用户、单级群组、两级群组)、“目标 id” 等属性;

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

  • 这个插件解决了什么问题?

Nonebot 适配器基类中提供了 get_session_id 函数用于获取会话 id, 但这一结果通常是 用户 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)

获取 session id

from nonebot_plugin_session import SessionId, SessionIdType

@matcher.handle()
async def handle(session_id: str = SessionId(SessionIdType.GROUP)):
    # 获取 “群组级别” 的 session id
    ...

Session 存至数据库中(需要安装 nonebot-plugin-datastore 插件)

from nonebot_plugin_session import extract_session
from nonebot_plugin_session.model import get_or_add_session_model

@matcher.handle()
async def handle(bot: Bot, event: Event):
    session = extract_session(bot, event)
    model = await get_or_add_session_model(session)  # 可关联其他表用于筛选等

Session 中获取 saaPlatformTarget 对象用于发送(需要安装 nonebot-plugin-send-anything-anywhere 插件)

from nonebot_plugin_session import extract_session

@matcher.handle()
async def handle(bot: Bot, event: Event):
    session = extract_session(bot, event)
    target = session.get_saa_target()  # 用于发送

不同的 “会话级别” 与 “会话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 QQ Guild

鸣谢

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_session-0.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

nonebot_plugin_session-0.0.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_session-0.0.1.tar.gz.

File metadata

  • Download URL: nonebot_plugin_session-0.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.0 Linux/5.15.0-1037-azure

File hashes

Hashes for nonebot_plugin_session-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5f5bb0a54199681fb8c0d77f28abc9ded4aa555b62dcdde5bcd875e7cf0cda3d
MD5 3547348b69e651fb5df03298a9e4bc94
BLAKE2b-256 b2dfd9203f8430a544ff22f93b5547d7f4db98e8a751432508a52b715c6b7655

See more details on using hashes here.

Provenance

File details

Details for the file nonebot_plugin_session-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_session-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3495532055ebe3ba775ad61c9576afd456179e9d5893c4755934659257495447
MD5 29a0f8dc1ef42db2f3d0e91d9a05075a
BLAKE2b-256 003acc016068189fb89b1885c49e9b04ee161aea3cc6e99c69a2bffa99273857

See more details on using hashes here.

Provenance

Supported by

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