CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-nonebot2

An asynchronous Python bot framework for building cross-platform chatbots with plugin architecture and adapter support.

Pending
Overview
Eval results
Files

framework-control.mddocs/

Framework Control

Core functions for initializing, configuring, and running the NoneBot2 framework. These functions manage the global framework state and lifecycle.

Capabilities

Framework Initialization

Initialize the NoneBot2 framework with configuration options.

def init(*, _env_file: Optional[DOTENV_TYPE] = None, **kwargs: Any) -> None:
    """
    Initialize NoneBot and global Driver object.

    Parameters:
    - _env_file: Configuration file name, defaults to .env.{env_name}
    - **kwargs: Additional variables stored in Driver.config object

    Raises:
    - ValueError: If initialization fails
    """

Usage example:

import nonebot

# Basic initialization
nonebot.init()

# With custom configuration
nonebot.init(
    driver="~fastapi+~httpx+~websockets",
    host="0.0.0.0", 
    port=8080,
    log_level="DEBUG"
)

# With custom environment file
nonebot.init(_env_file=".env.development")

Framework Execution

Start the NoneBot2 framework and run the event loop.

def run(*args: Any, **kwargs: Any) -> None:
    """
    Start NoneBot by running the global Driver object.

    Parameters:
    - *args: Positional arguments passed to Driver.run()
    - **kwargs: Keyword arguments passed to Driver.run()
    """

Usage example:

import nonebot

# Initialize framework
nonebot.init()

# Run with default settings
nonebot.run()

# Run with custom arguments (driver-specific)
nonebot.run(host="127.0.0.1", port=8080)

Driver Access

Get access to the global Driver instance for advanced operations.

def get_driver() -> Driver:
    """
    Get the global Driver instance.

    Returns:
    Driver: Global Driver object

    Raises:
    ValueError: If NoneBot has not been initialized (init() not called)
    """

Usage example:

import nonebot

# Must initialize first
nonebot.init()

# Get driver instance
driver = nonebot.get_driver()

# Use driver for advanced operations
@driver.on_startup
async def startup():
    print("NoneBot is starting up...")

@driver.on_shutdown
async def shutdown():
    print("NoneBot is shutting down...")

ASGI Support

Access ASGI-related functionality for web framework integration.

def get_app() -> Any:
    """
    Get the Server App object for ASGIMixin drivers.

    Returns:
    Any: Server App object (e.g., FastAPI app, Quart app)

    Raises:
    AssertionError: If global Driver is not ASGIMixin type
    ValueError: If global Driver object is not initialized
    """
def get_asgi() -> Any:
    """
    Get the ASGI application object for ASGIMixin drivers.

    Returns:
    Any: ASGI application object

    Raises:
    AssertionError: If global Driver is not ASGIMixin type  
    ValueError: If global Driver object is not initialized
    """

Usage example:

import nonebot

# Initialize with ASGI-compatible driver
nonebot.init(driver="~fastapi")

# Get app for custom routes
app = nonebot.get_app()

@app.get("/health")
async def health_check():
    return {"status": "ok"}

# Get ASGI app for deployment
asgi_app = nonebot.get_asgi()

Types

DOTENV_TYPE = Union[Path, str, list[Union[Path, str]], tuple[Union[Path, str], ...]]

Install with Tessl CLI

npx tessl i tessl/pypi-nonebot2

docs

adapters-drivers.md

bot-management.md

configuration.md

dependencies-parameters.md

event-handlers.md

framework-control.md

index.md

message-matching.md

plugin-system.md

tile.json