An asynchronous Python bot framework for building cross-platform chatbots with plugin architecture and adapter support.
—
Core functions for initializing, configuring, and running the NoneBot2 framework. These functions manage the global framework state and lifecycle.
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")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)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...")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()DOTENV_TYPE = Union[Path, str, list[Union[Path, str]], tuple[Union[Path, str], ...]]Install with Tessl CLI
npx tessl i tessl/pypi-nonebot2