How to host a Telegram bot 24/7
Keep a Telegram bot online with python-telegram-bot: get a token from BotFather, upload and start.
Telegram bots answer only while their code is running. This guide hosts a Python bot with python-telegram-bot so it replies 24/7. The same steps work for Node.js libraries such as Telegraf or grammY.
1. Get a token from BotFather
- In Telegram, open @BotFather and send /newbot.
- Choose a name and a username ending in "bot".
- Copy the token BotFather sends you.
2. The code
python-telegram-botimport os
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I'm online 24/7.")
app = ApplicationBuilder().token(os.environ["TELEGRAM_TOKEN"]).build()
app.add_handler(CommandHandler("start", start))
print("Bot is running")
app.run_polling()Use polling (run_polling), not webhooks. Bots on Marv Hosting connect out to Telegram but don't accept incoming connections.
3. Upload and start
- Zip requirements.txt and main.py.
- In the panel, create a bot with Python 3.12.
- Upload the .zip in the Deploy tab.
- Add TELEGRAM_TOKEN in the Environment tab.
- Press Start and send /start to your bot.