How to Connect OpenClaw to Telegram for 24/7 AI Assistance

If you’re looking to turn your OpenClaw instance into a personal, always-on AI assistant accessible from your phone, connecting it to Telegram is the most practical solution. The common pitfall is thinking you need complex webhooks or a full-blown web server. For most users, a simple polling mechanism combined with a systemd service is far more robust and easier to maintain, especially on a VPS where resources are shared. I’ve found this setup to be rock-solid on a Hetzner CX11, providing continuous uptime without the headaches of managing external reverse proxies.

Looking to get a VPS for your project? Vultr offers reliable VPS hosting starting at $5/month with global data centers. Many OpenClaw users self-host on Vultr for consistent uptime and affordable pricing.

\n

Affiliate Disclosure: As an Amazon Associate, we earn from qualifying purchases. This means we may earn a small commission when you click our links and make a purchase on Amazon. This comes at no extra cost to you and helps support our site.

\n

Setting up Your Telegram Bot

\n

First, you need a Telegram bot. Talk to @BotFather on Telegram. Send him /newbot, give your bot a name (e.g., “MyOpenClawAI”) and a username (e.g., “MyOpenClaw_bot”). BotFather will give you an API token. It looks something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11. Keep this token safe; it’s how your OpenClaw instance will interact with Telegram.

\n

Next, you need to get your Telegram User ID. There are several bots for this, but @userinfobot is reliable. Just start a chat with it, and it will tell you your ID (a sequence of digits). This is crucial because you don’t want your OpenClaw bot to respond to just anyone on Telegram; you want it to be exclusively for you or a trusted group.

\n

Configuring OpenClaw for Telegram Integration

\n

OpenClaw doesn’t have native Telegram integration out of the box, but we can easily bridge it using a small Python script that acts as a middleware. This script will poll Telegram for new messages, pass them to OpenClaw, and then send OpenClaw’s responses back to Telegram. This approach avoids exposing OpenClaw directly to the internet, which is a significant security benefit.

\n

Let’s create a new directory for our Telegram bridge script. On your VPS, navigate to your OpenClaw installation directory, typically ~/openclaw or /opt/openclaw. Then:

\n

mkdir -p ~/openclaw-telegram\ncd ~/openclaw-telegram\ntouch telegram_bridge.py\n

\n

Now, open telegram_bridge.py with your favorite editor (nano telegram_bridge.py) and paste the following Python code:

\n

import os\nimport time\nimport requests\nimport json\nimport subprocess\n\n# --- Configuration ---\nTELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN" # Replace with your bot token\nALLOWED_USER_ID = YOUR_TELEGRAM_USER_ID # Replace with your numeric user ID\nOPENCLAW_CLI_PATH = "/usr/local/bin/openclaw" # Adjust if openclaw is not in your PATH\nOPENCLAW_CONFIG_PATH = "~/.openclaw/config.json" # Adjust if your config is elsewhere\nPOLLING_INTERVAL_SECONDS = 5 # How often to check for new messages\n# --- End Configuration ---\n\ntelegram_api_base = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}"\nlast_update_id = 0\n\ndef get_updates():\n    global last_update_id\n    try:\n        params = {'offset': last_update_id + 1, 'timeout': 3}\n        response = requests.get(f"{telegram_api_base}/getUpdates", params=params)\n        response.raise_for_status()\n        updates = response.json()['result']\n        if updates:\n            last_update_id = max(u['update_id'] for u in updates)\n        return updates\n    except requests.exceptions.RequestException as e:\n        print(f"Error fetching Telegram updates: {e}")\n        return []\n\ndef send_message(chat_id, text):\n    try:\n        params = {'chat_id': chat_id, 'text': text, 'parse_mode': 'Markdown'}\n        response = requests.post(f"{telegram_api_base}/sendMessage", data=params)\n        response.raise_for_status()\n    except requests.exceptions.RequestException as e:\n        print(f"Error sending Telegram message: {e}")\n\ndef run_openclaw(prompt):\n    try:\n        # Pass model and config explicitly for robustness\n        # Using claude-haiku-4-5 is often 10x cheaper than default Opus/Sonnet and sufficient.\n        # Adjust --model and --config as needed.\n        cmd = [OPENCLAW_CLI_PATH, "chat", "--prompt", prompt, \n               "--model", "claude-haiku-4-5", \n               "--config", os.path.expanduser(OPENCLAW_CONFIG_PATH)]\n        \n        print(f"Running OpenClaw command: {' '.join(cmd)}")\n        process = subprocess.run(cmd, capture_output=True, text=True, check=True)\n        return process.stdout.strip()\n    except subprocess.CalledProcessError as e:\n        print(f"OpenClaw command failed: {e}")\n        print(f"Stderr: {e.stderr}")\n        return f"Error: OpenClaw failed to respond. Details: {e.stderr.strip()}"\n    except FileNotFoundError:\n        return f"Error: OpenClaw CLI not found at {OPENCLAW_CLI_PATH}. Please check the path."\n    except Exception as e:\n        return f"An unexpected error occurred while running OpenClaw: {e}"\n\ndef main():\n    print("OpenClaw Telegram bridge started...")\n    while True:\n        updates = get_updates()\n        for update in updates:\n            if 'message' in update and 'text' in update['message']:\n                message = update['message']\n                chat_id = message['chat']['id']\n                user_id = message['from']['id']\n                text = message['text']\n\n                if user_id != ALLOWED_USER_ID:\n                    print(f"Received message from unauthorized user {user_id} in chat {chat_id}: {text}")\n                    send_message(chat_id, "Sorry, I am a private bot and can only respond to my owner.")\n                    continue\n\n                print(f"Received message from {user_id} in chat {chat_id}: {text}")\n                send_message(chat_id, "_Thinking..._") # Provide immediate feedback\n\n                response = run_openclaw(text)\n                send_message(chat_id, response)\n            \n        time.sleep(POLLING_INTERVAL_SECONDS)\n\nif __name__ == "__main__":\n    main()\n

