Skip to content

☑️ Using /start

Because PBModular is designed with multiple modules coexisting, assigning the standard /start command to a single module wouldn't be appropriate. Instead, the framework allows each module to handle /start commands that are specifically targeted at it, usually via deep linking payloads.

How it Works

The /start command itself is typically handled by a core module or the main bot application logic. This initial handler checks if the /start command includes a payload (e.g., /start ModuleName-Payload). If the payload indicates a target module, the framework routes the command to that specific module's start_cmd method.

Implementing start_cmd

To handle a targeted start command, override the async def start_cmd(self, client: Client, message: Message) method within your main BaseModule class.

Important:

  • This method is only called if the incoming /start message contains a payload that the framework identifies as belonging to your module. The exact mechanism for identifying the target module (e.g., prefix matching like /start YourModuleName_...) depends on the core start handler's implementation. Consult the core module or main application logic for details on the expected payload format.
  • The start_cmd method cannot be defined within a ModuleExtension; it must be part of the main module class inheriting directly from BaseModule.

Example:

python
# main.py (inside your BaseModule class)
from pyrogram import Client
from pyrogram.types import Message

class MyAuthModule(BaseModule):

    # ... other methods ...

    async def start_cmd(self, client: Client, message: Message):
        """
        Handles /start MyAuthModule-SomeToken commands.
        Expected format: /start MyAuthModule-ABC123XYZ
        """
        self.logger.info(f"start_cmd triggered by user {message.from_user.id}")
        payload = message.command[1] # Assumes command is like ['/start', 'MyAuthModule-SomeToken']

        # Check if payload starts with expected prefix (adjust as needed)
        prefix = self.module_info.name + "-" # e.g., "MyAuthModule-"
        if payload.startswith(prefix):
            token = payload[len(prefix):]
            self.logger.info(f"Received start command with token: {token}")
            # Process the token (e.g., validate it, link user account)
            await self.process_auth_token(message.from_user.id, token)
            await message.reply(self.S["auth_token_received"].format(token=token))
        else:
            # This part might not even be reached if routing is strict
            self.logger.warning(f"Received unexpected payload in start_cmd: {payload}")
            await message.reply(self.S["errors"]["invalid_start_payload"])

    async def process_auth_token(self, user_id, token):
        # Dummy function: Replace with actual token processing logic
        self.logger.info(f"Processing token {token} for user {user_id}...")
        # (Database interactions, API calls, etc.)
        pass

    # ... rest of class ...

By implementing start_cmd, your module can cleanly handle deep links or specific initiation workflows triggered via the universal /start command without interfering with other modules.