Telegram as a task control panel: what a message can authorize

A narrow action vocabulary, immutable plans, current permissions and single-use confirmation separate a convenient chat from executor authority.

One key and one prepared control box suggest a deliberate confirmation of a specific action.
One key and one prepared control box suggest a deliberate confirmation of a specific action.

A message saying “refresh the index” is convenient on a phone, but it does not adequately describe an action. Which index, in which project, with which parameters, and based on what state? If a bot immediately forwards the text to an executor, the conversation becomes a second, informal administration interface. Permissions, confirmation content, and redelivery behaviour can easily diverge.

A more useful control panel has a small vocabulary of actions. It shows state, prepares a specific plan, lets a person review it, and accepts confirmation of that exact plan. We will examine this model using a fictional notes catalogue. Its only action updates a synthetic count of prepared previews in MySQL 8.4/InnoDB through a native PyMySQL TCP connection. The example is not connected to Telegram, launches no shell, and changes no real website.

Start with an action catalogue

“Execute whatever I write” provides no useful application boundary. Instead of an arbitrary string, we need named operations with defined inputs and results. The exercise deliberately keeps the catalogue short:

Request What the model allows What the person receives
Status Read an accessible catalogue Its current revision and counter
Prepare a preview refresh Store an immutable plan for notes The target, current state, and expected change
Confirm Apply a still-valid plan once A receipt for a specific result
Arbitrary command or path Reject before storing a plan No execution

There is no command to delete data, install packages, or restart a server. Every additional capability needs its own scope, permissions, verification method, and failure consequences. Extending this vocabulary changes the product contract; it is more than adding a menu item.

Even a status request needs authorization. Information about a private task, address, or error can be sensitive without any server-side write. The example returns only two agreed fields; raw exceptions and arbitrary logs do not become bot replies.

Who clicked, and in which context?

Trusting a webhook and authorizing a person to act are different checks. A correctly accepted transport request does not entitle its sender to control every project. The server identifies the bot, user, chat, and workspace, then checks their permitted combination.

Telegram identifies a callback sender in CallbackQuery.from. answerCallbackQuery ends the client's waiting indicator; it does not certify task completion. These transport fields and behaviour are described in the Bot API.

A real adapter must derive identities from a verified update and the server's bot binding. A username, button label, forwarded message, or client-supplied scope does not replace that binding. The exercise deliberately starts after this boundary: Context already contains trusted synthetic identities bot-a, user-a, chat-a, and space-a. The model does not prove that a network adapter will construct them correctly.

Authorization is checked during both preparation and confirmation. If someone is removed from a project between the two, the old button does not preserve permission for a new action. In the model, a table stores the grant and only a test administrative function changes it. The bot has no “give myself access” operation.

The plan must be understandable before consent

A person needs a clear explanation, not internal JSON: “Refresh previews for the notes catalogue in space-a. Current revision: 1; prepared previews: 0. Expected result: revision 2 and counter 1. This plan is valid for 60 seconds.” All names and numbers in this example are fictional.

The technical record also binds identities, action type and version, resource revision, expiry, and the review message. A person must not approve an abstract “yes” after which the server chooses a target or parameters again. This approach follows OWASP transaction authorization guidance: show significant details before approval and enforce verification on the server.

The exercise serializes the plan in a defined way and assigns it a digest. This links a receipt to the exact record and detects accidental payload changes. It is not authentication, a person's signature, or protection against an administrator who can rewrite both payload and digest. Trusted storage remains a separate assumption.

Changes to an operation's meaning matter too. If the same name now executes different behaviour, the old plan must be refused based on its action version. Otherwise, someone could approve a small refresh but execute a newer, broader procedure.

The button refers to a plan

A callback needs only an opaque random reference to the server record. It contains no shell command, private filename, executor arguments, or permission to choose another project. The example creates the reference with secrets.token_urlsafe(24); Python's secrets module is intended for cryptographically strong tokens. Only its hash enters the table.

Randomness makes guessing difficult, but possessing the token is insufficient. Confirmation must match the bot, user, chat, workspace, and review message. Even another person with their own permission in that workspace cannot use someone else's token. Team approval would need a separately designed rule rather than a weakened check.

A button's appearance or removal of its keyboard does not prevent replay. An incoming request can repeat after the interface changes. The server must remember the plan's state independently of what the client currently displays.

In a real bot, the message binding becomes available after the review is successfully sent. Before that, the plan must not accept confirmation. The local exercise simplifies this step: a test directly supplies the known number 17 for a fictional message. It does not test a lost response while sending the review or exact correspondence between rendered text and the stored plan. Those are separate transport-integration tasks.

Confirmation checks the current state

For an unexecuted plan, the model rechecks permission, context, expiry, action version, and resource revision. A synthetic plan created at second 100 with a ten-second lifetime may be confirmed at second 109; at second 110 it is no longer valid. A time preceding creation is also refused. Explicit boundaries prevent different handlers from interpreting “still valid” differently.

