mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-21 15:53:53 +00:00
add example with oneof and fsm
This commit is contained in:
parent
d062ab86ae
commit
522d1b55ba
10
FSMBot/README.md
Normal file
10
FSMBot/README.md
Normal file
@ -0,0 +1,10 @@
|
||||
# FSM
|
||||
|
||||
This bot contains an example of working with FSM included in project
|
||||
[MicroUtils](https://github.com/InsanusMokrassar/MicroUtils)
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
22
FSMBot/build.gradle
Normal file
22
FSMBot/build.gradle
Normal file
@ -0,0 +1,22 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="HelloBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
implementation "dev.inmo:micro_utils.fsm.common:$micro_utils_version"
|
||||
}
|
90
FSMBot/src/main/kotlin/SimpleFSMBot.kt
Normal file
90
FSMBot/src/main/kotlin/SimpleFSMBot.kt
Normal file
@ -0,0 +1,90 @@
|
||||
import dev.inmo.micro_utils.fsm.common.State
|
||||
import dev.inmo.micro_utils.fsm.common.dsl.buildFSM
|
||||
import dev.inmo.micro_utils.fsm.common.dsl.strictlyOn
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendMediaGroup
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.command
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
sealed interface State : State
|
||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : State
|
||||
data class StopState(override val context: ChatId) : State
|
||||
|
||||
fun TextContent.containsStopCommand() = parseCommandsWithParams().keys.firstOrNull { it == "stop" } != null
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
val fsm = buildFSM {
|
||||
strictlyOn<ExpectContentOrStopState> {
|
||||
sendMessage(
|
||||
it.context,
|
||||
buildEntities {
|
||||
+"Send me some content or "
|
||||
botCommand("stop")
|
||||
+" if you want to stop sending"
|
||||
}
|
||||
)
|
||||
|
||||
val content = oneOf(
|
||||
parallel {
|
||||
waitContentMessage(includeMediaGroups = false) { if (chat.id == it.context) content else null }.also(::println)
|
||||
},
|
||||
parallel {
|
||||
waitMediaGroup { chat ?.id == it.context }.also(::println)
|
||||
},
|
||||
parallel {
|
||||
waitText { if (content.containsStopCommand()) content else null }.also(::println)
|
||||
}
|
||||
).first()
|
||||
|
||||
when {
|
||||
content is TextContent && content.containsStopCommand() -> StopState(it.context) // assume we got "stop" command
|
||||
content is List<*> -> { // assume it is media group
|
||||
val casted = (content as List<MediaGroupContent>)
|
||||
|
||||
reply(it.sourceMessage, "Ok, I got this media group and now will resend it to you")
|
||||
sendMediaGroup(it.context, casted.map { it.toMediaGroupMemberInputMedia() })
|
||||
|
||||
it
|
||||
}
|
||||
content is MessageContent -> {
|
||||
|
||||
reply(it.sourceMessage, "Ok, I got this content and now will resend it to you")
|
||||
execute(content.createResend(it.context))
|
||||
|
||||
it
|
||||
}
|
||||
else -> {
|
||||
sendMessage(it.context, "Unknown internal error")
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
strictlyOn<StopState> {
|
||||
sendMessage(it.context, "You have stopped sending of content")
|
||||
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
command("start") {
|
||||
fsm.startChain(ExpectContentOrStopState(it.chat.id, it))
|
||||
}
|
||||
|
||||
fsm.start(this)
|
||||
}.second.join()
|
||||
}
|
@ -2,4 +2,5 @@ kotlin.code.style=official
|
||||
org.gradle.parallel=true
|
||||
|
||||
kotlin_version=1.5.20
|
||||
telegram_bot_api_version=0.35.1
|
||||
telegram_bot_api_version=0.35.1-branch_0.35.1-build280
|
||||
micro_utils_version=0.5.15
|
||||
|
@ -6,3 +6,4 @@ include ":FilesLoaderBot"
|
||||
include ":ResenderBot:ResenderBotLib"
|
||||
include ":ResenderBot:jvm_launcher"
|
||||
include ":SlotMachineDetectorBot"
|
||||
include ":FSMBot"
|
||||
|
Loading…
Reference in New Issue
Block a user