CrazyTapok-bit / tgWebValid

An easy way to validate Telegram Login Widget and Telegram Mini App users on your website using PHP
https://tgwebvalid.com
MIT License
52 stars 13 forks source link

can't get data #78

Closed ezreza closed 4 months ago

ezreza commented 4 months ago

Hi Where to get the data for validateInitData ?

Screenshot 2024-06-10 030050

I need the user ID of the user to connect to the database
tnx for helping

CrazyTapok-bit commented 4 months ago

Hello

After you plug in the js code that gives the telegram, the data will be available in the window.Telegram.WebApp.initData field. Keep in mind, data is only available by opening the app. This means that directly opening your program in the browser, data will not be provided. Also, sometimes these data may be missing. Therefore, you need to log the user in at the first login

After successful login, the user ID will be available here

$initData->user->id
ezreza commented 4 months ago

thanks for answer

I used Telegram script <script src="https://telegram.org/js/telegram-web-app.js"></script>

With js I can get the data "query_id=AAFF&user=%7B%22id%22%3A123456789 ..."

I entered the received data 👆 manually in validateInitData and ID received

The problem I have is that window.Telegram.WebApp.initData is not included in validateInitData to receive data automatically.

Should validateInitData be like this? Data is not received in this way! $initData = $tgWebValid->bot()->validateInitData('query_id=...');

CrazyTapok-bit commented 4 months ago

Oh, the data that is available on the frontend must be transferred to the backend. I can't include them, because it requires a script connection + access to the window object, which is not available with PHP. Therefore, you need to get this data using js, and send it to which will process it and return the result.

Thus, the approximate code will look like this (check for errors)

Frontend (JavaScript)

async function auth (payload) {
  try {
    const response = await fetch('/auth-telegram-handler.php', {
      method: 'POST',
      body: payload
    })
    console.log(response)
  } catch (e) {
    console.error(e)
  }
}

auth({
  initData: window.Telegram.WebApp.initData
})

Backend (PHP)

<?php

use TgWebValid\TgWebValid;
use TgWebValid\Exceptions\BotException;
use TgWebValid\Exceptions\ValidationException;
use Exception;

include './vendor/autoload.php';

// Get the data sent by the frontend
$initData = json_decode(file_get_contents('php://input'), true)['initData'] ?? '';

try {
    $tgWebValid = new TgWebValid('TELEGRAM_BOT_TOKEN', true);

    $initData = $tgWebValid->bot()->validateInitData($initData);

    var_dump($initData);
} catch (ValidationException $e) {
    // Verification failed
} catch (BotException $e) {
    // The bot name is incorrect
} catch (Exception $e) {
    // Other exceptions
}
ezreza commented 4 months ago

I applied the above but the data was not received again

errors logs The use statement with non-compound name 'Exception' has no effect in /.../auth-telegram-handler.php on line 6

auth-telegram-handler.php on line 6 use Exception;

CrazyTapok-bit commented 4 months ago

I showed you an example of how it all works, then you need to work on the code yourself and make it work the way you need it

ezreza commented 4 months ago

Ok, thanks for the help

CrazyTapok-bit commented 4 months ago

Glad to help. If you have any questions or suggestions regarding the work of the library, please contact us