An asynchronous Python bot framework for building cross-platform chatbots with plugin architecture and adapter support.
—
Functions for accessing and managing bot instances connected to the NoneBot2 framework. Bots represent connections to specific messaging platforms and handle message sending/receiving.
Get specific bot instances by ID or retrieve any available bot.
def get_bot(self_id: Optional[str] = None) -> Bot:
"""
Get a Bot object connected to NoneBot.
Parameters:
- self_id: Bot identifier (Bot.self_id attribute), optional
Returns:
Bot: Bot object instance
Raises:
KeyError: If specified self_id bot doesn't exist
ValueError: If no self_id provided and no bots available
ValueError: If global Driver object not initialized
"""Usage example:
import nonebot
# Get any available bot
bot = nonebot.get_bot()
# Get specific bot by ID
bot = nonebot.get_bot("12345")
# Use bot to send messages
await bot.send_private_msg(user_id="67890", message="Hello!")Access all connected bot instances.
def get_bots() -> dict[str, Bot]:
"""
Get all Bot objects connected to NoneBot.
Returns:
dict[str, Bot]: Dictionary with Bot.self_id as keys and Bot objects as values
Raises:
ValueError: If global Driver object not initialized
"""Usage example:
import nonebot
# Get all bots
bots = nonebot.get_bots()
# Iterate through all bots
for bot_id, bot in bots.items():
print(f"Bot {bot_id}: {bot.type}")
# Send message to all bots
for bot in bots.values():
await bot.send_private_msg(user_id="user123", message="Broadcast message")Get registered adapter instances by name or type.
def get_adapter(name: Union[str, type[Adapter]]) -> Adapter:
"""
Get registered Adapter instance.
Parameters:
- name: Adapter name (str) or Adapter class type
Returns:
Adapter: Specified Adapter object
Raises:
ValueError: If specified Adapter not registered
ValueError: If global Driver object not initialized
"""Usage example:
import nonebot
from nonebot.adapters.console import Adapter as ConsoleAdapter
# Get adapter by name
adapter = nonebot.get_adapter("Console")
# Get adapter by type
adapter = nonebot.get_adapter(ConsoleAdapter)
# Use adapter for advanced operations
print(f"Adapter name: {adapter.get_name()}")Retrieve all registered adapter instances.
def get_adapters() -> dict[str, Adapter]:
"""
Get all registered Adapter instances.
Returns:
dict[str, Adapter]: Dictionary of all Adapter instances
Raises:
ValueError: If global Driver object not initialized
"""Usage example:
import nonebot
# Get all adapters
adapters = nonebot.get_adapters()
# List all available adapters
for adapter_name, adapter in adapters.items():
print(f"Adapter: {adapter_name}")
# Check if specific adapter is available
if "Console" in adapters:
console_adapter = adapters["Console"]class Bot(abc.ABC):
"""Base class for bot implementations."""
def __init__(self, adapter: Adapter, self_id: str): ...
async def call_api(self, api: str, **data: Any) -> Any:
"""Call bot API method."""
@property
def type(self) -> str:
"""Get adapter name."""
@property
def config(self) -> Config:
"""Get global configuration."""
class Adapter(abc.ABC):
"""Base class for protocol adapters."""
def get_name(self) -> str:
"""Get adapter name."""
def bot_connect(self, bot: Bot) -> None:
"""Handle bot connection."""
def bot_disconnect(self, bot: Bot) -> None:
"""Handle bot disconnection."""Install with Tessl CLI
npx tessl i tessl/pypi-nonebot2