Boty Telegram2 minuty czytania

Host a python-telegram-bot app and preserve conversation state

Run a python-telegram-bot Application on a polling worker and check whether conversation data survives a restart.

Use the Application as the polling process

Wybierz Bot polling Python on Hosting botów Telegram. Pin the python-telegram-bot version you developed against in requirements.txt. Upload the entry file, save TELEGRAM_BOT_TOKEN in Variables, and call application.run_polling() from the entry point.

Start the deployment, send a private command with a known reply, and verify it again after restarting. If imports refer to the wrong telegram module, check that the dependency is python-telegram-bot and your project has no top-level telegram.py shadowing it.

Test whether a value survives restart

For a single worker with a trusted persistent directory, the framework’s PicklePersistence can save framework-managed data. Supply its path to the application builder and keep the file private. Use a file written by this deployment, since unpickling a file from an untrusted source can run code. For shared state across workers, use a database-backed persistence strategy.

Attach a persistence backend
from telegram.ext import Application, PicklePersistence

persistence = PicklePersistence(filepath="bot-state.pkl")
application = Application.builder().token(token).persistence(persistence).build()

Change a known preference through the bot, stop the worker gracefully, then start it and read the same preference. Check the state path and working directory if it resets. A persistent ConversationHandler also needs a stable name and persistent=True.

Check startup and shutdown behavior

For a standalone worker, let run_polling() control the application lifecycle. If another async server already owns the event loop, use the library’s manual lifecycle methods and deploy that HTTP service with the appropriate profile.

Application lifecycle reference i PicklePersistence reference.