add opportunity to set custom emoji status from webapp

This commit is contained in:
InsanusMokrassar 2024-12-08 13:17:39 +06:00
parent d294d0ef59
commit 8cd75673f5
4 changed files with 69 additions and 5 deletions

View File

@ -30,6 +30,7 @@ kotlin {
dependencies { dependencies {
implementation kotlin('stdlib') implementation kotlin('stdlib')
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
implementation "dev.inmo:tgbotapi.core:$telegram_bot_api_version"
implementation compose.runtime implementation compose.runtime
} }
} }

View File

@ -0,0 +1,3 @@
import dev.inmo.tgbotapi.types.CustomEmojiId
val CustomEmojiIdToSet = CustomEmojiId("5424939566278649034")

View File

@ -1,5 +1,7 @@
import androidx.compose.runtime.* import androidx.compose.runtime.*
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions 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.types.webAppQueryIdField
import dev.inmo.tgbotapi.webapps.* import dev.inmo.tgbotapi.webapps.*
import dev.inmo.tgbotapi.webapps.accelerometer.AccelerometerStartParams import dev.inmo.tgbotapi.webapps.accelerometer.AccelerometerStartParams
@ -85,6 +87,47 @@ fun main() {
P() P()
Text("Chat from WebAppInitData: ${webApp.initDataUnsafe.chat}") 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({ Button({
onClick { onClick {
scope.launchSafelyWithoutExceptions { scope.launchSafelyWithoutExceptions {

View File

@ -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.bot.setMyCommands
import dev.inmo.tgbotapi.extensions.api.send.reply import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send 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.api.telegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery 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.replyKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.webAppButton import dev.inmo.tgbotapi.extensions.utils.types.buttons.webAppButton
import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton 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.InlineQueryResult.InlineQueryResultArticle
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent 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.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.* import dev.inmo.tgbotapi.utils.*
import io.ktor.http.* import io.ktor.http.*
@ -30,7 +28,6 @@ import io.ktor.server.http.content.*
import io.ktor.server.request.* import io.ktor.server.request.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.io.File import java.io.File
@ -105,6 +102,26 @@ suspend fun main(vararg args: String) {
call.respond(HttpStatusCode.OK, isSafe.toString()) 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) }.start(false)