Merge pull request #165 from InsanusMokrassar/4.0.0

4.0.0
This commit is contained in:
InsanusMokrassar 2022-11-09 13:40:37 +06:00 committed by GitHub
commit 910f892b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 31 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.expectations.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams 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.extensions.utils.formatting.*
import dev.inmo.tgbotapi.types.ChatId 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.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.utils.botCommand import dev.inmo.tgbotapi.utils.botCommand
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
sealed interface BotState : State sealed interface BotState : State
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState data class ExpectContentOrStopState(override val context: Pair<ChatId, MessageThreadId?>, val sourceMessage: CommonMessage<TextContent>) : BotState
data class StopState(override val context: ChatId) : BotState data class StopState(override val context: Pair<ChatId, MessageThreadId?>) : BotState
suspend fun main(args: Array<String>) { suspend fun main(args: Array<String>) {
val botToken = args.first() val botToken = args.first()
@ -39,30 +43,35 @@ suspend fun main(args: Array<String>) {
) { ) {
strictlyOn<ExpectContentOrStopState> { strictlyOn<ExpectContentOrStopState> {
send( send(
it.context it.context.first,
threadId = it.context.second
) { ) {
+"Send me some content or " + botCommand("stop") + " if you want to stop sending" +"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 val content = contentMessage.content
when { when {
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context) content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
else -> { else -> {
execute(content.createResend(it.context)) execute(content.createResend(it.context.first, messageThreadId = it.context.second))
it it
} }
} }
} }
strictlyOn<StopState> { 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 null
} }
command("start") { command(
startChain(ExpectContentOrStopState(it.chat.id, it)) "start"
) {
startChain(ExpectContentOrStopState(it.chat.id to it.threadIdOrNull, it))
} }
}.second.join() }.second.join()
} }

View File

@ -1,5 +1,16 @@
import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.getMe 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 * This is one of the most easiest bot - it will just print information about itself

View File

@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.utils.shortcuts.* import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
import kotlinx.coroutines.* import kotlinx.coroutines.*
suspend fun activateResenderBot( suspend fun activateResenderBot(
@ -20,33 +21,23 @@ suspend fun activateResenderBot(
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) { bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
onContentMessage( onContentMessage(
initialFilter = CommonMessageFilterExcludeMediaGroups, subcontextUpdatesFilter = MessageFilterByChat,
subcontextUpdatesFilter = MessageFilterByChat
) { ) {
val chat = it.chat val chat = it.chat
withTypingAction(chat) {
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId)) { val answer = withTypingAction(chat) {
executeUnsafe(
it.content.createResend(
chat.id,
messageThreadId = it.threadIdOrNull,
replyToMessageId = it.messageId
)
) {
it.forEach(print) it.forEach(print)
} }
} }
}
onVisualGallery { println("Answer info: $answer")
val chat = it.chat ?: return@onVisualGallery
withUploadPhotoAction(chat) {
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
}
}
onPlaylist {
val chat = it.chat ?: return@onPlaylist
withUploadDocumentAction(chat) {
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
}
}
onDocumentsGroup {
val chat = it.chat ?: return@onDocumentsGroup
withUploadDocumentAction(chat) {
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
}
} }
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {

View File

@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m
kotlin_version=1.7.20 kotlin_version=1.7.20
telegram_bot_api_version=3.3.1 telegram_bot_api_version=4.0.0
micro_utils_version=0.13.2 micro_utils_version=0.13.2
serialization_version=1.4.1 serialization_version=1.4.1
ktor_version=2.1.3 ktor_version=2.1.3