This project implements a local, self-hosted autonomous AI agent ecosystem. It pairs OpenClaw (The Brain) with n8n (The Builder) to create a system that can chat with users via Telegram, plan complex tasks, and execute structured workflows.
The system consists of two primary agents running on a single machine via Docker:
- Role: Cognitive Engine & Interface.
- Interface: Connects to Telegram to chat with users.
- Function: Understands natural language, maintains memory/context, plans tasks, and decides when to call external tools.
- Model: Powered by Google Gemini (Flash).
- Role: Tool Executor & Workflow Engine.
- Interface: Webhooks.
- Function: Executes deterministic, complex logic (e.g., "Look up customer X in database," "Scrape website Y," "Generate PDF report").
- Integration: OpenClaw treats n8n webhooks as "Skills" or "Tools" it can invoke when needed.
- Self-Hosted: Runs entirely on your machine/server using Docker Compose.
- Secure:
- Pairing Mode: The bot ignores strangers until you explicitly approve their Telegram ID via the CLI.
- Dashboard Auth: The web dashboard is protected by a secure token.
- Persistent: Database (Postgres) and Agent Memory (Filesystem) are persisted across restarts.
- Visual Dashboard: Real-time view of the agent's thought process ("Canvas").
- Runtime: Docker & Docker Compose
- AI Agent: OpenClaw (Node.js)
- Workflow Automation: n8n
- Database: PostgreSQL (for n8n)
- Channel: Telegram Bot API
- Docker Desktop installed and running.
- Git installed.
- Telegram Bot Token (from @BotFather).
- Google Gemini API Key (from Google AI Studio).
-
Clone the repository:
git clone https://github.com/khallad2/ClawFlow.git ai-stack cd ai-stack -
Create
.envfile: Copy the example file and update it with your secrets.cp .env.example .env # Edit .env with your favorite editor nano .env- Set
OPENCLAW_TOKENto a secure random string.
openssl rand -base64 32- Add your
GOOGLE_API_KEYandTELEGRAM_BOT_TOKEN.
- Set
-
Clone OpenClaw Source (Required for Build):
git clone https://github.com/openclaw/openclaw.git openclaw_source
-
Create Data Directories:
mkdir -p openclaw_data n8n_data postgres_data chmod -R 777 openclaw_data n8n_data postgres_data
The default docker-compose.yml keeps the container valid but idle so you can run the setup wizard.
- Build and Start:
docker compose build --no-cache openclaw docker compose up -d
Enter the container to configure your agent interactively.
docker exec -it openclaw_brain openclaw onboard --install-daemon- Gateway: Select "Local".
- Model: Select Gemini -> Paste API Key -> Select
gemini-2.5-flash. - Channel: Select Telegram -> Paste Bot Token.
- Permissions: Enter your Telegram Username (e.g.,
@MyUser).
The wizard sets defaults that work for local apps but break in Docker. You MUST manually edit the config file.
- Open
openclaw_data/openclaw.jsonin your text editor. - Network Bind: Find
"bind": "loopback"and change it to"bind": "lan".- Why? "loopback" locks it to the container. "lan" allows your browser to reach it.
- Model Name: Ensure the model is
google/gemini-2.5-flash.- Why? The
google/prefix is mandatory.
- Why? The
Example of correct openclaw.json you find it in openclaw_temlate.json: make sure that controlUi exists or copy it to your openclaw.json
{
"gateway": {
"port": 18789,
"mode": "local",
"bind": "lan",
"auth": {
"mode": "token",
"token": "OPENCLAW_TOKEN"
},
"tailscale": {
"mode": "off",
"resetOnExit": false
},
"nodes": {
"denyCommands": [
"camera.snap",
"camera.clip",
"screen.record",
"calendar.add",
"contacts.add",
"reminders.add"
]
},
"controlUi": {
"allowInsecureAuth": true
}
},
"agents": {
"defaults": {
"model": { "primary": "google/gemini-2.5-flash" }
}
}
}Now we switch OpenClaw from "Setup Mode" to "Production Mode".
- Open
docker-compose.yml. - Comment out:
command: tail -f /dev/null - Uncomment:
command: openclaw gateway --port 18789 - Restart:
docker compose down docker compose up -d
-
Access Dashboard:
- Retrieve your secure token from
openclaw_data/openclaw.json(look forgateway.auth.token). - Open:
http://localhost:18789/#token=YOUR_TOKEN
- Retrieve your secure token from
-
Pair with Telegram:
- Message
/startto your bot. - It will reply with a specific authorization ID.
- Approve it via terminal:
docker exec -it openclaw_brain openclaw pairing approve telegram <YOUR_AUTH_ID>
- Message
-
Chat: You can now chat with your agent via Telegram!
User (Telegram): "Check the order status for customer #12345."
- OpenClaw (Brain): Receives message -> Analyzes intent -> Call
check_order_tool. - n8n (Developer): Receives Webhook -> Queries Database -> Returns
{ status: "Shipped", tracking: "AX99" }. - OpenClaw (Brain): Generates response.
- User (Telegram): Receives: "Your order #12345 has been shipped! Tracking number is AX99."