DocsTelegram bot with AI

Telegram bot with AI

Use casesBeginner15 min

In 15 minutes you'll have a Telegram bot that answers with an AI model and remembers the last 10 messages of each chat.

You need: a Telegram account, a fetch_... key (create one), a balance from $0.01 (top up), Python 3.10+ or Node.js 18+ (not sure — pick Python).

  1. 1
    Create the bot

    In @BotFather send /newbot, pick a name and a username ending in bot (e.g. my_helper_bot). You'll get a token 123456789:AA... — never share it.

  2. 2
    Create a key for the bot

    In Dashboard → API keys create a separate key. Set a limit, say $1 a day, and allow only claude-haiku-5-5 — how.

  3. 3
    Install the libraries

    Create an ai-bot folder and open a terminal in it.

    Python:

    bash
    python3 -m venv .venv
    source .venv/bin/activate      # Windows: .venv\Scripts\activate
    pip install "python-telegram-bot>=21,<22" openai

    Node.js:

    bash
    npm init -y
    npm install grammy @grammyjs/runner openai
  4. 4
    Save the code

    Copy the code from "Full code" below into bot.py (Python) or bot.mjs (Node.js) in the ai-bot folder.

  5. 5
    Start it

    macOS and Linux:

    bash
    export TELEGRAM_BOT_TOKEN="123456789:AA..."
    export ZUKKO_API_KEY="fetch_..."
    python bot.py        # or: node bot.mjs

    Windows (PowerShell):

    bash
    $env:TELEGRAM_BOT_TOKEN="123456789:AA..."
    $env:ZUKKO_API_KEY="fetch_..."
    python bot.py

    You'll see a "bot started" line. Keep the window open: the bot answers only while the program runs.

  6. 6
    Check

    Find the bot in Telegram, tap Start and ask something.

1Full code

The 10-message memory lives in RAM and is wiped on restart. /reset clears it manually.

import asyncio
import logging
import os
from collections import defaultdict, deque

from openai import APIConnectionError, APIStatusError, AsyncOpenAI
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logging.getLogger("httpx").setLevel(logging.WARNING)  # otherwise the log shows URLs containing the bot token
log = logging.getLogger("bot")

2Running 24/7

Copy the ai-bot folder to a Linux VPS, install the libraries and set up autostart.

# /etc/systemd/system/ai-bot.service
[Unit]
Description=Telegram AI bot
After=network-online.target
Wants=network-online.target

[Service]
User=bot
WorkingDirectory=/home/bot/ai-bot
EnvironmentFile=/home/bot/ai-bot/.env
ExecStart=/home/bot/ai-bot/.venv/bin/python bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

For systemd, create a .env file next to the bot with TELEGRAM_BOT_TOKEN=... and ZUKKO_API_KEY=... (no export, no quotes), run chmod 600 .env and enable the service:

bash
sudo systemctl daemon-reload
sudo systemctl enable --now ai-bot
journalctl -u ai-bot -f      # live bot log

3If it doesn't work

What you seeWhat to do
KeyError: 'TELEGRAM_BOT_TOKEN'Run the export ... lines in the same window where you start the bot
ModuleNotFoundError: No module named 'telegram'source .venv/bin/activate, then pip install again
Conflict: terminated by other getUpdates requestThe bot runs twice — keep one copy
"Can't answer right now", the log shows 401 / 402 / 403401 — check ZUKKO_API_KEY. 402 — top up. 403 model_not_allowed_for_key — allow the model in the key or change ZUKKO_MODEL
429 key_daily_limit_reachedRaise the key limit or wait until 00:00 UTC
The bot is slowUse a fast model: claude-haiku-5-5 or deepseek-v4.1-flash

Other codes: API errors.

Telegram bot with AI · Zukko Docs