Host a Discord or Telegram bot on Railway with Python

By Tracklayer · Updated 2026-11-24

Short answer: a chat bot is a long-running process that has to stay online, so it needs a host rather than your laptop. Tracklayer’s two starter templates give you a working Python bot on Railway as soon as you paste a token: a Discord bot on discord.py 2.7.1 with slash commands, and a Telegram bot on python-telegram-bot 22.8 that runs on webhooks. Both keep their data in a private Postgres 18 and are meant to be ejected and extended with your own code.

Deploy the Discord bot on Railway Deploy the Telegram bot on Railway

When a starter template makes sense

Use these templates when you want to write your own bot logic in Python and skip the hosting work: reconnects, health checks, storage, pinned dependencies and secrets. The READMEs list uses such as:

If you want an AI agent that chats on Telegram or Discord rather than a bot you program yourself, read the always-on AI agent guide instead.

What the templates deploy

Discord bot starter Telegram bot starter
Library discord.py 2.7.1 python-telegram-bot 22.8, behind an aiohttp server
Runtime python:3.13.18-slim-trixie, pinned by digest Same
How updates arrive WebSocket connection to Discord’s gateway Webhook at https://{your-domain}/telegram, polling as fallback
Public domain None; private /healthz only Yes, for the webhook and /healthz
Database PostgreSQL 18.6 with a volume PostgreSQL 18.6 with a volume
Commands included /help, /ping, /about, /remind, /tag /start, /help, /note, /notes, /delnote, /remind, /reminders, /cancel, /id, /ping, /stats

All Python dependencies are pinned, tables are created on first start, and Postgres is reachable only over the private network. Both libraries are covered on their app pages: discord.py and python-telegram-bot.

Set up the Discord bot

  1. In the Discord Developer Portal, create an application, open Bot, click Reset Token and copy the token.
  2. Paste it into DISCORD_TOKEN when you deploy, or set it on the Bot service later and redeploy.
  3. The deploy log prints Logged in as ... and, while the bot is in no server, an invite link with the bot and applications.commands scopes. Open it to add the bot to your server.
  4. Type / in your server. Global commands can take a few minutes to appear the first time. Set DISCORD_GUILD_ID to your server id for instant updates while developing.

The bot uses only default intents, so nothing has to be enabled under Privileged Gateway Intents. Slash commands are synced with Discord only when they change, and mentions in repeated text are disabled.

Set up the Telegram bot

  1. In Telegram, open @BotFather, send /newbot, choose a name and a username, and copy the token.
  2. Paste it into TELEGRAM_BOT_TOKEN when you deploy, or set it later and redeploy.
  3. The log prints Webhook set to https://... and that the bot runs in webhook mode. Open your bot in Telegram and send /start.

Telegram calls the webhook with a generated WEBHOOK_SECRET header, and every request without it gets 403. If Telegram cannot reach the domain after several tries, or there is no domain, the bot switches to polling. Set ADMIN_USER_IDS to your user id (shown by /id) to use /stats.

Add your own code

The deployed code comes from each template’s GitHub repository. Use Eject in the Bot service’s settings so Railway copies the repository to your GitHub account and deploys from it; every push then redeploys.

Keep tokens and API keys in service variables, not in code. The environment variables guide explains how Railway handles them.

Data, replicas and upgrades

Reminders, tags, notes and chats live in Postgres on a volume and survive redeploys; the bot services keep no state. For backup options, read Postgres backups on Railway.

Run one replica of each bot. A second Discord process with the same token would answer every command twice, and Telegram allows only one webhook or one poller per token, so use a separate test bot for local work. The Discord README says one process handles thousands of servers; very large bots need discord.py’s sharding. Versions are pinned in services/bot/requirements.txt and the Dockerfile of each repository; change them there to upgrade.

Cost

Railway bills memory and CPU by usage. Measured on Railway, the Discord bot idles at about 55 MB of RAM with Postgres at about 100 MB, and the Telegram bot at about 60 MB with Postgres at about 110 MB, all near 0 vCPU. The Trial plan is enough for either. The template author estimates roughly $1 to $2 a month per bot; treat that as an estimate. See Railway pricing for how plans and usage combine.

Terms and licences

Both are community templates published by Tracklayer. They are not affiliated with or endorsed by Discord Inc., Telegram, the discord.py or python-telegram-bot projects, or Railway.

Other options

For an AI agent you talk to on Telegram or Discord, read the always-on AI agent guide. For the library documentation, see discord.py and python-telegram-bot. More walkthroughs are listed under guides.

FAQ

What do I need before deploying?

A Railway account and a bot token. For Discord, create an application in the Discord Developer Portal, open Bot and click Reset Token. For Telegram, send /newbot to @BotFather. Paste the token into DISCORD_TOKEN or TELEGRAM_BOT_TOKEN when you deploy, or set it later and redeploy.

What happens if the token is missing or wrong?

The bot service stays up instead of crash-looping. Its /healthz endpoint reports not-configured or token-invalid, and the log says which variable to fix. The Telegram starter never writes the token to the log.

Is my bot's data kept across redeploys?

Yes. Both starters keep their data in a Railway Postgres with a volume: reminders and tags for Discord, chats, notes and reminders for Telegram. Reminders are still delivered after a redeploy. The bot services themselves keep no state.

How much does a bot cost on Railway?

Measured on Railway, the Discord bot idles at about 55 MB of RAM and the Telegram bot at about 60 MB, with Postgres at about 100 to 110 MB and near-zero CPU. The Trial plan is enough, and the template author estimates roughly $1 to $2 a month for either.

What licences and terms apply?

discord.py is MIT and python-telegram-bot is LGPL-3.0-only; the starters' own code is MIT. Bots must follow Discord's Developer Terms of Service and Developer Policy or Telegram's terms for bot developers, and Railway's Fair Use Policy. These are community templates, not affiliated with Discord, Telegram, either library project or Railway.