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!