From 43f50b090a48d757577e269ffc1a29c43e20a2ab Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 20:24:41 +0600 Subject: [PATCH] fixes --- ForwarderBot/build.gradle | 2 +- ForwarderBot/src/main/kotlin/ForwarderBot.kt | 42 ++++++++++++ .../insanusmokrassar/examples/ForwarderBot.kt | 51 --------------- RandomFileSenderBot/build.gradle | 2 +- .../src/main/kotlin/RandomFileSenderBot.kt | 55 ++++++++++++++++ .../examples/RandomFileSenderBot.kt | 64 ------------------- 6 files changed, 99 insertions(+), 117 deletions(-) create mode 100644 ForwarderBot/src/main/kotlin/ForwarderBot.kt delete mode 100644 ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt create mode 100644 RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt delete mode 100644 RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt diff --git a/ForwarderBot/build.gradle b/ForwarderBot/build.gradle index 75d9a52..f2e950f 100644 --- a/ForwarderBot/build.gradle +++ b/ForwarderBot/build.gradle @@ -11,7 +11,7 @@ buildscript { apply plugin: 'kotlin' apply plugin: 'application' -mainClassName="com.insanusmokrassar.examples.ForwarderBotKt" +mainClassName="ForwarderBotKt" repositories { jcenter() diff --git a/ForwarderBot/src/main/kotlin/ForwarderBot.kt b/ForwarderBot/src/main/kotlin/ForwarderBot.kt new file mode 100644 index 0000000..6980ea7 --- /dev/null +++ b/ForwarderBot/src/main/kotlin/ForwarderBot.kt @@ -0,0 +1,42 @@ +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.codeMarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.regularMarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.types.message.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.PossiblyForwardedMessage +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* + +/** + * 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(vararg args: String) { + val botToken = args.first() + + val bot = telegramBot(botToken) + + val scope = CoroutineScope(Dispatchers.Default) + + bot.startGettingFlowsUpdatesByLongPolling(scope = scope) { + messageFlow.asContentMessagesFlow().mapNotNull { it as? PossiblyForwardedMessage }.onEach { message -> + safely { + val toAnswer = when (val forwardInfo = message.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).title.codeMarkdownV2() + ")".regularMarkdownV2() + } + bot.sendTextMessage(message.chat, toAnswer, MarkdownV2) + } + }.launchIn(scope) + } + + scope.coroutineContext[Job]!!.join() +} diff --git a/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt b/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt deleted file mode 100644 index 37d4127..0000000 --- a/ForwarderBot/src/main/kotlin/com/insanusmokrassar/examples/ForwarderBot.kt +++ /dev/null @@ -1,51 +0,0 @@ -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.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.PossiblyForwardedMessage -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 PossiblyForwardedMessage) { - 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).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/RandomFileSenderBot/build.gradle b/RandomFileSenderBot/build.gradle index fea5c0d..232fb29 100644 --- a/RandomFileSenderBot/build.gradle +++ b/RandomFileSenderBot/build.gradle @@ -11,7 +11,7 @@ buildscript { apply plugin: 'kotlin' apply plugin: 'application' -mainClassName="com.insanusmokrassar.examples.RandomFileSenderBotKt" +mainClassName="RandomFileSenderBotKt" repositories { jcenter() diff --git a/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt b/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt new file mode 100644 index 0000000..eba2b42 --- /dev/null +++ b/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt @@ -0,0 +1,55 @@ +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.setMyCommands +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.media.sendDocument +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.filterExactCommands +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.toInputFile +import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import java.io.File + +private const val command = "send_file" + +/** + * This bot will send files inside of working directory OR from directory in the second argument + */ +suspend fun main(args: Array) { + val botToken = args.first() + val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("") + + fun pickFile(currentRoot: File = directoryOrFile): File? { + if (currentRoot.isFile) { + return currentRoot + } else { + return pickFile(currentRoot.listFiles() ?.random() ?: return null) + } + } + + val bot = telegramBot(botToken) + val scope = CoroutineScope(Dispatchers.Default) + + bot.startGettingFlowsUpdatesByLongPolling(scope = scope) { + messageFlow.filterExactCommands(Regex(command)).onEach { message -> + safely { + pickFile() ?.let { + bot.sendDocument( + message.chat.id, + it.toInputFile() + ) + } ?: bot.sendTextMessage(message.chat.id, "Nothing selected :(") + } + }.launchIn(scope) + } + + safely { + bot.setMyCommands( + BotCommand(command, "Send some random file in picker directory") + ) + } + + scope.coroutineContext[Job]!!.join() +} diff --git a/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt b/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt deleted file mode 100644 index 5a81e6e..0000000 --- a/RandomFileSenderBot/src/main/kotlin/com/insanusmokrassar/examples/RandomFileSenderBot.kt +++ /dev/null @@ -1,64 +0,0 @@ -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() -}