I’m working on a Telegram mini-app that lets users refer others and get rewards. I’ve set up authentication and most of the app’s features. But I’m having trouble getting the referral code from the bot’s start link.
For example, if the link is t.me/my_bot?start=RefC0d3, I need to grab that RefC0d3 part. I’ve tried different things, but nothing’s working. Can anyone help me out?
Here’s a simplified version of my JavaScript code:
In my Laravel controller, I’ve added Log::info('Input:', $request->all()); to check what’s coming in, but the referral code isn’t showing up. Any ideas what I’m doing wrong?
I encountered a similar challenge with Telegram’s start parameters. The issue likely stems from how Telegram handles these parameters in mini-apps. Instead of relying on tgApp.initDataUnsafe, you might try accessing window.Telegram.WebApp.initDataUnsafe.start_param directly. If that returns an empty value, consider parsing the URL by examining window.location.search to extract the start parameter. Once you have refCode, include it in your fetch request. Also, ensure your Laravel backend checks for ‘refCode’ rather than ‘start_param’. This approach resolved the issue for me.
I’ve dealt with a similar issue when working on a Telegram mini-app. The problem is likely that the start_param isn’t available in tgApp.initDataUnsafe. Telegram doesn’t always include it there, especially if the user has already started the bot before.
Instead, try getting the start parameter from the URL. You can do this by parsing window.Telegram.WebApp.initParams. Here’s how you might modify your code:
const urlParams = new URLSearchParams(window.Telegram.WebApp.initParams);
const refCode = urlParams.get('start_param');
// Then include refCode in your fetch body
body: JSON.stringify({
...tgApp.initDataUnsafe,
refCode: refCode
})
This should reliably grab the start parameter, even if it’s not in the initDataUnsafe object. Make sure your Laravel backend is set up to receive and process this parameter correctly. If you’re still not seeing it in your logs, double-check that you’re actually passing a start parameter when launching the mini-app from Telegram.