mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-24 11:08:49 +00:00
update up to 0.35.9
This commit is contained in:
parent
16c27fa493
commit
6aa7fe151e
@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.extensions.api.send.reply
|
|||||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
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.command
|
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.formatting.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat
|
||||||
@ -15,14 +15,11 @@ 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.types.message.content.abstracts.MediaGroupContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
|
|
||||||
sealed interface State : State
|
sealed interface BotState : State
|
||||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : State
|
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||||
data class StopState(override val context: ChatId) : State
|
data class StopState(override val context: ChatId) : BotState
|
||||||
|
|
||||||
fun TextContent.containsStopCommand() = parseCommandsWithParams().keys.firstOrNull { it == "stop" } != null
|
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
@ -33,46 +30,23 @@ suspend fun main(args: Array<String>) {
|
|||||||
sendMessage(
|
sendMessage(
|
||||||
it.context,
|
it.context,
|
||||||
buildEntities {
|
buildEntities {
|
||||||
+"Send me some content or "
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||||
botCommand("stop")
|
|
||||||
+" if you want to stop sending"
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
val content = oneOf(
|
doInSubContext(stopOnCompletion = false) {
|
||||||
parallel {
|
val behaviourSubcontext = this
|
||||||
waitContentMessage(includeMediaGroups = false, filter = { message -> message.chat.id == it.context }).also(::println)
|
onContentMessage(
|
||||||
},
|
initialFilter = { message -> message.chat.id == it.context }
|
||||||
parallel {
|
) { message ->
|
||||||
waitMediaGroup { chat ?.id == it.context }.also(::println)
|
execute(message.content.createResend(it.context))
|
||||||
},
|
|
||||||
parallel {
|
|
||||||
waitText (filter = { it.content.containsStopCommand() }).also(::println)
|
|
||||||
}
|
}
|
||||||
).first()
|
onCommand("stop") {
|
||||||
|
behaviourSubcontext.cancel()
|
||||||
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 -> {
|
}.join()
|
||||||
|
|
||||||
reply(it.sourceMessage, "Ok, I got this content and now will resend it to you")
|
StopState(it.context)
|
||||||
execute(content.createResend(it.context))
|
|
||||||
|
|
||||||
it
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
sendMessage(it.context, "Unknown internal error")
|
|
||||||
it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
strictlyOn<StopState> {
|
strictlyOn<StopState> {
|
||||||
sendMessage(it.context, "You have stopped sending of content")
|
sendMessage(it.context, "You have stopped sending of content")
|
||||||
|
@ -18,7 +18,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
directoryOrFile.mkdirs()
|
directoryOrFile.mkdirs()
|
||||||
|
|
||||||
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onMedia(includeMediaGroups = true) {
|
onMedia(initialFilter = null) {
|
||||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||||
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||||
createNewFile()
|
createNewFile()
|
||||||
|
@ -17,7 +17,7 @@ suspend fun main(vararg args: String) {
|
|||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onContentMessage(includeMediaGroups = true) {
|
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
|
||||||
val toAnswer = buildEntities {
|
val toAnswer = buildEntities {
|
||||||
when (val forwardInfo = it.forwardInfo) {
|
when (val forwardInfo = it.forwardInfo) {
|
||||||
null -> +"There is no forward info"
|
null -> +"There is no forward info"
|
||||||
|
@ -36,7 +36,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
if (currentRoot.isFile) {
|
if (currentRoot.isFile) {
|
||||||
return currentRoot
|
return currentRoot
|
||||||
} else {
|
} else {
|
||||||
return pickFile(currentRoot.listFiles() ?.random() ?: return null)
|
return pickFile(currentRoot.listFiles() ?.takeIf { it.isNotEmpty() } ?.random() ?: return null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,8 +76,10 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (chosen.isNotEmpty()) {
|
||||||
sendFiles(message.chat, chosen)
|
sendFiles(message.chat, chosen)
|
||||||
sent || chosen.isNotEmpty()
|
sent = true
|
||||||
|
}
|
||||||
|
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
bot.reply(message, "Nothing selected :(")
|
bot.reply(message, "Nothing selected :(")
|
||||||
|
@ -2,7 +2,9 @@ import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
|
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.behaviour_builder.utils.plus
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
@ -18,7 +20,7 @@ suspend fun activateResenderBot(
|
|||||||
|
|
||||||
bot.buildBehaviour(CoroutineScope(coroutineContext + SupervisorJob())) {
|
bot.buildBehaviour(CoroutineScope(coroutineContext + SupervisorJob())) {
|
||||||
onContentMessage(
|
onContentMessage(
|
||||||
additionalFilter = { it !is MediaGroupMessage<*> }
|
subcontextUpdatesFilter = MessageFilterByChat + BehaviourContextAndTwoTypesReceiver { it, _ -> it !is MediaGroupMessage<*> }
|
||||||
) {
|
) {
|
||||||
executeUnsafe(it.content.createResend(it.chat.id, replyToMessageId = it.messageId))
|
executeUnsafe(it.content.createResend(it.chat.id, replyToMessageId = it.messageId))
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,6 @@ kotlin.code.style=official
|
|||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.5.30
|
kotlin_version=1.5.31
|
||||||
telegram_bot_api_version=0.35.8
|
telegram_bot_api_version=0.35.9
|
||||||
micro_utils_version=0.5.26
|
micro_utils_version=0.5.28
|
||||||
|
Loading…
Reference in New Issue
Block a user