1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

add parseCommandsWithParams for text inputs

This commit is contained in:
InsanusMokrassar 2021-03-24 13:41:37 +06:00
parent 5b620014cb
commit 1a5d1cde78
3 changed files with 66 additions and 0 deletions

View File

@ -7,6 +7,8 @@
* `Kotlin`: `1.4.31` -> `1.4.32` * `Kotlin`: `1.4.31` -> `1.4.32`
* `MicroUtils`: `0.4.29` -> `0.4.30` * `MicroUtils`: `0.4.29` -> `0.4.30`
* `Klocks`: `2.0.6` -> `2.0.7` * `Klocks`: `2.0.6` -> `2.0.7`
* `Utils Extensions`:
* Add extensions `parseCommandsWithParams`
## 0.33.0 ## 0.33.0

View File

@ -6,6 +6,7 @@ interface Captioned {
val caption: String? val caption: String?
} }
@Deprecated("This interface is not used in library and will be removed soon")
interface CaptionedOutput : Captioned { interface CaptionedOutput : Captioned {
val parseMode: ParseMode? val parseMode: ParseMode?
} }

View File

@ -0,0 +1,63 @@
package dev.inmo.tgbotapi.extensions.utils.extensions
import dev.inmo.tgbotapi.CommonAbstracts.*
import dev.inmo.tgbotapi.types.MessageEntity.textsources.BotCommandTextSource
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
val defaultArgsSeparator = Regex(" ")
/**
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
*/
fun List<TextSource>.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
): MutableMap<String, Array<String>> {
val result = mutableMapOf<String, Array<String>>()
var currentBotCommandSource: BotCommandTextSource? = null
var currentArgs = ""
fun includeCurrent() = currentBotCommandSource ?.let {
result[it.command] = currentArgs.split(argsSeparator).toTypedArray()
currentArgs = ""
}
for (textSource in this) {
if (textSource is BotCommandTextSource) {
includeCurrent()
currentBotCommandSource = textSource
} else {
currentArgs += textSource.source
}
}
includeCurrent()
return result
}
/**
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
*/
fun TextedInput.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
) = textSources.parseCommandsWithParams(argsSeparator)
/**
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
*/
fun TextedOutput.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
) = entities ?.parseCommandsWithParams(argsSeparator) ?: emptyMap()
/**
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
*/
fun CaptionedInput.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
) = textSources.parseCommandsWithParams(argsSeparator)
fun ContentMessage<TextContent>.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
) = content.parseCommandsWithParams(argsSeparator)
fun <T> ContentMessage<T>.parseCommandsWithParams(
argsSeparator: Regex = defaultArgsSeparator
) where T : CaptionedInput, T : MessageContent = content.parseCommandsWithParams(argsSeparator)