Deploy Telegram Bot Python | Starter Bot with Optional AI Chat

Python Telegram bot starter: commands, buttons, optional AI replies

Deploy Telegram Bot Python | Starter Bot with Optional AI Chat

Just deployed

Deploy and Host a Telegram Bot (Python) on Railway

Deploy on Railway

A Python Telegram bot that is running a minute after you paste its token: commands, inline buttons, a user allowlist, and optional AI chat through any OpenAI-compatible API (OpenAI, OpenRouter, Groq, DeepSeek, a self-hosted model). It's one short file built on python-telegram-bot, meant to be ejected and made your own.

About Hosting a Telegram Bot

The bot uses long polling: it asks Telegram for new messages, so it needs no public domain, webhook or certificate. One small service, no database.

  • Without an AI key it echoes what you send, so you can see it working first.
  • With OPENAI_API_KEY set it chats with the model, keeping the last 20 messages of each chat as context.
  • A missing or wrong token doesn't crash-loop the service: the bot waits and says in its logs what to fix.

Common Use Cases

  • A personal AI assistant in Telegram, on any model you choose
  • A starting point for a support, notification or community bot
  • A private bot for a family or team, locked to listed user IDs
  • Learning bot development with a working deploy from minute one

Dependencies for Telegram Bot Hosting

  • A bot token from @BotFather (send /newbot, pick a name, copy the token).
  • Optional: an API key for an OpenAI-compatible provider.

Deployment Dependencies

Implementation Details

First use: paste the token as TELEGRAM_BOT_TOKEN, deploy, then open your bot in Telegram and send /start.

Commands: /start, /help, /id (shows your Telegram user ID) and /reset (clears the AI's memory of this chat).

AI chat: set OPENAI_API_KEY, then OPENAI_BASE_URL and OPENAI_MODEL for your provider:

ProviderOPENAI_BASE_URLOPENAI_MODEL example
OpenAIhttps://api.openai.com/v1gpt-4o-mini
OpenRouterhttps://openrouter.ai/api/v1anthropic/claude-sonnet-4.5
Groqhttps://api.groq.com/openai/v1llama-3.3-70b-versatile
DeepSeekhttps://api.deepseek.comdeepseek-chat

Change the bot's personality with SYSTEM_PROMPT.

Lock it down: anyone who finds your bot can use it, and your AI key pays for it. Send /id to the bot, then put your ID (comma-separated for several people) in ALLOWED_USER_IDS.

Make it yours: eject the template in the service's settings to get your own copy of the repo. Add handlers in main() in bot.py; every push redeploys.

Notes and limits:

  • A bot token can be polled by one process at a time. Running the same token elsewhere (your laptop) makes both report Conflict errors.
  • Chat memory is kept in RAM and is cleared on each redeploy.
  • Messages sent while the bot is down are skipped on start, so it doesn't answer a backlog.

Why Deploy a Telegram Bot on Railway?

Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.

By deploying a Telegram bot on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.


Template Content

More templates in this category

View Template
Swarm
Named LLM bots join channels, take jobs, run routines on your Railway box

mcmax
3
View Template
Telegram JavaScript Bot
A template for Telegram bot in JavaScript using grammY

Agampreet Singh
294
View Template
Cobalt Tools [Updated Oct ’26]
Cobalt Tools [Oct ’26] (Media Downloader, Converter & Automation) Self Host

shinyduo
308