Trouble extracting 'start' parameter from Telegram bot link in PHP webhook

Hey guys, I’m stuck with my Telegram bot. I set up a webhook using PHP to handle user commands, but I’m having issues with the ‘start’ parameter in the bot’s URL.

The bot works fine when users start it normally (like https://t.me/my_cool_bot). But I want to add a referral system, so I made URLs like https://t.me/my_cool_bot/?start=987654321. The problem is, when someone clicks this URL, the bot can’t grab the ‘start’ value.

I need the bot to catch this value and use it in a WebApp link, like:

$appLink = 'https://mysite.com/?user=' . $name . '&id=' . $userId . '&start=' . $startParam;

Right now, it gets the username and user ID, but misses the ‘start’ parameter. When someone uses the referral link, they just get an ‘Invalid command’ message. The bot only responds properly when they manually type ‘/start’.

I think the ‘start’ parameter might be hidden when the bot first runs. Any ideas how to fix this? Here’s a simplified version of my code:

$botApi = 'https://api.telegram.org/bot' . $token;
$update = json_decode(file_get_contents('php://input'), true);

if (isset($update['message'])) {
    $chatId = $update['message']['chat']['id'];
    $text = $update['message']['text'];
    $userId = $update['message']['from']['id'];
    $name = $update['message']['from']['first_name'];

    if (strpos($text, '/start') === 0) {
        $startParam = explode(' ', $text)[1] ?? null;
        if (!$startParam) {
            $urlParts = parse_url($text);
            if (isset($urlParts['query'])) {
                parse_str($urlParts['query'], $params);
                $startParam = $params['start'] ?? null;
            }
        }
        $appLink = 'https://mysite.com/?user=' . $name . '&id=' . $userId . '&start=' . $startParam;
        sendWelcome($chatId, $name, $appLink);
    } else {
        sendMessage($chatId, 'Please use a valid command.');
    }
}

function sendWelcome($chatId, $name, $appLink) {
    // Send welcome message with app link
}

function sendMessage($chatId, $text) {
    // Send regular message
}

Can anyone spot what I’m doing wrong or suggest a fix? Thanks!

The issue appears to stem from how Telegram handles deep links. When a user clicks a referral link, Telegram may not immediately send the /start command with the extra parameter.

One effective approach is to implement a two-step process. First, capture the referral parameter as soon as the bot is opened, perhaps storing it temporarily with the user’s ID. Then, when the user sends a message to the bot, retrieve this parameter and integrate it into your logic.

This method can help ensure that you don’t miss the parameter due to the delay in command processing.

I’ve encountered this issue before and found that it comes down to how Telegram processes deep links. When a referral link is clicked, Telegram does not immediately send the /start command with its parameter, which means the extra data can be missed.

One solution I used was to adjust the flow. Instead of assuming the parameter is available during the initial command, I started fetching updates using Telegram’s API and stored the parameter against the user’s ID. Then, as soon as the user sends any follow-up message, I retrieve and process the stored parameter. This extra step can be a bit more involved, but it ensures that the referral data isn’t lost even if there’s a delay in processing.