Host a Discord or Telegram bot on Railway with Python
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 RailwayWhen 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:
- a community bot with FAQ tags and reminders on Discord,
- a personal or team assistant with notes and reminders on Telegram,
- notifications from your own apps, moderation helpers or integrations with your APIs,
- a bot that calls other services in your Railway project over the private network.
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
- In the Discord Developer Portal, create an application, open Bot, click Reset Token and copy the token.
- Paste it into
DISCORD_TOKENwhen you deploy, or set it on the Bot service later and redeploy. - The deploy log prints
Logged in as ...and, while the bot is in no server, an invite link with thebotandapplications.commandsscopes. Open it to add the bot to your server. - Type
/in your server. Global commands can take a few minutes to appear the first time. SetDISCORD_GUILD_IDto 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
- In Telegram, open @BotFather, send
/newbot, choose a name and a username, and copy the token. - Paste it into
TELEGRAM_BOT_TOKENwhen you deploy, or set it later and redeploy. - 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.
- Discord: add a module under
app/cogs/with anasync def setup(bot)function, list it inEXTENSIONSinapp/bot.py, and add tables toSCHEMAinapp/db.py. - Telegram: add handlers in
register()inapp/handlers.pyand tables inSCHEMAinapp/db.py. Plain text in a private chat goes to theechohandler, the place for your own logic. The database pool iscontext.bot_data["pool"].
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
- discord.py: MIT. Bots must follow the Discord Developer Terms of Service and Developer Policy.
- python-telegram-bot: LGPL-3.0-only, installed unmodified from PyPI and used as a library. Bots must follow Telegram’s terms for bot developers.
- Starter code: MIT, written for the templates.
- Not included, on purpose: music or YouTube playback, selfbots and user-token features, userbots, MTProto proxies, spam and mass-messaging tools. They break Discord’s, Telegram’s or Railway’s terms, including Railway’s Fair Use Policy.
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.