diff --git a/WebApp/build.gradle b/WebApp/build.gradle index 8700d47..8e04927 100644 --- a/WebApp/build.gradle +++ b/WebApp/build.gradle @@ -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 } } diff --git a/WebApp/src/commonMain/kotlin/CustomEmojiIdToSet.kt b/WebApp/src/commonMain/kotlin/CustomEmojiIdToSet.kt new file mode 100644 index 0000000..6c384a6 --- /dev/null +++ b/WebApp/src/commonMain/kotlin/CustomEmojiIdToSet.kt @@ -0,0 +1,3 @@ +import dev.inmo.tgbotapi.types.CustomEmojiId + +val CustomEmojiIdToSet = CustomEmojiId("5424939566278649034") diff --git a/WebApp/src/jsMain/kotlin/main.kt b/WebApp/src/jsMain/kotlin/main.kt index e7bb2a9..fa1c3d0 100644 --- a/WebApp/src/jsMain/kotlin/main.kt +++ b/WebApp/src/jsMain/kotlin/main.kt @@ -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 { diff --git a/WebApp/src/jvmMain/kotlin/WebAppServer.kt b/WebApp/src/jvmMain/kotlin/WebAppServer.kt index f067cd2..d6ed35f 100644 --- a/WebApp/src/jvmMain/kotlin/WebAppServer.kt +++ b/WebApp/src/jvmMain/kotlin/WebAppServer.kt @@ -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)