From b72921a44c3cc55959f27632b31c8ae51411fc11 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 20 Aug 2020 17:53:45 +0600 Subject: [PATCH] add example for resender bot and JS --- ForwarderBot/build.gradle | 2 +- GetMeBot/build.gradle | 2 +- HelloBot/build.gradle | 2 +- RandomFileSenderBot/build.gradle | 2 +- ResenderBot/build.gradle | 22 ++++++++ ResenderBot/src/main/kotlin/ResenderBot.kt | 65 ++++++++++++++++++++++ ResenderBot/src/main/resources/index.html | 16 ++++++ gradle.properties | 6 +- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle | 1 + 10 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 ResenderBot/build.gradle create mode 100644 ResenderBot/src/main/kotlin/ResenderBot.kt create mode 100644 ResenderBot/src/main/resources/index.html diff --git a/ForwarderBot/build.gradle b/ForwarderBot/build.gradle index f2e950f..c68f401 100644 --- a/ForwarderBot/build.gradle +++ b/ForwarderBot/build.gradle @@ -20,5 +20,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" } diff --git a/GetMeBot/build.gradle b/GetMeBot/build.gradle index 034ded0..e30ee88 100644 --- a/GetMeBot/build.gradle +++ b/GetMeBot/build.gradle @@ -20,5 +20,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" } diff --git a/HelloBot/build.gradle b/HelloBot/build.gradle index 034ded0..e30ee88 100644 --- a/HelloBot/build.gradle +++ b/HelloBot/build.gradle @@ -20,5 +20,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" } diff --git a/RandomFileSenderBot/build.gradle b/RandomFileSenderBot/build.gradle index 232fb29..2b61893 100644 --- a/RandomFileSenderBot/build.gradle +++ b/RandomFileSenderBot/build.gradle @@ -20,5 +20,5 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" } diff --git a/ResenderBot/build.gradle b/ResenderBot/build.gradle new file mode 100644 index 0000000..a5f2341 --- /dev/null +++ b/ResenderBot/build.gradle @@ -0,0 +1,22 @@ +plugins { + id 'org.jetbrains.kotlin.js' version "$kotlin_version" +} + +repositories { + jcenter() + mavenCentral() + mavenLocal() +} + +kotlin { + js(IR) { + browser() + binaries.executable() + } +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-js" + + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" +} diff --git a/ResenderBot/src/main/kotlin/ResenderBot.kt b/ResenderBot/src/main/kotlin/ResenderBot.kt new file mode 100644 index 0000000..7ed7f00 --- /dev/null +++ b/ResenderBot/src/main/kotlin/ResenderBot.kt @@ -0,0 +1,65 @@ +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.getMe +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.media.sendMediaGroup +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.* +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import kotlinx.browser.document +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import org.w3c.dom.* + +private val scope = CoroutineScope(Dispatchers.Default) + +fun main() { + document.addEventListener( + "DOMContentLoaded", + { + val botsContainer = document.getElementById("bots_container") ?: return@addEventListener + + (document.getElementById("bot_token_form") as? HTMLFormElement) ?.onsubmit = { + val botContainer = document.createElement("div") as HTMLDivElement + botsContainer.append(botContainer) + + val statusDiv = document.createElement("div") as HTMLDivElement + botContainer.append(statusDiv) + + val lastRequestAnswerDiv = document.createElement("div") as HTMLDivElement + botContainer.append(lastRequestAnswerDiv) + + val token = (document.getElementById("bot_token") as? HTMLInputElement) ?.value + if (token != null) { + val bot = telegramBot(token) + scope.launch { + statusDiv.innerHTML = "Loaded bot: ${bot.getMe()}" + + bot.startGettingFlowsUpdatesByLongPolling { + filterContentMessages(scope).onEach { + it.content.createResends(it.chat.id, replyToMessageId = it.messageId).forEach { + bot.executeUnsafe(it) ?.also { + lastRequestAnswerDiv.innerHTML = it.toString() + } + } + }.launchIn(scope) + filterMediaGroupMessages(scope).onEach { + safely { + bot.sendMediaGroup( + it.first().chat, + it.map { it.content.toMediaGroupMemberInputMedia() }, + replyToMessageId = it.first().messageId + ).also { + lastRequestAnswerDiv.innerHTML = it.toString() + } + } + }.launchIn(scope) + } + } + } + + false + } + } + ) +} diff --git a/ResenderBot/src/main/resources/index.html b/ResenderBot/src/main/resources/index.html new file mode 100644 index 0000000..955818f --- /dev/null +++ b/ResenderBot/src/main/resources/index.html @@ -0,0 +1,16 @@ + + + + + Resender bot + + +
+ + +
+
Type your bot token to the input above to start its work
+ +
+ + \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 27739e0..d647706 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,5 @@ -kotlin_version=1.3.72 +kotlin.code.style=official +org.gradle.parallel=true -telegram_bot_api_version=0.27.11 +kotlin_version=1.4.0 +telegram_bot_api_version=0.28.0-rc diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 917840c..fa55e31 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip diff --git a/settings.gradle b/settings.gradle index e5faa13..53527c1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,3 +2,4 @@ include ":ForwarderBot" include ":RandomFileSenderBot" include ":HelloBot" include ":GetMeBot" +include ":ResenderBot"