Telegram-Bots1 Minute Lesezeit

Wie hoste ich einen aiogram- oder python-telegram-bot-Bot rund um die Uhr?

Betreibe einen Python-Telegram-Bot mit aiogram oder python-telegram-bot auf einem ständig laufenden Worker: Dateien, Abhängigkeiten, Token-Variable und eine erste Antwort.

Führe ihn als Polling-Worker aus

Nutze einen Host, der einen Python-Prozess am Laufen hält und deine Requirements installiert. Wähle auf Bothosting das Profil für einen Python-Polling-Bot, lade main.py und requirements.txt hoch, speichere das BotFather-Token als TELEGRAM_BOT_TOKEN und starte ihn. Domain oder offener Port sind nicht nötig. Spark führt ihn kostenlos mit 256 MB RAM aus und erneuert ihn alle 7 Tage.

aiogram 3

requirements.txt
aiogram>=3.13
main.py
import asyncio, os
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart
from aiogram.types import Message

dp = Dispatcher()

@dp.message(CommandStart())
async def start(message: Message):
    await message.answer('Hosted and running.')

async def main():
    bot = Bot(os.environ['TELEGRAM_BOT_TOKEN'])
    await dp.start_polling(bot)

asyncio.run(main())

python-telegram-bot 21+

requirements.txt
python-telegram-bot>=21
main.py
import os
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text('Hosted and running.')

app = Application.builder().token(os.environ['TELEGRAM_BOT_TOKEN']).build()
app.add_handler(CommandHandler('start', start))
app.run_polling()

Starte ihn und prüfe die Antwort

  1. Erstelle das Deployment mit dem Python-Polling-Bot-Profil. Lade beide Dateien ins Stammverzeichnis und setze MAIN_FILE auf main.py.
  2. Speichere TELEGRAM_BOT_TOKEN in den Variablen und starte das Deployment.
  3. Stoppe jede Kopie auf deinem Laptop und schick dann /start vom Handy.

Wenn nichts ankommt und das Log einen Konfliktfehler zeigt, nutzt ein anderer Prozess oder ein Webhook das Token. Siehe Wechsel vom Webhook zum Polling. Damit der Zustand Neustarts übersteht, siehe wie du Unterhaltungen behältst.