mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-22 00:27:19 +00:00
add opportunity to set custom emoji status from webapp
This commit is contained in:
parent
d294d0ef59
commit
8cd75673f5
@ -30,6 +30,7 @@ kotlin {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
|
||||
implementation "dev.inmo:tgbotapi.core:$telegram_bot_api_version"
|
||||
implementation compose.runtime
|
||||
}
|
||||
}
|
||||
|
3
WebApp/src/commonMain/kotlin/CustomEmojiIdToSet.kt
Normal file
3
WebApp/src/commonMain/kotlin/CustomEmojiIdToSet.kt
Normal file
@ -0,0 +1,3 @@
|
||||
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||
|
||||
val CustomEmojiIdToSet = CustomEmojiId("5424939566278649034")
|
@ -1,5 +1,7 @@
|
||||
import androidx.compose.runtime.*
|
||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||
import dev.inmo.tgbotapi.types.userIdField
|
||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||
import dev.inmo.tgbotapi.webapps.*
|
||||
import dev.inmo.tgbotapi.webapps.accelerometer.AccelerometerStartParams
|
||||
@ -85,6 +87,47 @@ fun main() {
|
||||
P()
|
||||
Text("Chat from WebAppInitData: ${webApp.initDataUnsafe.chat}")
|
||||
|
||||
val emojiStatusAccessState = remember { mutableStateOf(false) }
|
||||
webApp.onEmojiStatusAccessRequested {
|
||||
emojiStatusAccessState.value = it.isAllowed
|
||||
}
|
||||
Button({
|
||||
onClick {
|
||||
webApp.requestEmojiStatusAccess()
|
||||
}
|
||||
}) {
|
||||
Text("Request custom emoji status access")
|
||||
}
|
||||
if (emojiStatusAccessState.value) {
|
||||
Button({
|
||||
onClick {
|
||||
webApp.setEmojiStatus(CustomEmojiIdToSet/* android custom emoji id */)
|
||||
}
|
||||
}) {
|
||||
Text("Set custom emoji status")
|
||||
}
|
||||
val userId = webApp.initDataUnsafe.user ?.id
|
||||
userId ?.let { userId ->
|
||||
Button({
|
||||
onClick {
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
client.post("$baseUrl/setCustomEmoji") {
|
||||
parameter(userIdField, userId.long)
|
||||
setBody(
|
||||
Json.encodeToString(
|
||||
WebAppDataWrapper.serializer(),
|
||||
WebAppDataWrapper(webApp.initData, webApp.initDataUnsafe.hash)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}) {
|
||||
Text("Set custom emoji status via bot")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button({
|
||||
onClick {
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
|
@ -6,6 +6,7 @@ import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
import dev.inmo.tgbotapi.extensions.api.set.setUserEmojiStatus
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery
|
||||
@ -16,12 +17,9 @@ import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.webAppButton
|
||||
import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||
import dev.inmo.tgbotapi.types.InlineQueryId
|
||||
import dev.inmo.tgbotapi.types.LinkPreviewOptions
|
||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import io.ktor.http.*
|
||||
@ -30,7 +28,6 @@ import io.ktor.server.http.content.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
@ -105,6 +102,26 @@ suspend fun main(vararg args: String) {
|
||||
|
||||
call.respond(HttpStatusCode.OK, isSafe.toString())
|
||||
}
|
||||
post("setCustomEmoji") {
|
||||
val requestBody = call.receiveText()
|
||||
val webAppCheckData = Json.decodeFromString(WebAppDataWrapper.serializer(), requestBody)
|
||||
|
||||
val isSafe = telegramBotAPIUrlsKeeper.checkWebAppData(webAppCheckData.data, webAppCheckData.hash)
|
||||
val rawUserId = call.parameters[userIdField] ?.toLongOrNull() ?.let(::RawChatId) ?: error("$userIdField should be presented as long value")
|
||||
|
||||
val set = if (isSafe) {
|
||||
runCatching {
|
||||
bot.setUserEmojiStatus(
|
||||
UserId(rawUserId),
|
||||
CustomEmojiIdToSet
|
||||
)
|
||||
}.getOrElse { false }
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
call.respond(HttpStatusCode.OK, set.toString())
|
||||
}
|
||||
}
|
||||
}.start(false)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user