Telegram bot with AI
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).
- 1Create the bot
In @BotFather send
/newbot, pick a name and a username ending inbot(e.g.my_helper_bot). You'll get a token123456789:AA...— never share it. - 2Create 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. - 3Install the libraries
Create an
ai-botfolder and open a terminal in it.Python:
bashpython3 -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate pip install "python-telegram-bot>=21,<22" openaiNode.js:
bashnpm init -y npm install grammy @grammyjs/runner openai - 4Save the code
Copy the code from "Full code" below into
bot.py(Python) orbot.mjs(Node.js) in theai-botfolder. - 5Start it
macOS and Linux:
bashexport TELEGRAM_BOT_TOKEN="123456789:AA..." export ZUKKO_API_KEY="fetch_..." python bot.py # or: node bot.mjsWindows (PowerShell):
bash$env:TELEGRAM_BOT_TOKEN="123456789:AA..." $env:ZUKKO_API_KEY="fetch_..." python bot.pyYou'll see a "bot started" line. Keep the window open: the bot answers only while the program runs.
- 6Check
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.targetFor 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:
sudo systemctl daemon-reload
sudo systemctl enable --now ai-bot
journalctl -u ai-bot -f # live bot log3If it doesn't work
| What you see | What 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 request | The bot runs twice — keep one copy |
| "Can't answer right now", the log shows 401 / 402 / 403 | 401 — 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_reached | Raise the key limit or wait until 00:00 UTC |
| The bot is slow | Use a fast model: claude-haiku-5-5 or deepseek-v4.1-flash |
Other codes: API errors.