11import androidx.compose.runtime.*
22import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
33import dev.inmo.tgbotapi.types.CustomEmojiId
4+ import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
45import dev.inmo.tgbotapi.types.userIdField
56import dev.inmo.tgbotapi.types.webAppQueryIdField
67import dev.inmo.tgbotapi.webapps.*
@@ -17,6 +18,7 @@ import io.ktor.client.request.*
1718import io.ktor.client.statement.bodyAsText
1819import io.ktor.http.*
1920import io.ktor.http.content.TextContent
21+ import kotlinx.browser.document
2022import kotlinx.browser.window
2123import kotlinx.coroutines.*
2224import kotlinx.dom.appendElement
@@ -65,7 +67,12 @@ fun main() {
6567 }
6668 val scope = rememberCoroutineScope()
6769 val isSafeState = remember { mutableStateOf<Boolean ?>(null ) }
68- val logsState = remember { mutableStateListOf<Any ?>() }
70+ val logsState = remember {
71+ mutableStateListOf<Any ?>(
72+ window.location.href,
73+ )
74+ }
75+ val buttonIdState = remember { mutableStateOf<PreparedKeyboardButtonId ?>(null ) }
6976
7077// Text(window.location.href)
7178// P()
@@ -94,6 +101,30 @@ fun main() {
94101 )
95102 }
96103
104+ LaunchedEffect (baseUrl) {
105+ val response = client.post(" $baseUrl /getPreparedKeyboardButtonId" ) {
106+ setBody(
107+ Json .encodeToString(
108+ WebAppDataWrapper .serializer(),
109+ WebAppDataWrapper (webApp.initData, webApp.initDataUnsafe.hash)
110+ )
111+ )
112+ parameter(userIdField, webApp.initDataUnsafe.user ?.id ?.long ? : return @LaunchedEffect)
113+ }
114+ when (response.status) {
115+ HttpStatusCode .OK -> {
116+ val buttonId = response.bodyAsText()
117+ buttonIdState.value = PreparedKeyboardButtonId (buttonId)
118+ }
119+ HttpStatusCode .NoContent -> {
120+ buttonIdState.value = null
121+ }
122+ else -> {
123+ logsState.add(" Error while getting prepared keyboard button id: ${response.status} " )
124+ }
125+ }
126+ }
127+
97128 Text (
98129 when (isSafeState.value) {
99130 null -> " Checking safe state..."
@@ -249,6 +280,23 @@ fun main() {
249280 Text (" Confirm" )
250281 }
251282
283+ P ()
284+ H3 { Text (" Prepared keyboard button" ) }
285+ val buttonIdValue = buttonIdState.value
286+ if (buttonIdValue == null ) {
287+ Text (" Ensure that you have called /prepareKeyboard in bot. If you did it, check logs of server" )
288+ } else {
289+ Button ({
290+ onClick {
291+ webApp.requestChat(buttonIdValue) {
292+ logsState.add(" Chat have been received: $it " )
293+ }
294+ }
295+ }) {
296+ Text (" Prepared keyboard button" )
297+ }
298+ }
299+
252300 P ()
253301 H3 { Text (" Write access callbacks" ) }
254302 Button ({
@@ -396,11 +444,15 @@ fun main() {
396444 }
397445 mainButton.apply {
398446 setText(" Main button" )
399- setParams(
400- BottomButtonParams (
401- iconCustomEmojiId = CustomEmojiId (" 5370976574969486150" ) // 😏
447+ runCatching {
448+ setParams(
449+ BottomButtonParams (
450+ iconCustomEmojiId = CustomEmojiId (" 5370976574969486150" ) // 😏
451+ )
402452 )
403- )
453+ }.onFailure {
454+ logsState.add(" Can't set params for main button: $it " )
455+ }
404456 onClick {
405457 logsState.add(" Main button clicked" )
406458 hapticFeedback.notificationOccurred(
@@ -411,11 +463,15 @@ fun main() {
411463 }
412464 secondaryButton.apply {
413465 setText(" Secondary button" )
414- setParams(
415- BottomButtonParams (
416- iconCustomEmojiId = CustomEmojiId (" 5370763368497944736" ) // 😒
466+ runCatching {
467+ setParams(
468+ BottomButtonParams (
469+ iconCustomEmojiId = CustomEmojiId (" 5370763368497944736" ) // 😒
470+ )
417471 )
418- )
472+ }.onFailure {
473+ logsState.add(" Can't set params for secondary button: $it " )
474+ }
419475 onClick {
420476 logsState.add(" Secondary button clicked" )
421477 hapticFeedback.notificationOccurred(
0 commit comments