The core components of Bot SDK provide all the basic functionality needed to build Zulip bots.
Bot SDK contains the following core components:
┌─────────────────────────────────────────────┐
│ Your Bot │
│ (extends BaseBot) │
│ │
│ - on_message() │
│ - Command handlers │
│ - Custom logic │
└──────────────┬──────────────────────────────┘
│
┌──────────────▼──────────────────────────────┐
│ BaseBot │
│ │
│ - Event dispatching │
│ - Command parsing │
│ - Message replies │
└──────────────┬──────────────────────────────┘
│
┌──────────────▼──────────────────────────────┐
│ AsyncClient │
│ │
│ - HTTP requests │
│ - Event polling │
│ - API wrapping │
└──────────────┬──────────────────────────────┘
│
┌──────────────▼──────────────────────────────┐
│ Zulip Server │
└──────────────────────────────────────────────┘
Low-level HTTP client, responsible for:
Use cases:
Example:
from bot_sdk import AsyncClient
async with AsyncClient() as client:
profile = await client.get_profile()
print(profile.full_name)
Mid-level abstraction, responsible for:
Use cases:
Example:
from bot_sdk import BaseBot, Message
class MyBot(BaseBot):
async def on_message(self, message: Message):
await self.send_reply(message, "Hello!")
Top-level manager, responsible for:
Use cases:
Example:
from bot_sdk import BotRunner
async with BotRunner(lambda c: MyBot(c)) as runner:
await runner.run_forever()
from bot_sdk import BaseBot, Message, run_bot
class SimpleBot(BaseBot):
async def on_message(self, message: Message):
await self.send_reply(message, "Echo: " + message.content)
if __name__ == "__main__":
run_bot(SimpleBot)
from bot_sdk import BaseBot, Message, CommandSpec, CommandArgument
class CommandBot(BaseBot):
def __init__(self, client):
super().__init__(client)
self.command_parser.register_spec(
CommandSpec(
name="hello",
description="Say hello",
handler=self.handle_hello
)
)
async def handle_hello(self, invocation, message, bot):
await self.send_reply(message, "Hello, World!")
async def on_message(self, message: Message):
await self.send_reply(message, "Use /help for commands")