In Telegram docs, https://core.telegram.org/bots/webapps#initializing-mini-apps,
To connect your Mini App to the Telegram client, place the script telegram-web-app.js in the
<head>tag before any other scripts, using this code:<script src="https://telegram.org/js/telegram-web-app.js?59"></script>Once the script is connected, awindow.Telegram.WebAppobject will become available with the following fields: ...
According to Microsoft docs, JavaScript [JSImport]/[JSExport] interop in .NET WebAssembly, my code:
Program.cs
private static Task Main(string[] args)
{
JSHost.ImportAsync("TelegramMiniAppJS", "/telegram-web-app.js");
return BuildAvaloniaApp()
.WithInterFont()
.StartBrowserAppAsync("out");
}
TelegramMiniAppInterop.cs
public partial class TelegramMiniAppInterop
{
[JSImport("window.Telegram.WebApp.version", "TelegramMiniAppJS")]
public static partial string Version();
}
in browser console.
[Telegram.WebView] > postEvent web_app_set_header_color Object telegram-web-app.js:135 [Telegram.WebView] > postEvent web_app_set_bottom_bar_color Object telegram-web-app.js:135 [Telegram.WebView] > postEvent web_app_request_theme telegram-web-app.js:135 [Telegram.WebView] > postEvent web_app_request_viewport telegram-web-app.js:135 [Telegram.WebView] > postEvent web_app_request_safe_area telegram-web-app.js:135 [Telegram.WebView] > postEvent web_app_request_content_safe_area
But after a call of string Version = TelegramMiniAppInterop.Version()
I get an exception.
Exception message: Error: window not found while looking up window.Telegram.WebApp.version at invoke-js.ts:399:19 at invoke-js.ts:94:16 at Fc (invoke-js.ts:28:9) at dotnet.native.wasm.do_icall (dotnet.native.wasm-06188c26:0x1ea476) at dotnet.native.wasm.do_icall_wrapper (dotnet.native.wasm-06188c26:0x1e68d9) at dotnet.native.wasm.mono_interp_exec_method (dotnet.native.wasm-06188c26:0x1d7b2d) at dotnet.native.wasm.interp_entry (dotnet.native.wasm-06188c26:0x1e9a89) at dotnet.native.wasm.interp_entry_static_0 (dotnet.native.wasm-06188c26:0x1eb9f3) at dotnet.native.wasm.wasm_native_to_interp_System_Threading_System_Private_CoreLib_ThreadPool_BackgroundJobHandler (dotnet.native.wasm-06188c26:0x9975ec) at dotnet.native.wasm.mono_background_exec (dotnet.native.wasm-06188c26:0x21ffe9)
How to access an window.Telegram.WebApp object in C#?
I try: Program.cs
JSHost.ImportAsync("TelegramMiniAppJS", "/telegram-web-app.js?59");JSHost.ImportAsync("TelegramMiniAppJS", "https://telegram.org/js/telegram-web-app.js?59");
TelegramMiniAppInterop.cs
[JSImport("window.Telegram.WebApp.version", "TelegramMiniAppJS")][JSImport("globalThis.window.Telegram.WebApp")],[JSImport("globalThis.window.Telegram.WebApp"), "TelegramMiniAppJS"],
but there was no result.