Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,6 @@ bots.yaml

bot_data/

bot.yaml
bot.yaml

logs/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ from bot_sdk import (
Message,
CommandSpec,
CommandArgument,
setup_logging
setup_logging,
)

class MyBot(BaseBot):
def __init__(self, client):
# BaseBot will receive a bot-specific logger when used via BotRunner / console
super().__init__(client)
# Register commands (prefixes come from bot.yaml)
self.command_parser.register_spec(
Expand All @@ -129,7 +130,8 @@ class MyBot(BaseBot):

async def on_start(self):
"""Called when bot starts"""
print(f"Bot started! User ID: {self._user_id}")
# Prefer structured logging over print
self.logger.info("Bot started! user_id={}", self._user_id)

async def handle_echo(self, invocation, message, bot):
"""Handle echo command"""
Expand All @@ -138,6 +140,7 @@ class MyBot(BaseBot):

async def on_message(self, message: Message):
"""Handle non-command messages"""
self.logger.debug("Incoming message: {}", message.content[:50])
await self.send_reply(message, "Try !help to see available commands!")

BOT_CLASS = MyBot
Expand Down
7 changes: 5 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ from bot_sdk import (
Message,
CommandSpec,
CommandArgument,
setup_logging
setup_logging,
)

class MyBot(BaseBot):
def __init__(self, client):
# 通过 BotRunner / 控制台启动时,BaseBot 会注入带有 Bot 名称标签的 logger
super().__init__(client)
# 注册命令(前缀来源于 bot.yaml)
self.command_parser.register_spec(
Expand All @@ -127,7 +128,8 @@ class MyBot(BaseBot):

async def on_start(self):
"""启动时调用"""
print(f"Bot started! User ID: {self._user_id}")
# 推荐使用结构化日志而不是 print
self.logger.info("Bot started! user_id={}", self._user_id)

async def handle_echo(self, invocation, message, bot):
"""处理 echo 命令"""
Expand All @@ -136,6 +138,7 @@ class MyBot(BaseBot):

async def on_message(self, message: Message):
"""处理非命令消息"""
self.logger.debug("Incoming message: {}", message.content[:50])
await self.send_reply(message, "尝试使用 !help 查看可用命令!")

BOT_CLASS = MyBot
Expand Down
123 changes: 62 additions & 61 deletions bot_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from .async_zulip import AsyncClient
from .bot import BaseBot
from .commands import (
CommandArgument,
CommandError,
CommandInvocation,
CommandParser,
CommandSpec,
InvalidArgumentsError,
UnknownCommandError,
OptionValidator,
CommandArgument,
CommandError,
CommandInvocation,
CommandParser,
CommandSpec,
InvalidArgumentsError,
UnknownCommandError,
OptionValidator,
Validator,
)
from .runner import BotRunner
from .logging import setup_logging
from .log import setup_logging, get_bot_logger
from .i18n import I18n, build_i18n_for_bot
from .storage import BotStorage, CachedStorage
from .config import (
Expand All @@ -21,20 +21,20 @@
save_config,
)
from .models import (
Event,
EventsResponse,
Message,
PrivateMessageRequest,
PrivateRecipient,
RegisterResponse,
UserProfileResponse,
SendMessageResponse,
StreamMessageRequest,
ProfileFieldValue,
User,
Event,
EventsResponse,
Message,
PrivateMessageRequest,
PrivateRecipient,
RegisterResponse,
UserProfileResponse,
SendMessageResponse,
StreamMessageRequest,
ProfileFieldValue,
User,
UpdatePresenceRequest,
GetUserGroupsRequest,
GetUserGroupsResponse,
GetUserGroupsResponse,
DataModel,
Timestamped,
IDModel,
Expand All @@ -50,48 +50,49 @@
)

__all__ = [
"AsyncClient",
"BaseBot",
"CommandParser",
"CommandSpec",
"CommandArgument",
"CommandInvocation",
"CommandError",
"InvalidArgumentsError",
"UnknownCommandError",
"BotRunner",
"setup_logging",
"BotStorage",
"CachedStorage",
"AsyncClient",
"BaseBot",
"CommandParser",
"CommandSpec",
"CommandArgument",
"CommandInvocation",
"CommandError",
"InvalidArgumentsError",
"UnknownCommandError",
"BotRunner",
"setup_logging",
"BotStorage",
"CachedStorage",
"StorageConfig",
"Event",
"EventsResponse",
"Message",
"PrivateRecipient",
"StreamMessageRequest",
"PrivateMessageRequest",
"SendMessageResponse",
"UserProfileResponse",
"RegisterResponse",
"ProfileFieldValue",
"Event",
"EventsResponse",
"Message",
"PrivateRecipient",
"StreamMessageRequest",
"PrivateMessageRequest",
"SendMessageResponse",
"UserProfileResponse",
"RegisterResponse",
"ProfileFieldValue",
"UpdatePresenceRequest",
"User",
"User",
"GetUserGroupsRequest",
"GetUserGroupsResponse",
"GetUserGroupsResponse",
"Validator",
"OptionValidator",
"DataModel",
"Timestamped",
"IDModel",
"SoftDelete",
"Base",
"make_sqlite_url",
"create_engine",
"create_sessionmaker",
"session_scope",
"AsyncRepository",
"I18n",
"build_i18n_for_bot",
"load_config",
"save_config",
"OptionValidator",
"DataModel",
"Timestamped",
"IDModel",
"SoftDelete",
"Base",
"make_sqlite_url",
"create_engine",
"create_sessionmaker",
"session_scope",
"AsyncRepository",
"I18n",
"build_i18n_for_bot",
"load_config",
"save_config",
"get_bot_logger",
]
Loading