| name | add-channel |
| description | MindBot 通道开发指南——BaseChannel 接口契约、注册机制、消息处理流程与完整代码模板 |
| when_to_use | 添加新通道、新增 channel、接入新平台(如 Discord、微信、Slack) |
添加新通道
架构定位
通道属于 L1 接口/传输层,职责是对接外部消息协议。通道只负责收发消息,通过 MessageBus 与 MindBot 主链路交互。
依赖规则:通道 → MindBot(L2),禁止通道直接调用 Provider(L5)或工具(L4)。
接口契约
继承 BaseChannel,实现 4 个方法:
class BaseChannel(ABC):
name: str = "base"
def __init__(self, config: Any, bus: MessageBus):
self.config = config
self.bus = bus
self._running = False
@abstractmethod
async def start(self) -> None:
"""启动通道,开始监听消息"""
@abstractmethod
async def stop(self) -> None:
"""停止通道,释放资源"""
@abstractmethod
async def send(self, msg: OutboundMessage) -> None:
"""向用户发送消息"""
def is_allowed(self, sender_id: str) -> bool:
"""权限检查,可在子类覆盖"""
注册方式
在 src/mindbot/channels/manager.py 的 _init_channels() 中添加:
def _init_channels(self) -> None:
channels_config = getattr(self.config, "channels", None)
my_config = getattr(channels_config, "my_channel", None)
if my_config and getattr(my_config, "enabled", False):
from mindbot.channels.my_channel import MyChannel
self.channels["my_channel"] = MyChannel(my_config, self.bus)
消息处理流程
async def _handle_message(self, sender_id: str, text: str) -> None:
"""收到外部消息后的处理"""
if not self.is_allowed(sender_id):
return
await self.bus.publish(InboundMessage(
channel=self.name,
sender_id=sender_id,
content=text,
))
完整模板
from typing import Any
from mindbot.channels.base import BaseChannel
from mindbot.bus.events import InboundMessage, OutboundMessage
class MyChannel(BaseChannel):
name: str = "my_channel"
def __init__(self, config: Any, bus: "MessageBus") -> None:
super().__init__(config, bus)
async def start(self) -> None:
self._running = True
async def stop(self) -> None:
self._running = False
async def send(self, msg: OutboundMessage) -> None:
pass
async def _handle_message(self, sender_id: str, text: str) -> None:
if not self.is_allowed(sender_id):
return
from mindbot.bus.events import InboundMessage
await self.bus.publish(InboundMessage(
channel=self.name,
sender_id=sender_id,
content=text,
))
配置
在 settings.json 的 channels 下添加配置:
{
"channels": {
"my_channel": {
"enabled": true,
"allowed_senders": []
}
}
}
检查清单