\n

Crucial step: Replace "YOUR_TELEGRAM_BOT_TOKEN" with the token you got from BotFather and YOUR_TELEGRAM_USER_ID with your numeric User ID. Make sure OPENCLAW_CLI_PATH points to your actual OpenClaw executable (you can find it by running which openclaw). The default ~/.openclaw/config.json usually works, but verify its location.

\n

A non-obvious insight here: while the OpenClaw documentation might suggest using the default model for various tasks, models like claude-haiku-4-5 (or even gpt-3.5-turbo if you’re using OpenAI) are often 10x cheaper and perfectly sufficient for 90% of interactive chat tasks. For a 24/7 assistant, cost efficiency is paramount. I’ve explicitly set --model claude-haiku-4-5 in the script for this reason.

\n

This setup works best on a VPS with at least 2GB RAM. While OpenClaw itself is relatively light, the underlying LLM calls and Python process will consume some resources. A Raspberry Pi might struggle, especially if you’re running other services or requesting very long completions.

\n

Making it Persistent with Systemd

\n

To ensure your Telegram bridge runs continuously and restarts automatically after crashes or reboots, we’ll use systemd. Create a service file:

\n

sudo nano /etc/systemd/system/openclaw-telegram.service\n

\n

Paste the following content, adjusting the paths for User, WorkingDirectory, and ExecStart to match your user and the script’s location:

\n

[Unit]
\nDescription=OpenClaw Telegram Bridge
\nAfter=network.target

\n

[Service]
\nUser=your_username # e.g., 'ubuntu', 'root', or your specific user
\nWorkingDirectory=/home/your_

\n\n

Frequently Asked Questions

\n

\n

What is OpenClaw and what does this integration achieve?

OpenClaw is an AI system. Connecting it to Telegram allows you to access its AI assistance 24/7 directly from your chat app, providing instant support and information whenever you need it.

\n

Why should I connect OpenClaw to Telegram for AI assistance?

This integration provides continuous, round-the-clock AI support directly within your Telegram chats. It offers unparalleled convenience, allowing you to leverage OpenClaw's capabilities for instant help, information, or task execution anytime, anywhere.

\n

What do I need to prepare before connecting OpenClaw to Telegram?

To get started, you'll typically need an active OpenClaw instance or account, a Telegram account, and potentially a Telegram Bot API token. The article will provide detailed steps for configuration and setup.

\n\n

? Get the OpenClaw Automation Starter Kit (9) →
Instant download — no subscription needed

Want to script OpenClaw with Python? See how to use the OpenClaw Python SDK for task automation →

Related: How to Connect OpenClaw to Telegram, Discord, WhatsApp, and Signal (2026 Guide)

Related: How to Connect OpenClaw to Telegram — Full Setup Guide

Related: How to Connect OpenClaw to Telegram, Discord, WhatsApp, and Signal (2026 Guide)

Related: How to Connect OpenClaw to Telegram — Full Setup Guide

Related: How to Connect OpenClaw to Telegram, Discord, WhatsApp, and Signal (2026 Guide)

Related: How to Connect OpenClaw to Telegram — Full Setup Guide

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *