How to Build a Self-Hosted Telegram AI Bot with DeepSeek-R1 (2026)
In 2026, user privacy and data sovereignty have become top priorities for developers and tech enthusiasts worldwide. Traditional cloud-based AI messaging bots, while powerful, routinely harvest chat histories, share user data with advertisers, and lock advanced reasoning models behind restrictive subscription paywalls. To combat this, a massive movement toward self-hosted, decentralized solutions has emerged. At the forefront of this shift is running DeepSeek-R1—the industry-leading open-source reasoning model—locally on your own hardware and bridging it to Telegram. By leveraging the Telegram Bot API and Ollama, you can deploy a completely private, uncensored, and zero-cost AI assistant accessible from any of your devices. This comprehensive, step-by-step guide walks you through building and self-hosting your own Telegram AI bot using DeepSeek-R1 and Python in 2026.
To build a self-hosted Telegram AI bot, you need to run **Ollama** locally, pull the **DeepSeek-R1** model, obtain a bot token from **@BotFather**, and run a lightweight **Python script** using the `python-telegram-bot` framework. For pre-configured bots and verified technical tools, explore Telekit's Science & Technology Directory.
What is DeepSeek-R1 and Why Deploy It Locally on Telegram?
DeepSeek-R1 is a state-of-the-art open-source reasoning model that matches or exceeds proprietary models in math, coding, and logical reasoning tasks. Unlike standard chat models, DeepSeek-R1 employs a chain-of-thought (CoT) reasoning process, allowing it to think through complex problems step-by-step before producing a final output. Running this model locally using Ollama guarantees that your conversational data never leaves your computer, providing complete privacy. By interfacing Ollama with Telegram, you gain a personal AI assistant that is always on, responsive, and available in your favorite chat client.
Prerequisites and Hardware Requirements
Before beginning the installation, ensure your host system meets the necessary hardware requirements. Since you will be running the AI model locally, performance is directly tied to your computer's resources:
- For the 1.5B/7B Model: A minimum of 8 GB of RAM (16 GB recommended) and a modern CPU or Apple Silicon chip.
- For the 8B/14B Model: 16 GB of system RAM or dedicated VRAM (e.g., NVIDIA RTX 3060/4060 or higher).
- For the 32B/70B Model: 32 GB to 64 GB of RAM/VRAM, ideally running on dedicated workstation hardware or server nodes.
- Software: Python 3.10 or higher installed, and a terminal environment with administrator privileges.
Step 1: Installing Ollama and Downloading DeepSeek-R1
Ollama is the premier engine for running large language models locally. It abstracts away complex model loading, quantization, and CUDA configurations into a simple command-line interface. Follow these terminal steps to install Ollama and download DeepSeek-R1:
1. Open your terminal and execute the following installation command for Linux/macOS, or download the installer directly from the official website for Windows systems:
# For Linux systems:
curl -fsSL https://ollama.com/install.sh | sh
2. Once Ollama is installed and running in the background, pull the DeepSeek-R1 reasoning model to your local machine. We will use the optimized 8B model version for this guide, which offers an excellent balance between reasoning capabilities and speed on consumer hardware:
# Download the 8-billion parameter DeepSeek-R1 model
ollama pull deepseek-r1:8b
3. Verify that the model is loaded and working by executing a direct prompt query in your terminal:
# Run the model locally in interactive mode
ollama run deepseek-r1:8b "Why is the sky blue?"
Step 2: Creating Your Telegram Bot with BotFather
To hook up our local model to the Telegram interface, we must create a registered bot entity and obtain an API token. Telegram's official bot creator, BotFather, manages this process:
- Open the Telegram application on your desktop or mobile device.
- Search for the verified username @BotFather and start a chat session.
- Send the command
/newbotto initiate the setup wizard. - Follow the prompts to enter a display name (e.g.,
My Local AI Assistant) and a unique username ending in_bot(e.g.,DeepSeekLocalAssistant_bot). - Copy the HTTP API Token provided in the success message. Keep this token highly secure, as it controls access to your bot.
Step 3: Setting Up the Python Development Environment
With our AI engine and Telegram bot registered, we will build a Python bridge script to route messages between Telegram and Ollama. First, create a new project directory and set up a clean Python virtual environment to manage dependencies:
# Create and navigate to the project folder
mkdir local-telegram-ai
cd local-telegram-ai
# Initialize Python virtual environment
python -m venv venv
# Activate the virtual environment
# On Linux/macOS:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate
Next, install the required packages. We need python-telegram-bot for the Telegram client loop and httpx to handle local HTTP requests to Ollama's API:
# Install project dependencies
pip install python-telegram-bot httpx
Step 4: Writing the Telegram AI Bot Script
Now, create a new file named bot.py in your project directory. This Python script initializes the Telegram client, listens for incoming text messages, queries the local Ollama instance, and streams the reasoning steps and final answer back to the user. Copy the following code configuration:
import logging
import httpx
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters, CommandHandler
# Enable logging to track errors
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
# Configuration Constants
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN_HERE"
OLLAMA_API_URL = "http://localhost:11434/api/generate"
MODEL_NAME = "deepseek-r1:8b"
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Send welcome message on /start command."""
welcome_text = (
"🤖 Hello! I am your private, self-hosted DeepSeek-R1 AI Assistant.\n"
"Send me a message and I will think through the response locally."
)
await update.message.reply_text(welcome_text)
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle incoming text messages and query local DeepSeek model."""
user_message = update.message.text
chat_id = update.message.chat_id
# Send a typing indicator to let the user know the AI is thinking
await context.bot.send_chat_action(chat_id=chat_id, action="typing")
# Initial status message
status_msg = await update.message.reply_text("🤔 Thinking...")
try:
# Prepare request payload for Ollama
payload = {
"model": MODEL_NAME,
"prompt": user_message,
"stream": False
}
# Query Ollama REST API asynchronously
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.post(OLLAMA_API_URL, json=payload)
if response.status_code == 200:
result = response.json()
ai_response = result.get("response", "Error: No response generated.")
# Format response and update thinking message
# Note: DeepSeek-R1 puts thinking process inside ... tags.
# You can style or extract this process.
if "<think>" in ai_response:
parts = ai_response.split("</think>")
thinking_process = parts[0].replace("<think>", "").strip()
final_answer = parts[1].strip()
formatted_reply = f"💭 Thought Process:\n{thinking_process}\n\n🤖 Answer:\n{final_answer}"
else:
formatted_reply = ai_response
await status_msg.edit_text(formatted_reply, parse_mode="HTML")
else:
await status_msg.edit_text("❌ Error: Failed to communicate with Ollama server.")
except httpx.ReadTimeout:
await status_msg.edit_text("⏱️ Error: Ollama timeout. Reasoning took longer than 120 seconds.")
except Exception as e:
await status_msg.edit_text(f"❌ System Error: {str(e)}")
def main():
# Build application
app = ApplicationBuilder().token(TELEGRAM_BOT_TOKEN).build()
# Register command and message handlers
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
# Run the bot
print("Self-hosted Telegram bot is running...")
app.run_polling()
if __name__ == '__main__':
main()
Replace YOUR_TELEGRAM_BOT_TOKEN_HERE with the token you copied from BotFather in Step 2. Run the script using Python:
python bot.py
Step 5: Production Deployment and Always-On Setup
To keep your bot running 24/7 without keeping your terminal window open, you should set it up as a system service. On Linux servers (such as Ubuntu), you can create a simple systemd service:
- Create a service definition file:
- Add the following configuration, ensuring you adjust paths to match your system username:
- Reload systemd, enable the service to start automatically on system boot, and launch the service:
- Check the service logs to ensure it's functioning correctly:
sudo nano /etc/systemd/system/telegram-ai.service
[Unit]
Description=Self-Hosted Telegram AI Assistant
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/local-telegram-ai
ExecStart=/home/ubuntu/local-telegram-ai/venv/bin/python bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable telegram-ai.service
sudo systemctl start telegram-ai.service
sudo journalctl -u telegram-ai.service -f
Frequently Asked Questions (FAQ)
Why is the bot taking so long to respond to messages?
Local AI generation speed is limited by your hardware's computing power. If your CPU or GPU does not have enough VRAM/RAM to load the active model parameters, generation slows down. You can resolve this by downloading a smaller version of DeepSeek-R1, such as the 1.5B parameter variant (ollama pull deepseek-r1:1.5b).
Can multiple Telegram users interact with my bot simultaneously?
Yes, the bot can handle messages from multiple users. However, because Ollama runs queries sequentially by default on your local system, simultaneous requests will queue up and process one after another, which might increase the response time for subsequent users.
How do I implement conversational memory for my Telegram bot?
By default, the script processes each message as a standalone query. To implement memory, you must maintain a chat history database (like SQLite) or a local python dictionary grouped by chat_id, storing the last few messages and sending them as a conversation list to the Ollama endpoint.
Is my conversational data secure when using this bot?
Absolutely. Since Ollama is running locally on your hardware, no conversation data is sent to external cloud APIs or third parties. Telegram's servers route the messages between the user and your host server, but the processing is completely local.
Conclusion
Deploying a self-hosted Telegram AI bot using DeepSeek-R1 and Ollama is a powerful way to reclaim your data privacy while accessing state-of-the-art reasoning capabilities for free. By running the bot locally and managing it with a system service, you ensure 24/7 uptime for your personal, uncensored assistant. Ready to explore more integrations or browse other verified Telegram tools and channels? Head over to Telekit's Gaming & Apps Section or visit Telekit's Business & Marketing catalog to connect with global developers today!
No active reviews. Be the first to add one!