Overview
Adding a new channel to nanobot allows users to interact with the agent through additional chat platforms. Channels are the interface between nanobot and external messaging services.Channel Architecture
All channels implement theBaseChannel abstract class, which provides a consistent interface for:
- Starting the channel (connecting to the platform)
- Receiving messages from the platform
- Sending messages to the platform
- Stopping the channel (cleanup and disconnect)
The BaseChannel Interface
Step-by-Step Guide
Step 1: Create the Channel File
Create a new file innanobot/channels/, e.g., mychannel.py.
Step 2: Define the Config Schema
Editnanobot/config/schema.py and add a configuration class:
ChannelsConfig:
Step 3: Implement the Channel Class
Step 4: Register the Channel
Editnanobot/channels/manager.py to register your channel:
Step 5: Test Your Channel
- Configure in
~/.nanobot/config.json:
- Run the gateway:
- Send a test message from the platform.
- Verify nanobot responds.
Real-World Example: Telegram Channel
Let’s examine the Telegram channel implementation as a reference.Key Features
- Long polling (no webhook needed)
- Media handling (photos, voice, documents)
- Typing indicators
- Command handling (
/start,/new,/help) - Media groups (multiple images in one message)
- Voice transcription (via Groq Whisper)
Code Structure
Typing Indicator Implementation
Common Patterns
1. Access Control
Use the built-inis_allowed() method:
2. Media Handling
Download media to~/.nanobot/media/:
3. Voice Transcription
Use Groq for voice-to-text:4. Long Polling vs WebSocket
Long Polling (Telegram):5. Message Formatting
Convert markdown to platform-specific format:6. Group/Thread Support
For platforms with threads (Discord, Slack):7. Mention Detection
For group chats, respond only when mentioned:8. Error Handling
Log errors and continue:Platform-Specific Considerations
Telegram
- Uses long polling (no public IP needed)
- Supports media groups (multiple images)
- Has command menu (BotFather registration)
- Requires HTML escaping for special characters
Discord
- Uses WebSocket gateway
- Requires intents (Message Content intent)
- Has typing indicators and reactions
- Supports threads (session isolation)
- Requires bridge server (Node.js)
- Uses QR code login
- Media sent as base64 or URLs
- No group support (DMs only)
Slack
- Uses Socket Mode (no public URL)
- Requires app-level token
- Supports threads and reactions
- Uses mrkdwn formatting (not markdown)
- Uses IMAP (receive) and SMTP (send)
- Long poll interval (30s default)
- Requires consent flag (mailbox access)
- Supports attachments and HTML emails
Feishu/DingTalk/QQ
- Use WebSocket long connection
- No public IP required
- Require app registration on platform
- Use open_id / staff_id for users
Testing Checklist
Basic Functionality
- Channel connects successfully
- Receives text messages
- Sends text responses
- Access control works (
allowFrom) - Channel disconnects cleanly
Media Support
- Receives images
- Sends images
- Receives voice/audio (if applicable)
- Transcribes voice (if Groq configured)
- Receives documents/files
Advanced Features
- Typing indicators (if applicable)
- Reactions (if applicable)
- Thread support (if applicable)
- Group chat support
- Mention detection (groups)
- Command handling (
/new,/help)
Error Handling
- Handles network disconnects
- Recovers from rate limits
- Logs errors without crashing
- Invalid
chat_idhandled gracefully
Configuration
- Config schema documented
- Required fields validated
- Optional fields have defaults
- README updated with setup instructions
Debugging Tips
Enable Debug Logging
Inspect Message Bus Events
Test with --logs Flag
Use Simple Test Message
Performance Considerations
Connection Pooling
Reuse HTTP connections:Rate Limiting
Implement backoff:Message Batching
Batch multiple media files:Security Best Practices
Validate Input
Sanitize Content
Protect API Credentials
Implement Access Control
Contributing Your Channel
When submitting a PR:- Implement all BaseChannel methods
- Add config schema to
schema.py - Register in ChannelManager
- Add README section with setup instructions
- Test thoroughly (see checklist above)
- Include example config in PR description
- Document special requirements (API keys, permissions, etc.)