这个小节我们来讲解一下openclaw的通道概念。
1. 概念解析
1.1. 通道
所谓通道,就是管理平台的方法类,比如接受cli输入的CLIChannel, 管理Telegrame会话的TelegramChannel,以及管理Feishu会话的FeishuChannel。在openclaw中,每个xxChannel都继承自一个Channel抽象方法.
python
class Channel(ABC):
@abstractmethod
def receive(self) -> InboundMessage | None: ...
@abstractmethod
def send(self, to: str, text: str, **kwargs: Any) -> bool: ...
1.2. 统一消息格式
在OpenClaw中,为了能够统一接受和处理相同消息,申明了统一的消息格式,如下所示:
python
@dataclass
class InboundMessage:
text: str # 消息文本
sender_id: str # 发送者ID
channel: str # 来源平台("cli"/"telegram"/"feishu")
account_id: str # Bot账号标识(支持同平台多账号)
peer_id: str # 会话唯一标识(私聊为用户ID,群聊为群ID)
is_group: bool # 是否为群聊
media: list # 附件信息
raw: dict # 平台原始数据(用于调试)
其中比较重要的有:
text: 接受的消息内容
sender_id: 发送者的id, 比如account_xxx
channel: 表示来源的平台,是cli, telegram还是feishu
2. 流程设计

根据流程图我们可以知道:
- CLIChannel, TelegramChannel和FeishuChannel分别和各自的平台保持通信和会话
- 接受到消息后,统一包装成InboundMessage的格式储存在消息队列中
- 循环读取消息队列的消息,发送给run_agent_turn进行消息处理,在这里会读取消息记录和绑定tools方法,然后发送给大模型接受回复和工具处理
- 发送给相应的Channel进行通信