From d49ce88a0b6e5992b0da77d972aaa7a7ad438995 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 Nov 2020 22:35:05 +0600 Subject: [PATCH] update and add SlotMachineDetectorBot --- SlotMachineDetectorBot/README.md | 9 +++++ SlotMachineDetectorBot/build.gradle | 24 +++++++++++++ .../src/main/kotlin/SlotMachineDetectorBot.kt | 35 +++++++++++++++++++ gradle.properties | 2 +- settings.gradle | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 SlotMachineDetectorBot/README.md create mode 100644 SlotMachineDetectorBot/build.gradle create mode 100644 SlotMachineDetectorBot/src/main/kotlin/SlotMachineDetectorBot.kt diff --git a/SlotMachineDetectorBot/README.md b/SlotMachineDetectorBot/README.md new file mode 100644 index 0000000..b793749 --- /dev/null +++ b/SlotMachineDetectorBot/README.md @@ -0,0 +1,9 @@ +# SlotMachineDetectorBot + +This bot must reply with information about slot machine answer + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/SlotMachineDetectorBot/build.gradle b/SlotMachineDetectorBot/build.gradle new file mode 100644 index 0000000..24a13b9 --- /dev/null +++ b/SlotMachineDetectorBot/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="SlotMachineDetectorBotKt" + +repositories { + jcenter() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/SlotMachineDetectorBot/src/main/kotlin/SlotMachineDetectorBot.kt b/SlotMachineDetectorBot/src/main/kotlin/SlotMachineDetectorBot.kt new file mode 100644 index 0000000..ab02785 --- /dev/null +++ b/SlotMachineDetectorBot/src/main/kotlin/SlotMachineDetectorBot.kt @@ -0,0 +1,35 @@ +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage +import dev.inmo.tgbotapi.extensions.api.telegramBot +import dev.inmo.tgbotapi.extensions.utils.* +import dev.inmo.tgbotapi.extensions.utils.shortcuts.filterContentMessages +import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType +import dev.inmo.tgbotapi.types.message.content.DiceContent +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach + +suspend fun main(args: Array) { + val bot = telegramBot(args.first()) + + val scope = CoroutineScope(Dispatchers.Default) + bot.startGettingFlowsUpdatesByLongPolling(scope = scope) { + filterContentMessages(scope).onEach { + val content = it.content + val dice = content.dice + val diceType = dice.animationType + + safely ({ it.printStackTrace() }) { + if (diceType == SlotMachineDiceAnimationType) { + val result = dice.calculateSlotMachineResult() ?: return@safely + bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}") + } else { + bot.reply(it, "There is no slot machine dice in message") + } + } + }.launchIn(scope) + } + + scope.coroutineContext[Job]!!.join() +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index ce1aa01..8af1b2b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,4 +2,4 @@ kotlin.code.style=official org.gradle.parallel=true kotlin_version=1.4.10 -telegram_bot_api_version=0.29.3 +telegram_bot_api_version=0.29.4 diff --git a/settings.gradle b/settings.gradle index 0506b87..b12d2dd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,3 +5,4 @@ include ":GetMeBot" include ":FilesLoaderBot" include ":ResenderBot:ResenderBotLib" include ":ResenderBot:jvm_launcher" +include ":SlotMachineDetectorBot"