1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-26 19:48:09 +00:00
tgbotapi/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/SendData.kt

34 lines
1.4 KiB
Kotlin
Raw Normal View History

2022-05-04 05:01:26 +00:00
package dev.inmo.tgbotapi.webapps
import dev.inmo.tgbotapi.types.WebAppQueryId
/**
* @param onSendData Should return the data which must be used in [WebApp.sendData]. If returns null, data will not be sent
* @param onAnswerWebAppQuery In case if [WebAppInitData.queryId] is presented in [WebApp.initDataUnsafe], will be called
* that callback. Before and after calling of this callback will not be used any method of answering to the telegram
* system, so, you must use something like [answerWebAppQuery] by yourself to send the result
*/
2022-05-04 07:05:54 +00:00
inline fun sendDataOrWorkWithQueryId(
2022-05-04 05:01:26 +00:00
onSendData: () -> String?,
onAnswerWebAppQuery: (WebAppQueryId) -> Unit
) {
2022-05-04 07:05:54 +00:00
val queryId = webApp.initDataUnsafe.queryId
if (queryId == null) {
webApp.sendData(onSendData() ?: return)
} else {
onAnswerWebAppQuery(queryId)
}
2022-05-04 05:01:26 +00:00
}
2022-05-04 07:05:54 +00:00
/**
* @param onSendData Should return the data which must be used in [WebApp.sendData]. If returns null, data will not be sent
* @param onAnswerWebAppQuery In case if [WebAppInitData.queryId] is presented in [WebApp.initDataUnsafe], will be called
* that callback. Before and after calling of this callback will not be used any method of answering to the telegram
* system, so, you must use something like [answerWebAppQuery] by yourself to send the result
*/
inline fun handleResult(
onSendData: () -> String?,
onAnswerWebAppQuery: (WebAppQueryId) -> Unit
) = sendDataOrWorkWithQueryId(onSendData, onAnswerWebAppQuery)