I’m working on a Telegram bot in PHP and I want to create a referral system. The idea is to use the /start command with a payload to track referrals and give points to users who bring in new people.
I’ve set up the command following the docs, but I’m stuck on how to get the payload data. Here’s what I’ve tried:
$token = 'your_bot_token_here';
$website = 'https://api.telegram.org/bot' . $token;
$update = json_decode(file_get_contents('php://input'), true);
$chatId = $update['message']['chat']['id'];
$message = $update['message']['text'];
$referralId = $update['message']['text']['payload'] ?? null;
if ($referralId) {
$referrer = getRefererInfo($referralId);
sendMessage($chatId, 'Welcome! You were referred by ' . $referrer['name']);
} else {
sendMessage($chatId, 'Welcome to our bot!');
}
function sendMessage($chatId, $text) {
global $website;
$url = $website . '/sendMessage?chat_id=' . $chatId . '&text=' . urlencode($text);
file_get_contents($url);
}
The problem is, I can’t seem to access the payload. When I try to print it, nothing shows up. I’ve searched online but couldn’t find a clear answer on how to fetch the payload using PHP. Any ideas on what I’m doing wrong or how to fix this? Thanks!
hey there! i’ve dealt with this before. the payload isn’t in ‘text’, it’s in the actual command. try something like this:
$fullCommand = $update['message']['text'];
$parts = explode(' ', $fullCommand);
$referralId = $parts[1] ?? null;
this should grab the referral id after /start. hope it helps!
I’ve implemented referral systems in Telegram bots before, and I can see where you’re running into issues. The payload isn’t directly accessible in the ‘text’ field. Instead, you need to look for it in the ‘start_parameter’ of the message entity.
Try modifying your code like this:
$update = json_decode(file_get_contents('php://input'), true);
$message = $update['message'];
$chatId = $message['chat']['id'];
if (isset($message['entities']) && $message['entities'][0]['type'] === 'bot_command') {
$fullCommand = $message['text'];
$commandParts = explode(' ', $fullCommand);
$referralId = $commandParts[1] ?? null;
if ($referralId) {
$referrer = getRefererInfo($referralId);
sendMessage($chatId, 'Welcome! You were referred by ' . $referrer['name']);
} else {
sendMessage($chatId, 'Welcome to our bot!');
}
}
This approach checks if the message contains a bot command, then splits the text to extract the referral ID. It should work for commands like ‘/start referral123’. Remember to properly sanitize and validate the referral ID before using it in your database queries.
I’ve actually implemented a similar referral system in one of my Telegram bots recently. The trick is to parse the command arguments correctly. Here’s what worked for me:
$update = json_decode(file_get_contents(‘php://input’), TRUE);
$message = $update[‘message’];
if (strpos($message[‘text’], ‘/start’) === 0) {
$args = explode(’ ', $message[‘text’]);
$referralId = $args[1] ?? null;
if ($referralId) {
// Process referral
$referrer = fetchReferrerFromDatabase($referralId);
sendMessage($message['chat']['id'], "Welcome! You've been referred by {$referrer['name']}.");
updateReferralPoints($referralId);
} else {
sendMessage($message['chat']['id'], 'Welcome to our bot! Start exploring our features.');
}
}
This approach has been quite reliable for me. Just make sure to sanitize the referralId before using it in database queries to prevent any potential security issues. Also, you might want to implement some anti-abuse measures to prevent users from gaming the system.