How to implement authorization using a Telegram API
Implementing multi-user authentication for the Telegram Bot API or MTProto client API is a common requirement, but the architecture differs significantly from standard REST interactions. Your goal involves managing dozens of distinct sessions where users earn "carma" points and are automatically routed into private chats based on their accumulated status. The complexity stems from Telegram's dual-layer approach: simple HTTP for bot configuration and a binary, hardware-bound protocol (MTProto) for actual user messaging.
The confusion often arises when examining the official documentation at https://core.telegram.org/api/auth. You will encounter definitions like auth.sendCode#efed51d9, which appears as a pseudo-code structure rather than JSON. This is because Telegram does not use JSON for user-side authentication; it uses TL (Type Language) over MTProto. The code snippet you found, defining structures with fields like phone_registered:Bool and send_call_timeout:int, describes the binary schema of the request, not a text-based payload. Attempting to send raw JSON directly to these endpoints will fail because the underlying transport layer expects binary framing.
To solve this in PHP without reinventing the wheel or managing complex C-based client libraries like tgbot or Webogram, you should leverage existing PHP packages that abstract the MTProto binary protocol. Libraries such as vlucas/phpdotenv are irrelevant here, but specific MTProto clients exist that handle the TL schema serialization and decryption automatically.
The core challenge of your architectureâmanaging multiple simultaneous authorizationsâis handled by maintaining separate session files (usually named key) for each user on your server. When a user logs in with their phone number and receives an OTP code, the client library generates a unique session key and stores it locally. This is distinct from the Bot Token, which is a single shared credential for your application logic.
Handling the Binary Protocol
If you choose to write this from scratch using raw sockets, you must implement the TL serializer yourself. You would need to read the auth.sendCode definition, map the fields (phone hash, code hash, password salt), and pack them into a binary buffer according to the MTProto specification. However, this approach is error-prone regarding version compatibility and encryption keys.
A more practical implementation strategy involves using a library that exposes an API similar to standard HTTP requests but operates over TCP or Unix sockets. You would establish a connection to one of the Data Centers (DCs), such as 149.154.175.50, and initiate the handshake. The client will manage the rotation between these servers, as seen in the C source code you analyzed (bl_do_dc_option).
Managing Multiple Users
The "multiple authorization" aspect is not about creating multiple tokens; it is about session isolation. In your PHP application:
- Session Storage: You must store the generated
keyfile path for each user in your database (e.g., mappinguser_idto/var/www/telegram_sessions/user_123.key). - Authentication Flow: When a user signs up or gains enough carma to join a chat, your PHP backend triggers the auth flow. The library handles the
auth.sendCode, validates the returned code hash, and completes the login. - Private Chat Logic: Once authenticated, you use the same session to call methods like
messages.getHistoryormessages.send. You can then programmatically invite the user to specific private chats by callingmessages.importChatInviteHashif they are not already members, or simply sending them a message that creates a group context.
Avoid trying to replicate the full functionality of desktop clients like Telegram Desktop. The source code you referenced shows complex state machines and server rotation logic that is unnecessary if you rely on a well-maintained PHP wrapper. Focus on your business logic: monitoring carma thresholds and triggering the auth sequence, rather than managing the low-level binary packets directly.
For production environments requiring robust session management, ensure your server environment supports the necessary file permissions for storing private key files securely. While there is no single "LaravelCompany" specific module for this, integrating these sessions into your existing Laravel stack requires careful handling of the key files outside the web root to prevent accidental exposure via HTTP requests.