From 656b8b67740459fc34f144015ff14e8eae540783 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 19:28:10 +0600 Subject: [PATCH] update --- ForwarderBot/build.gradle | 16 +---- .../insanusmokrassar/examples/ForwarderBot.kt | 2 +- RandomFileSenderBot/README.md | 9 +++ RandomFileSenderBot/build.gradle | 24 +++++++ .../examples/RandomFileSenderBot.kt | 64 +++++++++++++++++++ gradle.properties | 5 +- settings.gradle | 1 + 7 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 RandomFileSenderBot/README.md create mode 100644 RandomFileSenderBot/build.gradle create mode 100644 RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt diff --git a/ForwarderBot/build.gradle b/ForwarderBot/build.gradle index a254060..75d9a52 100644 --- a/ForwarderBot/build.gradle +++ b/ForwarderBot/build.gradle @@ -1,38 +1,24 @@ buildscript { repositories { - mavenLocal() jcenter() - mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" } } -plugins { - id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" -} - apply plugin: 'kotlin' apply plugin: 'application' -project.version = "0.1.0" -project.group = "com.insanusmokrassar" - mainClassName="com.insanusmokrassar.examples.ForwarderBotKt" repositories { - mavenLocal() jcenter() - mavenCentral() } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" - implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$telegram_bot_api_version" + implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" } diff --git a/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt b/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt index 27dc02e..37d4127 100644 --- a/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt +++ b/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt @@ -33,7 +33,7 @@ suspend fun main(args: Array) { is UserForwardInfo -> forwardInfo.from.let { user -> "User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2() } - is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat as ChannelChat).title.codeMarkdownV2() + ")".regularMarkdownV2() + is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat).title.codeMarkdownV2() + ")".regularMarkdownV2() } } else { "There is no forward info" diff --git a/RandomFileSenderBot/README.md b/RandomFileSenderBot/README.md new file mode 100644 index 0000000..4765c04 --- /dev/null +++ b/RandomFileSenderBot/README.md @@ -0,0 +1,9 @@ +# RandomFileSenderBot + +This bot will send random file from input folder OR from bot working folder + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN[ optional/folder/path]" +``` diff --git a/RandomFileSenderBot/build.gradle b/RandomFileSenderBot/build.gradle new file mode 100644 index 0000000..fea5c0d --- /dev/null +++ b/RandomFileSenderBot/build.gradle @@ -0,0 +1,24 @@ +buildscript { + repositories { + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="com.insanusmokrassar.examples.RandomFileSenderBotKt" + +repositories { + jcenter() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" +} diff --git a/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt b/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt new file mode 100644 index 0000000..5a81e6e --- /dev/null +++ b/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt @@ -0,0 +1,64 @@ +package com.insanusmokrassar.examples + +import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.ChannelChat +import com.github.insanusmokrassar.TelegramBotAPI.types.message.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.TextContent +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.utils.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.startGettingOfUpdates +import kotlinx.coroutines.* + +/** + * This bot will always return message about forwarder. In cases when sent message was not a forward message it will + * send suitable message + */ +suspend fun main(args: Array) { + val botToken = args.first() + + val bot = KtorRequestsExecutor(TelegramAPIUrlsKeeper(botToken)) + + val scope = CoroutineScope(Dispatchers.Default) + + val callback: UpdateReceiver = { messageUpdate -> + val message = messageUpdate.data + val infoToSend = if (message is ContentMessage<*>) { + val content = message.content + when (content) { + is TextContent -> { + val commandSource = (content.entities.singleOrNull() ?.source as? BotCommandTextSource) + if (commandSource != null && commandSource.command == "sendFile") { + // TODO + } else { + bot.sendTextMessage(message.chat, "Send me /sendFile to get random file") + } + } + } + val forwardInfo = message.forwardInfo + when (forwardInfo) { + null -> "There is no forward info" + is AnonymousForwardInfo -> "Anonymous user which signed as \"${forwardInfo.senderName.codeMarkdownV2()}\"" + is UserForwardInfo -> forwardInfo.from.let { user -> + "User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2() + } + is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat as ChannelChat).title.codeMarkdownV2() + ")".regularMarkdownV2() + } + } else { + "There is no forward info" + } + bot.sendTextMessage(message.chat, infoToSend, MarkdownV2) + } + + bot.startGettingOfUpdates( + messageCallback = callback, + channelPostCallback = callback, + scope = scope + ) + + scope.coroutineContext[Job]!!.join() +} diff --git a/gradle.properties b/gradle.properties index d5add5a..bf733f3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,3 @@ -kotlin_version=1.3.61 -kotlin_coroutines_version=1.3.3 +kotlin_version=1.3.72 -telegram_bot_api_version=0.23.3 \ No newline at end of file +telegram_bot_api_version=0.27.5 diff --git a/settings.gradle b/settings.gradle index ef2162a..141a24b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ include ":ForwarderBot" +include ":RandomFileSenderBot"