Accessing Telegram User Data in Unity WebGL Game via Bot API

Hey folks!

I’m working on this cool Unity WebGL game for Telegram. I want to grab the player’s profile info when they start the game using the Bot API. Here’s what I’ve done so far:

  • Set up a bot with BotFather
  • Created a web app for my game
  • Tried using getUpdates with an offset

But I’m stuck! The getUpdates thing only gives me chat user info, not the individual player data I need. Any ideas on how to get the user’s profile? I’ve been digging through the Bot API docs but can’t find a clear solution.

Has anyone done something similar? I’d really appreciate some pointers or code examples. Thanks in advance!

I’ve faced a similar challenge with Telegram WebApps. The key is to use the Telegram WebApp JavaScript API instead of relying solely on the Bot API. In your Unity WebGL build, you can include a JavaScript interface to access the Telegram.WebApp object. This object provides methods like getInitData() and initDataUnsafe, which give you access to the user’s Telegram ID, first name, last name, and username.

To implement this, you’ll need to create a JavaScript plugin for Unity that can communicate with the Telegram WebApp API. Then, use Unity’s JsonUtility to parse the data in C#. This approach should give you the user profile information you need without having to rely on getUpdates.

Remember to handle cases where the game might be accessed outside of Telegram, as the WebApp object won’t be available in those instances.

From personal experience with Telegram bots and WebApps, integrating user data into a Unity WebGL game can be challenging. I discovered that using a combination of the Telegram WebApp API and a server component is an effective solution.

In my implementation, I developed a custom JavaScript plugin in the Unity WebGL build to interface with Telegram.WebApp and retrieve the initialization data. This data was then forwarded securely to my game server for verification using Telegram’s checkData method. After confirmation, I used the user ID to request profile details from the Bot API, and the verified data was finally relayed back into the game. This method, although more complex than direct API calls, offers a more reliable and secure way to access authentic user data.

hey there! i’ve done somthing similar before. instead of using getUpdates, try the telegram WebApp API. you can add a JS plugin to your unity project that talks to the WebApp object. it has cool stuff like getInitData() which gives you the user info youre after. just remember to handle cases where the game isnt run in telegram. hope this helps!