This commit is contained in:
InsanusMokrassar 2022-11-08 17:49:41 +06:00
parent 3b26971152
commit 8232cb4d62
3 changed files with 29 additions and 9 deletions

View File

@ -6,17 +6,21 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
import dev.inmo.tgbotapi.extensions.utils.formatting.*
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.utils.botCommand
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
sealed interface BotState : State
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
data class StopState(override val context: ChatId) : BotState
data class ExpectContentOrStopState(override val context: Pair<ChatId, MessageThreadId?>, val sourceMessage: CommonMessage<TextContent>) : BotState
data class StopState(override val context: Pair<ChatId, MessageThreadId?>) : BotState
suspend fun main(args: Array<String>) {
val botToken = args.first()
@ -39,30 +43,35 @@ suspend fun main(args: Array<String>) {
) {
strictlyOn<ExpectContentOrStopState> {
send(
it.context
it.context.first,
threadId = it.context.second
) {
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
}
val contentMessage = waitContentMessage().first()
val contentMessage = waitContentMessage().filter { message ->
message.sameThread(it.sourceMessage)
}.first()
val content = contentMessage.content
when {
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
else -> {
execute(content.createResend(it.context))
execute(content.createResend(it.context.first, messageThreadId = it.context.second))
it
}
}
}
strictlyOn<StopState> {
send(it.context) { +"You have stopped sending of content" }
send(it.context.first, threadId = it.context.second) { +"You have stopped sending of content" }
null
}
command("start") {
startChain(ExpectContentOrStopState(it.chat.id, it))
command(
"start"
) {
startChain(ExpectContentOrStopState(it.chat.id to it.threadIdOrNull, it))
}
}.second.join()
}

View File

@ -1,5 +1,16 @@
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onForumTopicClosed
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.CustomEmojiId
import dev.inmo.tgbotapi.types.ForumTopic
import kotlinx.coroutines.delay
/**
* This is one of the most easiest bot - it will just print information about itself

View File

@ -21,7 +21,7 @@ suspend fun activateResenderBot(
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
onContentMessage(
subcontextUpdatesFilter = MessageFilterByChat
subcontextUpdatesFilter = MessageFilterByChat,
) {
val chat = it.chat