Skip to content
Browse documentation

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

  1. In Telegram, open @BotFather and send /newbot.
  2. Choose a name and a username ending in "bot".
  3. Copy the token BotFather sends you.

2. The code

requirements.txt
python-telegram-bot
main.py
import 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

  1. Zip requirements.txt and main.py.
  2. In the panel, create a bot with Python 3.12.
  3. Upload the .zip in the Deploy tab.
  4. Add TELEGRAM_TOKEN in the Environment tab.
  5. Press Start and send /start to your bot.