Save the next step before the next message arrives
Store conversation state somewhere the bot can reopen after startup. For a small Telegram bot on a single deployment, that can be a local SQLite file. A database is useful when a separate bot, website or worker must read the same records. A map in memory is convenient for a demonstration, but all entries disappear when the process exits.
Choose the key to match the feature. For the language of a whole group, the chat ID makes sense. For two people filling in separate forms in that group, use both the chat ID and the sender’s user ID. A username can change, and the group’s ID alone would put both people in the same form.
Walk through a two-message signup
Imagine a class-booking bot. When a user selects a class, write its ID and the next expected answer before asking for a preferred date. When the date arrives, read the saved step, validate the answer and record the transition. If the user sends a date after the form has already closed, offer a fresh start instead of creating another booking.
The Python Telegram starter keeps recently processed update IDs in .telegram-state. That helps with duplicate delivery. It does not contain your class selection, preferred date or booking records. Give that application data its own file or database schema.
Test two users and a restart
- Start independent signups from two accounts in a test group after enabling the appropriate group handler. Select a different class from each account.
- Restart the hosted bot between the class and date steps. Finish each signup, then verify that each date belongs to the chosen class and user.
- Back up the database using a method appropriate for the engine. For a local database in deployment files, stop writes at a clean point before a file backup. Restore a test copy and read a known booking before depending on it.
See the file recovery guide or compare standalone database engines for shared state.