From d0b4f0d70d025ba3ffa0a9127b280d8325bb698f Mon Sep 17 00:00:00 2001 From: Re-bin Date: Wed, 11 Mar 2026 07:57:12 +0000 Subject: [PATCH] feat(wecom): add WeCom channel with SDK pinned to GitHub tag v0.1.2 --- README.md | 25 ++++++++++++++----------- nanobot/channels/manager.py | 1 - nanobot/channels/wecom.py | 8 ++++---- nanobot/config/schema.py | 2 +- pyproject.toml | 4 +++- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5be0ce5..6e8211e 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ Connect nanobot to your favorite chat platform. | **Slack** | Bot token + App-Level token | | **Email** | IMAP/SMTP credentials | | **QQ** | App ID + App Secret | -| **Wecom** | Bot ID + App Secret | +| **Wecom** | Bot ID + Bot Secret |
Telegram (Recommended) @@ -683,12 +683,17 @@ nanobot gateway Uses **WebSocket** long connection — no public IP required. -**1. Create a wecom bot** +**1. Install the optional dependency** -In the client's workspace, click on "Intelligent Robot" to create a robot and choose API mode for creation. -Select to create in "long connection" mode, and obtain Bot ID and Secret. +```bash +pip install nanobot-ai[wecom] +``` -**2. Configure** +**2. Create a WeCom AI Bot** + +Go to the WeCom admin console → Intelligent Robot → Create Robot → select **API mode** with **long connection**. Copy the Bot ID and Secret. + +**3. Configure** ```json { @@ -696,23 +701,21 @@ Select to create in "long connection" mode, and obtain Bot ID and Secret. "wecom": { "enabled": true, "botId": "your_bot_id", - "secret": "your_secret", - "allowFrom": [ - "your_id" - ] + "secret": "your_bot_secret", + "allowFrom": ["your_id"] } } } ``` -**3. Run** +**4. Run** ```bash nanobot gateway ``` > [!TIP] -> wecom uses WebSocket to receive messages — no webhook or public IP needed! +> WeCom uses WebSocket to receive messages — no webhook or public IP needed!
diff --git a/nanobot/channels/manager.py b/nanobot/channels/manager.py index 369795a..2c5cd3f 100644 --- a/nanobot/channels/manager.py +++ b/nanobot/channels/manager.py @@ -156,7 +156,6 @@ class ChannelManager: self.channels["wecom"] = WecomChannel( self.config.channels.wecom, self.bus, - groq_api_key=self.config.providers.groq.api_key, ) logger.info("WeCom channel enabled") except ImportError as e: diff --git a/nanobot/channels/wecom.py b/nanobot/channels/wecom.py index dc97311..1c44451 100644 --- a/nanobot/channels/wecom.py +++ b/nanobot/channels/wecom.py @@ -2,6 +2,7 @@ import asyncio import importlib.util +import os from collections import OrderedDict from typing import Any @@ -36,10 +37,9 @@ class WecomChannel(BaseChannel): name = "wecom" - def __init__(self, config: WecomConfig, bus: MessageBus, groq_api_key: str = ""): + def __init__(self, config: WecomConfig, bus: MessageBus): super().__init__(config, bus) self.config: WecomConfig = config - self.groq_api_key = groq_api_key self._client: Any = None self._processed_message_ids: OrderedDict[str, None] = OrderedDict() self._loop: asyncio.AbstractEventLoop | None = None @@ -50,7 +50,7 @@ class WecomChannel(BaseChannel): async def start(self) -> None: """Start the WeCom bot with WebSocket long connection.""" if not WECOM_AVAILABLE: - logger.error("WeCom SDK not installed. Run: pip install wecom-aibot-sdk-python") + logger.error("WeCom SDK not installed. Run: pip install nanobot-ai[wecom]") return if not self.config.bot_id or not self.config.secret: @@ -213,7 +213,6 @@ class WecomChannel(BaseChannel): if file_url and aes_key: file_path = await self._download_and_save_media(file_url, aes_key, "image") if file_path: - import os filename = os.path.basename(file_path) content_parts.append(f"[image: {filename}]\n[Image: source: {file_path}]") else: @@ -308,6 +307,7 @@ class WecomChannel(BaseChannel): media_dir = get_media_dir("wecom") if not filename: filename = fname or f"{media_type}_{hash(file_url) % 100000}" + filename = os.path.basename(filename) file_path = media_dir / filename file_path.write_bytes(data) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index b772d18..bb0d286 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -208,7 +208,7 @@ class WecomConfig(Base): secret: str = "" # Bot Secret from WeCom AI Bot platform allow_from: list[str] = Field(default_factory=list) # Allowed user IDs welcome_message: str = "" # Welcome message for enter_chat event - react_emoji: str = "eyes" # Emoji for message reactions + class ChannelsConfig(Base): """Configuration for chat channels.""" diff --git a/pyproject.toml b/pyproject.toml index 0582be6..9868513 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,11 +44,13 @@ dependencies = [ "json-repair>=0.57.0,<1.0.0", "chardet>=3.0.2,<6.0.0", "openai>=2.8.0", - "wecom-aibot-sdk-python>=0.1.2", "tiktoken>=0.12.0,<1.0.0", ] [project.optional-dependencies] +wecom = [ + "wecom-aibot-sdk-python @ git+https://github.com/chengyongru/wecom_aibot_sdk.git@v0.1.2", +] matrix = [ "matrix-nio[e2e]>=0.25.2", "mistune>=3.0.0,<4.0.0",