Skip to content

Library for creating telegram mini apps with Kotlin Multiplatform and Compose Multiplatform

License

Notifications You must be signed in to change notification settings

quaterma1n/tg-mini-app

 
 

Repository files navigation

Telegram mini app

Library for creating telegram mini apps with Kotlin Multiplatform and Compose Multiplatform.

Setup

  1. Create Kotlin Multiplatform app with js target. Check this tutorial for more information.

  2. Place script telegram-web-app.js in the <head> tag:

<script src="https://telegram.org/js/telegram-web-app.js"></script>
  1. Add implementation for jsMain tg-mini-app library:
implementation("io.github.kirillNay:tg-mini-app:1.1.0")
  1. In main funtion of jsMain call telegramWebApp with providing Composable content:
fun main() {
   telegramWebApp { style ->
      // Compose content ...
   }
}

WebApp

To get access of Telegram web app instance call webApp. All properties and functions of Telegram WebApp are available in kotlin style.

Example

Showing confirmation dialog before closing app.

webApp.showConfirm("Want to exit?") { result ->
  if (result) {
      webApp.close()
  }
}

Designing

Mini apps should be designed to provide smooth user experience so pay attention of themeing of app. Check Telegram's Design Guidelines for more information.

Viewport

Pay attention to viewport changes. Viewport of mini app can change due to users gestures. On every change new viewport size value will be provided to content composable as a TelegramStyle parameter. Recompose composable functions that should be changed on viewport size modifications.

You can get current viewport height using webApp.viewportHeight. In case you need last stable viewport state you can get it from webApp.viewportStableHeight. Aldo you can get if current mini app state is expanded using webApp.isExpanded.

Example

telegramWebApp { style ->
      val height = remember(style.viewPort.viewPortHeight) {
          max(style.viewPort.viewPortHeight, 500.dp)
      }
      Column(
          modifier = Modifier.height(height)
      ) {
         /...
      }
}

Colors

The colors of the mini-apps should not contrast with the main theme of the application. Create your system design based on colors parameter that is provided to content composable as a TelegramStyle. This parameter will change and cause recomposition if user change app style in settings of telegram app or in using dark theme of his device.

You can observe theme changes in any place of your app using:

var telegramColors by remember { mutableStateOf(YourDefaultColors()) }
webApp.addEventHandler(EventType.THEME_CHANGED) {
   telegramColors = webApp.themeParams.toYourAppColors() // Map telegram theme colors to your colors scheme
}

You can get if current theme of app is in dark mode using webApp.colorScheme.

Sample

Check GlassOfWater - simple web app created with Kotlin and Compose Multiplatform.

About

Library for creating telegram mini apps with Kotlin Multiplatform and Compose Multiplatform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%