Usa il polling, a meno che tu non abbia un motivo per evitarlo
Usa il long polling per la maggior parte dei bot Telegram. Non serve un dominio, una porta aperta né un certificato TLS, e funziona su un worker gratis sempre acceso. Usa un webhook quando il bot vive già dentro una web app, quando ti aspetti tanto traffico, o quando la tua piattaforma si sveglia solo per richieste HTTP. Telegram consegna un solo modo per token. Finché un webhook è impostato, getUpdates restituisce un errore.
Polling e webhook fianco a fianco
| Long polling | Webhook | |
|---|---|---|
| Come arrivano gli aggiornamenti | Il tuo bot chiama getUpdates e aspetta | Telegram invia un POST HTTPS al tuo URL |
| Serve un URL pubblico | No | Sì, HTTPS sulla porta 443, 80, 88 o 8443 |
| Processo | Worker sempre attivo | Server web; può dormire tra una richiesta e l’altra |
| Librerie tipiche | aiogram, python-telegram-bot, grammY, Telegraf | Le stesse librerie in modalità webhook, oppure FastAPI ed Express |
| Profilo Bothosting | Bot di polling Node.js o Python | Bot webhook Node.js o Python |
Telegram documenta entrambi i metodi nella Bot API.
Eseguire ogni modalità su Bothosting
Per il polling, scegli un profilo di polling, salva TELEGRAM_BOT_TOKEN e avvia. Non serve altro. Per un webhook, scegli un profilo webhook. Imposta TELEGRAM_WEBHOOK_URL sull’indirizzo HTTPS del deploy che finisce con /telegram, e imposta TELEGRAM_WEBHOOK_SECRET così il bot può rifiutare le richieste che non arrivano da Telegram. Un sito Spark gratuito può usare anche il tuo dominio per il webhook. Vedi i domini webhook personalizzati.
Passare senza perdere messaggi
Ferma prima il deploy precedente. Nel passaggio al polling, chiama deleteWebhook lasciando drop_pending_updates a false, così i messaggi in coda arrivano ancora. Poi avvia il poller. La versione passo passo: il passaggio dal webhook al polling. Per le opzioni di hosting, vedi il confronto sull’hosting gratis di bot Telegram.