Suppose a user prepares two plans for revision 1. The first has already advanced the resource to revision 2. The second cannot apply its old promise over the new state. It is refused, and the person needs a fresh review. Automatically rebuilding the second plan while retaining the old approval would authorize something the person never saw.

The same logic applies to changed recipients, amounts, file lists, or publication content. If a value is material to the decision, it belongs in the immutable plan or a verifiable version of that plan. A stable, attractive label should not hide a moving target.

Single use requires an atomic transition

Checking used == false and then executing separately leaves a gap for two simultaneous confirmations. In the exercise, one transaction includes reading the plan, checking current permissions and revision, changing the synthetic resource, and storing its receipt. A successful commit makes the plan completed.

In MySQL/InnoDB, current locking reads with SELECT ... FOR UPDATE hold the grant, plan, and resource rows until the transaction ends. Authorization checks serialize with revocation, and concurrent confirmations cannot independently apply the same plan or an old resource revision. Native errors, including duplicate key 1062, lock wait timeout 1205, or deadlock 1213, propagate after rollback of the controller's own transaction; the action is not performed outside it. Rollback behaviour is described in the InnoDB error-handling documentation.

Prepare the schema separately: Controller.initialize(migrator) runs DDL with the migrator account before business transactions. Tables use InnoDB, identities use VARCHAR with utf8mb4_0900_bin, and the serialized plan and receipt use LONGBLOB; CHECK constraints validate grant and plan-state values. The constructor does not create the schema, and the runtime account has no CREATE, ALTER, or DROP privileges. MySQL DDL can implicitly commit, so this is neither an atomic schema upgrade nor an automatic data transfer from the old SQLite database.

The same token submitted again by the same authorized person returns the stored receipt as already_completed. The counter does not change again. The result can be read even after the old plan expires: this reads completed work rather than reusing expired permission for a new action. Revoked access blocks this read too.

Running the example requires Python 3.12+, PyMySQL[rsa]==1.2.4, and the shared guarded MySQL fixture. From the project root, run the tests with bash fixtures/mysql_labs/run.sh python3 tools/local-control-lab/test_control.py. The tools/mysql_fixture.py helper provides a disposable schema, a separate migrator connection for explicit initialization, and runtime connections restricted to data operations. On MySQL 8.4, 41 unittest tests passed. In two threads with independent native TCP connections to one schema, one confirmation performed one synthetic counter increment and stored a receipt; the other returned that receipt as already_completed. Concurrent plans from different users against the old revision produced one completion and one resource-changed refusal. After closing and reopening a connection, the saved plan and receipt remained available. These checks demonstrate committed persistence through a new connection, not operating-system crash or power-loss behaviour.

The boundary between a local effect and external work

Here, the “executor” is an ordinary SQL increment in the same database. Injecting an error after the increment but before the receipt rolled both parts back. That behaviour is possible precisely because everything shares one transaction.

Replacing the increment with an HTTP request to an external service, SSH, or process launch would remove this guarantee. The external action might finish while receipt storage fails. We cannot transfer “single use” from the transactional MySQL model to an arbitrary executor. A real integration needs stored intent, explicit execution states, idempotency where supported, and reconciliation of uncertain outcomes. The TCP connection to MySQL is not itself such an external effect: the increment and receipt remain in one InnoDB transaction.

Nor should a transaction be held open around a long external-service call. A real control panel's separate executor accepts only permitted plan types, checks its own preconditions, and returns a result with defined meaning. The bot displays that work's state; a lost reply does not automatically start it again.

What a person should see after clicking

Useful responses distinguish “confirmation accepted,” “work running,” “completed,” “plan obsolete,” and “result needs reconciliation.” Our synchronous exercise supports only a completed local change or refusal. There is no need to invent asynchronous delivery for it.

The tests cover another person's token, changed context and message, revocation, expiry, resource changes, action versions, replay, concurrent confirmation, and rollback. They do not test a Telegram webhook, real-account permissions, button rendering, or an external executor. These boundaries determine which integration evidence is needed next.

A control panel becomes predictable when every click can be explained through a specific permitted action, a reviewed plan, and a stored result. Begin with a short vocabulary and a small, easily verified effect. Expand it once repeated requests, state changes, and unfinished outcomes have understandable behaviour for the person using it.

The shared MySQL fixture and its files are introduced in the transaction chapter. Run commands from the single article_demo root; do not create a separate Symfony application for each article.

python3 -m venv .venv-mysql
.venv-mysql/bin/pip install -r tools/requirements-mysql.txt
PYTHONPATH=tools bash fixtures/mysql_labs/run.sh .venv-mysql/bin/python -B -m unittest discover -s tools/local-control-lab -v
  1. Distribution A stale callback: menu versions and authorization when the action runs
  2. Architecture From manual commands to a CLI: planning, approving and verifying server changes
  3. Knowledge & AI AI prepares the publication, a human decides: preserving the exact approved version

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article