Eseguilo come worker in polling
Usa un host che tiene in esecuzione un processo Python e installa i tuoi requirements. Su Bothosting, scegli il profilo bot Python in polling, carica main.py e requirements.txt, salva il token di BotFather come TELEGRAM_BOT_TOKEN e avvialo. Non servono un dominio né una porta aperta. Spark lo esegue gratis con 256 MB di RAM, da rinnovare ogni 7 giorni.
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()Avvialo e controlla la risposta
- Crea il deploy con il profilo di bot Python in polling. Carica entrambi i file nella root e imposta
MAIN_FILEsumain.py. - Salva
TELEGRAM_BOT_TOKENin Variabili e avvia il deploy. - Ferma ogni copia sul laptop, poi invia
/startdal telefono.
Se non arriva nulla e il log mostra un errore di conflitto, un altro processo o un webhook sta usando il token. Vedi il passaggio dal webhook al polling. Per conservare lo stato fra un riavvio e l’altro, vedi ricordare le conversazioni.