Execute como um worker de polling
Use um host que mantenha um processo Python em execução e instale os seus requirements. Na Bothosting, escolha o perfil de bot Python em polling, envie main.py e requirements.txt, salve o token do BotFather como TELEGRAM_BOT_TOKEN e inicie. Não é preciso domínio nem porta aberta. O Spark executa grátis com 256 MB de RAM, renovado a cada 7 dias.
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()Inicie e confira a resposta
- Crie o deploy com o perfil de bot Python em polling. Envie os dois arquivos para a raiz e defina
MAIN_FILEcomomain.py. - Salve
TELEGRAM_BOT_TOKENem Variáveis e inicie o deploy. - Pare qualquer cópia no notebook e depois envie
/startpelo celular.
Se nada chegar e o log mostrar um erro de conflito, outro processo ou um webhook está usando o token. Veja a mudança de webhook para polling. Para manter o estado entre reinícios, veja lembrar conversas.