ai
3 мин
22 ноября 2026 г.
Источник: Dev.to AI Feed

Build a Private Telegram AI Bot with Ollama and Docker on a VPS

Артем Ахмадеев
Артем Ахмадеев
RSS AI Ingest
Build a Private Telegram AI Bot with Ollama and Docker on a VPS

Cloud AI chatbots are convenient, but every message you send goes to someone else's servers. A public Telegram bot is also open to anyone who finds its username, which is a problem if it can read your server logs. You want a small assistant...

Cloud AI chatbots are convenient, but every message you send goes to someone else's servers. A public Telegram bot is also open to anyone who finds its username, which is a problem if it can read your server logs. You want a small assistant that lives on your own machine and answers only you. In this guide we build exactly that: a Telegram bot running in Docker on your VPS. It talks to a tiny local Ollama model, summarizes a server log on request, and ignores everyone except your Telegram ID. Self-hosting keeps your prompts and logs on your own server, costs nothing per request, and needs no GPU. What you need A VPS with Ubuntu 24.04 (2 vCPU / 4 GB RAM is plenty). I use VDSina (affiliate link — it supports the channel at no extra cost to you). An SSH connection to it (any terminal works). Short on time? Every command below is also in one ready-to-run script. Step 1: Update the system Updating the system refreshes the package list and installs pending upgrades. Starting from a current base avoids odd dependency problems later, and it can take a minute. apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y Step 2: Install Docker Docker is installed with the official script, which handles the whole setup at once. The bot will run in a container, so it stays isolated from the rest of the server and is easy to restart or remove. curl -fsSL https://get.docker.com | sh Step 3: Install Ollama Ollama is the tool that runs language models locally. We install zstd first because the installer needs it to unpack its files, then run the official script. By default Ollama listens only on the server itself, so it isn't exposed to the internet. DEBIAN_FRONTEND=noninteractive apt-get install -y zstd && curl -fsSL https://ollama.com/install.sh | sh Step 4: Pull a tiny model We pull Qwen 2.5 at 0.5 billion parameters. It is small enough to run on a modest VPS without a GPU. It won't be brilliant, but it handles short questions and quick summaries well. ollama pull qwen2.5:0.5b Step 5: Create the bot in BotFather We create a folder for the bot, then do the Telegram setup on your phone. BotFather creates the bot and gives you a token, and userinfobot tells you your numeric Telegram ID. You need both to lock the bot to yourself. mkdir -p /opt/tgbot echo "On your phone: open Telegram, message @BotFather, send /newbot, pick a name and a username ending in 'bot'." echo "Copy the token it gives you. Then message @userinfobot to get your numeric Telegram ID." Step 6: Save token and owner ID The token and your ID go into a small env file, replacing the two placeholders with your real values. We then restrict the file so only root can read it. The token controls your bot, so treat it like a password. BOT_TOKEN="123456789:PASTE_TOKEN_FROM_BOTFATHER" OWNER_ID="123456789" cat > /opt/tgbot/.env /opt/tgbot/bot.py /dev/null || true docker run -d --name tgbot --restart unless-stopped --network host \ --env-file /opt/tgbot/.env \ -v /opt/tgbot/bot.py:/app/bot.py:ro \ -v /var/log/dpkg.log:/host/dpkg.log:ro \ python:3.12-slim python -u /app/bot.py Step 9: Test chat and log summary On your phone, send /start, ask a simple question like what Docker is, then try /logs. The first reply can be slow while the model loads into memory. The curl check queries Ollama directly, helping you tell model problems from bot problems. echo "On your phone, open your bot, send: /start, then: What is Docker in one sentence?, then: /logs" curl -s http://127.0.0.1:11434/api/generate -d '{"model":"qwen2.5:0.5b","prompt":"Say hello in five words.","stream":false,"options":{"num_predict":30}}' Step 10: View bot logs The container logs show when the bot started, when your messages arrived, and any errors. If a stranger messages the bot, their ID appears there marked as ignored. This is the first place to look when something misbehaves. docker logs --tail 20 tgbot Common mistakes Leaving the placeholder token or ID in the env file, or pasting them with extra spaces or quotes, makes the bot fail or ignore you. Double-check both values and the container logs after starting. Exposing Ollama to the public internet by changing its listen address is risky because it has no authentication. Keep the default local-only setting and use host networking only for the bot. Expecting great answers from a 0.5B model leads to disappointment. Keep prompts short and log summaries modest, or move to a larger model if your VPS has the RAM. Wrap-up Once your bot works, try a larger model if your server has the memory, or point /logs at other files like nginx or auth logs. You can also add more commands, such as disk usage or service status, while keeping the owner-ID check in place. Every command here was tested on a fresh Ubuntu 24.04 server before publishing. Watch the full walkthrough on YouTube — new tutorials every week on Lamplight Servers.

Хотите внедрить ИИ в ваш бренд?

Спроектируем и развернем автономных агентов и современный цифровой стек под ваши задачи.

Рассчитать проект