Channels
Create custom channel integrations
Custom Channels
Create custom channel integrations for platforms not natively supported by SwiftClaw.
Channel Interface
Implement the channel interface:
from swiftclaw import Channel
class CustomChannel(Channel):
def __init__(self, config):
self.config = config
async def connect(self):
"""Establish connection to the platform"""
pass
async def receive(self):
"""Receive messages from the platform"""
pass
async def send(self, message):
"""Send messages to the platform"""
pass
async def disconnect(self):
"""Close connection"""
passExample: Custom API Channel
import aiohttp
from swiftclaw import Channel
class APIChannel(Channel):
def __init__(self, api_url, api_key):
self.api_url = api_url
self.api_key = api_key
self.session = None
async def connect(self):
self.session = aiohttp.ClientSession(
headers={"Authorization": f"Bearer {self.api_key}"}
)
async def receive(self):
async with self.session.get(f"{self.api_url}/messages") as resp:
messages = await resp.json()
return messages
async def send(self, message):
async with self.session.post(
f"{self.api_url}/messages",
json={"content": message}
) as resp:
return await resp.json()
async def disconnect(self):
await self.session.close()Register Custom Channel
Register your channel with SwiftClaw:
from swiftclaw import Agent
agent = Agent(name="my-agent")
# Register custom channel
agent.register_channel(
name="custom-api",
channel=APIChannel(
api_url="https://api.example.com",
api_key="your-api-key"
)
)Configuration
Configure custom channels:
{
"channels": {
"custom-api": {
"enabled": true,
"config": {
"api_url": "https://api.example.com",
"api_key": "${CUSTOM_API_KEY}"
}
}
}
}Message Handling
Handle incoming messages:
@agent.on_message(channel="custom-api")
async def handle_custom_message(message):
response = await agent.generate(message.content)
return responseWebhooks
Set up webhooks for real-time messages:
from swiftclaw import webhook
@webhook("/custom-api/webhook")
async def handle_webhook(request):
data = await request.json()
message = data["message"]
response = await agent.process(message)
return {"response": response}Testing
Test your custom channel:
swiftclaw channels test custom-api \
--message "Hello, agent!"Best Practices
Error Handling
Implement robust error handling:
async def send(self, message):
try:
return await self._send_internal(message)
except Exception as e:
self.log_error(e)
raise ChannelError(f"Failed to send: {e}")Rate Limiting
Respect platform rate limits:
from swiftclaw import RateLimiter
limiter = RateLimiter(requests=100, period=60)
async def send(self, message):
await limiter.acquire()
return await self._send_internal(message)Connection Management
Handle reconnections:
async def ensure_connected(self):
if not self.is_connected():
await self.connect()Custom channels give you full control over how your agents interact with any platform.
How is this guide ?
Last updated on