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
aiogram>=3.13import 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+
python-telegram-bot>=21import 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
- Erstelle das Deployment mit dem Python-Polling-Bot-Profil. Lade beide Dateien ins Stammverzeichnis und setze
MAIN_FILEaufmain.py. - Speichere
TELEGRAM_BOT_TOKENin den Variablen und starte das Deployment. - Stoppe jede Kopie auf deinem Laptop und schick dann
/startvom 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.