How to capture referral parameters in PHP Telegram bot using /start command

I’m working on a PHP Telegram bot where I need to implement a referral system. The idea is to reward users with points when they invite others to use the bot. I want to use the /start command with additional parameters to track who referred whom.

I’ve been following the Telegram bot documentation and managed to set up the basic command structure, but I’m having trouble extracting the referral parameter from the message.

Here’s my current code:

error_reporting(E_ALL);
$bot_token = "123456789:ABCdefGHijklMNopQRstuvWXyz123456789";
$api_url = "https://api.telegram.org/bot" . $bot_token;

$input_data = file_get_contents("php://input");
$decoded_data = json_decode($input_data, TRUE);

$user_chat_id = $decoded_data["message"]["chat"]["id"];
$user_message = $decoded_data["message"]["text"];
$referral_param = $decoded_data["message"]["text"]["payload"];

$referrer_data = mysqli_fetch_assoc(mysqli_query($db_connection, "SELECT * FROM users WHERE referral_id='$referral_param'"));

sendBotMessage($user_chat_id, "Your referrer is: " . $referrer_data['username']);

function sendBotMessage($chat_id, $text_message) {
    global $api_url;
    $request_url = $api_url . "/sendMessage?chat_id=" . $chat_id . "&text=" . urlencode($text_message);
    file_get_contents($request_url);
}

The problem is that I’m not getting any output for the referral parameter. I’ve searched online but couldn’t find clear examples of how to extract these parameters in PHP. Can someone help me understand the correct way to handle this?

I experienced the same issue while setting up a referral system recently. The problem arises because you’re trying to access a nonexistent payload in the JSON response. When users utilize a link like https://t.me/yourbot?start=ref123, the actual message that Telegram sends is /start ref123.

To properly extract the referral parameter, you should split the message text. Here’s a snippet that helped me:

if (strpos($user_message, '/start') === 0) {
    $parts = explode(' ', $user_message);
    if (count($parts) > 1) {
        $referral_param = $parts[1];
        // You can now use $referral_param for your database queries
    }
}

Additionally, ensure you implement prepared statements to prevent SQL injection vulnerabilities in your queries. This method has effectively tracked referrals for my bot without issues.

Your code’s trying to access a payload property that doesn’t exist in Telegram’s API response. When someone clicks a referral link like https://t.me/yourbot?start=referral123, the whole message text becomes /start referral123. You’ve got to parse the message text manually to grab the parameter.

Replace your current parameter extraction with this:

$user_message = $decoded_data["message"]["text"];
$message_parts = explode(' ', $user_message, 2);
$referral_param = isset($message_parts[1]) ? $message_parts[1] : null;

This splits the message by spaces and grabs the second part as your referral parameter. Don’t forget to validate that the parameter exists before hitting your database - otherwise you’ll get errors when users send just /start without any referral code.

Ah, I see the problem! You’re trying to access $decoded_data["message"]["text"]["payload"] but text is just a string, not an array. When someone sends /start xyz123, it all comes as one text message. Just split it like this: $parts = explode(' ', $user_message); $referral_param = $parts[1] ?? null; and you should be good to go.