Reputation: 21
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
Upvotes: 2
Views: 8810
Reputation: 367
<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>
Upvotes: 0
Reputation: 185
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
.
Upvotes: 3
Reputation: 21
The problem was in global params in TS Need to declare
declare global {
interface Window {
Telegram: {
WebApp: {
close: () => void;
}
};
}
}
Upvotes: 0