skip to Main Content

I make telegram web app using React Js.
I need method which closed telegram web app and returned to chat.
I found info that there is method close in window.Telegram.WebApp. But it’s undefined when I consoled. And this method doesn’t work.
Even console.log(window) return nothing in telegram web app

3

Answers


  1. Chosen as BEST ANSWER

    The problem was in global params in TS Need to declare

    declare global {
      interface Window {
        Telegram: {
          WebApp: {
            close: () => void;
          }
        };
      }
    }
    

  2. To make Telegram object appear in global window object, you have to append Telegram’s script in your <head/> tag. You can find official mention about it here.

    Nevertheless, I don’t recommend usage of Telegram’s script as long as its code is recognized as not optimal and probably vulnerable. To avoid known problems, you can use up-to-date TypeScript libraries, such as twa-sdk or twa-bridge.

    Login or Signup to reply.
  3. <script>
    Telegram.WebApp.MainButton
            .setText('CLOSE WEBVIEW')
            .show()
            .onClick(function(){ webviewClose(); });
    </script>
    

    <div id="buttons" style="margin-top:100px;">          
    <button onclick="webviewClose();">Close</button>
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search