Skip to content
The Chat Archive
gpt-4o-2024-05-13· OpenAI WildChat-4.8M (AI2)

How to detect when web site opened not from telegram mini app using @twa-dev/sdk ts?

  1. User

    How to detect when web site opened not from telegram mini app using @twa-dev/sdk ts?

  2. Assistant

    To detect when your website is opened outside of a Telegram Mini App using the @twa-dev/sdk library in a TypeScript project, you can utilize the initDataUnsafe property of the WebApp object provided by the SDK. This property will contain initialization data if the website is opened within a Telegram Mini App. If the initDataUnsafe property is empty or not present, it implies that the website is opened outside of the Telegram context.

    Here’s a step-by-step guide on how to implement this:

    1. Install the @twa-dev/sdk library: Make sure you have the library installed in your project. You can install it using npm:

      npm install @twa-dev/sdk
      
    2. Import and Initialize the SDK: Import the SDK in your TypeScript file and initialize it.

      import { WebApp } from '@twa-dev/sdk';
      
      // Initialize the WebApp
      const webApp = new WebApp();
      
    3. Detect Telegram Mini App Context: Use the initDataUnsafe property to check if the website is opened within a Telegram Mini App.

      function isOpenedInTelegram(): boolean {
        try {
          // 'initDataUnsafe' will only be set if the website is opened inside Telegram
          return !!webApp.initDataUnsafe;
        } catch (error) {
          console.error('Error checking Telegram context:', error);
          return false;
        }
      }
      
      // Example usage
      if (isOpenedInTelegram()) {
        console.log('Website opened inside Telegram Mini App');
      } else {
        console.log('Website opened outside of Telegram');
      }
      
    4. Handle the Result: You can then handle the logic accordingly, based on whether the website is opened from Telegram or not.

    Here is a full example:

    import { WebApp } from '@twa-dev/sdk';
    
    const webApp = new WebApp();
    
    function isOpenedInTelegram(): boolean {
      try {
        // 'initDataUnsafe' will contain initialization data if inside Telegram
        return !!webApp.initDataUnsafe;
      } catch (error) {
        console.error('Error checking Telegram context:', error);
        return false;
      }
    }
    
    if (isOpenedInTelegram()) {
      console.log('Website opened inside Telegram Mini App');
      // Place your logic for when the website is opened within Telegram here
    } else {
      console.log('Website opened outside of Telegram');
      // Place your logic for when the website is opened outside of Telegram here
    }
    

    Additional Notes:

    • Error Handling: Ensure you catch any potential errors when accessing initDataUnsafe in case there are issues with the initialization or the environment.
    • Security: Be cautious about how you use the initialization data, especially initDataUnsafe, to avoid any security vulnerabilities.

    By following these steps, you should be able to detect whether your website is being accessed from within a Telegram Mini App or from an external source.