When the client fills out the form on the website, the application should reach the manager in seconds. Mail is slow, CRM requires manual processing, but Telegram-бот with the inline button "Take a request" is when the manager opens the chat and immediately works. We analyze the full implementation in PHP: from receiving data from the site to processing a callback from the manager.
Architecture: website → webhook-bot → manager
The scheme is simple: HTML форма на сайте sends a post request to your API endpoint. The PHP script generates a lead_id, writes an entry to the leads table, generates a message with an inline button and sends it through the Bot API to the manager. When the manager clicks "Take", the bot receives a callback_query, checks that the application is still free, and redirects it to the chat with the manager.
Key constraint to consider immediately: callback_data inline button cannot exceed 64 bytes. Therefore, instead of long IDs or JSON objects, we use a compact hash.
Table leads in the database
Before sending a request, we create a table. You will need: a unique identifier, the status (new/taken/closed), the ID of the manager who took the request, and the time of taking.
Generating lead_id: bin2hex(random_bytes(7))
Why do you need your ID if there is an autoincrement? Autoincrement is predictable, and lead_id should be unpredictable and short. random_bytes(7) generates 7 bytes, bin2hex turns them into 14 characters — compact enough for callback_data and at the same time safe.
Result Example a3f8b2c1d9e04f
On-site form handler: HTML + JavaScript
The form is minimal, but validation is mandatory. We send a fetch request, show the loading status and process the errors.
API endpoint: /api/lead/submit
This PHP script receives data, validates, writes to the database and sends a notification to Telegram. The bot token and the manager's chat_id are from the environment variables.
Processing callback_query: the bot takes the request
The bot's webhook endpoint receives a callback_query. We are checking: the application exists, it has not yet been taken. If everything is ok, we update the status, remember the manager and send him the application details in a personal chat.
Why not inline button with long data
Telegram limits callback_data up to 64 bytes. If you stuff JSON with client data there, you can easily exceed the limit with a long message. Binary hash from random_bytes(7) is 14 characters, compact and sufficient for a reliable join with a base. Data is stored in MySQL, not the button.
Idempotency: double clicking is not scary
The manager may accidentally press the button twice. UPDATE ... WHERE status = 'new' with verification $stmt->rowCount() guarantees: only the first press will be performed. The second one will receive the answer "the application has already been taken".
What to do next
To the basic scheme, you can add: notification to the client about the receipt of the application, automatic restart of the button after N minutes if the manager did not take, or export of unprocessed applications at the end of the day. The main thing is that the data is already in the database, and the manager receives them in one tap.
To quickly deploy a bundle of forms and a Telegram bot, you can use ready-made solutions on BotCreator.