From 0217f9701483c937d974ba007e15885d3b3f0f37 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 21 Jun 2022 19:45:24 +0600 Subject: [PATCH] checked 2.1.0 and Bot API 6.1 + update several examples --- .../src/main/kotlin/FilesLoaderBot.kt | 8 ++--- .../src/main/kotlin/ForwardInfoSenderBot.kt | 8 ++++- WebApp/src/jsMain/kotlin/main.kt | 36 ++++++++++++++++--- gradle.properties | 2 +- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt b/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt index b3a53c4..81183ba 100644 --- a/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt +++ b/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt @@ -20,11 +20,9 @@ suspend fun main(args: Array) { telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) { onMedia(initialFilter = null) { val pathedFile = bot.getFileAdditionalInfo(it.content.media) - val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply { - createNewFile() - writeBytes(bot.downloadFile(pathedFile)) - } - reply(it, "Saved to ${file.absolutePath}") + val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl) + bot.downloadFile(it.content.media, outFile) + reply(it, "Saved to ${outFile.absolutePath}") } onContentMessage { println(it) } }.second.join() diff --git a/ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt b/ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt index e8ddd0a..7a23ff3 100644 --- a/ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt +++ b/ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt @@ -26,7 +26,13 @@ suspend fun main(vararg args: String) { is UserForwardInfo -> { val user = forwardInfo.from when (user) { - is CommonUser -> regular("User ") + is CommonUser -> { + if (user.isPremium) { + regular("Premium user ") + } else { + regular("User ") + } + } is CommonBot, is ExtendedBot -> regular("Bot ") } + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})" diff --git a/WebApp/src/jsMain/kotlin/main.kt b/WebApp/src/jsMain/kotlin/main.kt index 0640c2c..37e0080 100644 --- a/WebApp/src/jsMain/kotlin/main.kt +++ b/WebApp/src/jsMain/kotlin/main.kt @@ -1,6 +1,8 @@ import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions import dev.inmo.tgbotapi.types.webAppQueryIdField import dev.inmo.tgbotapi.webapps.* +import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackStyle +import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackType import io.ktor.client.HttpClient import io.ktor.client.request.* import io.ktor.client.statement.bodyAsText @@ -15,8 +17,8 @@ import kotlinx.serialization.json.Json import org.w3c.dom.HTMLElement fun HTMLElement.log(text: String) { - appendElement("p", {}) appendText(text) + appendElement("p", {}) } fun main() { @@ -39,13 +41,17 @@ fun main() { } val dataIsSafe = response.bodyAsText().toBoolean() - document.body ?.appendElement("div") { - textContent = if (dataIsSafe) { + document.body ?.log( + if (dataIsSafe) { "Data is safe" } else { "Data is unsafe" } - } + ) + + document.body ?.log( + webApp.initDataUnsafe.chat.toString() + ) } document.body ?.appendElement("button") { @@ -69,6 +75,28 @@ fun main() { onViewportChanged { document.body ?.log("Viewport changed: ${it.isStateStable}") } + backButton.apply { + onClick { + document.body ?.log("Back button clicked") + hapticFeedback.impactOccurred( + HapticFeedbackStyle.Heavy + ) + } + show() + } + mainButton.apply { + setText("Main button") + onClick { + document.body ?.log("Main button clicked") + hapticFeedback.notificationOccurred( + HapticFeedbackType.Success + ) + } + show() + } + onSettingsButtonClicked { + document.body ?.log("Settings button clicked") + } } webApp.ready() }.onFailure { diff --git a/gradle.properties b/gradle.properties index d546ba9..69a8ac1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m kotlin_version=1.6.21 -telegram_bot_api_version=2.0.3 +telegram_bot_api_version=2.1.0 micro_utils_version=0.11.3 serialization_version=1.3.3 ktor_version=2.0.2