Skip to content

Commit

Permalink
sendDataOrWorkWithQueryId
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed May 4, 2022
1 parent aa6e5b2 commit f36d642
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* `WebApps`:
* `WebAppInitData#queryId` now have correct js name of field
* New function `sendDataOrWorkWithQueryId`

## 0.38.20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ import dev.inmo.tgbotapi.types.WebAppQueryId
* 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(
inline fun sendDataOrWorkWithQueryId(
onSendData: () -> String?,
onAnswerWebAppQuery: (WebAppQueryId) -> Unit
) {
webApp.initDataUnsafe.queryId ?.let {
onAnswerWebAppQuery(it)
} ?: webApp.sendData(onSendData() ?: return)
val queryId = webApp.initDataUnsafe.queryId

if (queryId == null) {
webApp.sendData(onSendData() ?: return)
} else {
onAnswerWebAppQuery(queryId)
}
}

/**
* @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)

0 comments on commit f36d642

Please sign in to comment.