mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 23:29:33 +00:00
Merge branch 'master' into 9.3.0
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.extensions
|
||||
|
||||
import dev.inmo.tgbotapi.abstracts.TextedWithTextSources
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
import dev.inmo.tgbotapi.types.message.textsources.TextSource
|
||||
|
||||
object TelegramBotCommandsDefaults {
|
||||
const val defaultArgsSeparator = " "
|
||||
val defaultArgsSeparatorRegex = Regex(defaultArgsSeparator)
|
||||
const val defaultNamesArgsSeparator = "="
|
||||
val defaultNamesArgsSeparatorRegex = Regex(defaultNamesArgsSeparator)
|
||||
}
|
||||
|
||||
@Deprecated(message = "Replaced", replaceWith = ReplaceWith("TelegramBotCommandsDefaults.defaultArgsSeparatorRegex", "dev.inmo.tgbotapi.extensions.utils.extensions.TelegramBotCommandsDefaults"))
|
||||
val defaultArgsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun List<TextSource>.parseCommandsWithArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
): MutableMap<String, Array<String>> {
|
||||
val result = mutableMapOf<String, Array<String>>()
|
||||
var currentBotCommandSource: BotCommandTextSource? = null
|
||||
var currentArgs = ""
|
||||
|
||||
fun includeCurrent() = currentBotCommandSource?.let {
|
||||
currentArgs = currentArgs.trim()
|
||||
result[it.command] = if (currentArgs.isNotEmpty()) {
|
||||
currentArgs.split(argsSeparator).toTypedArray()
|
||||
} else {
|
||||
emptyArray()
|
||||
}
|
||||
currentArgs = ""
|
||||
currentBotCommandSource = null
|
||||
}
|
||||
|
||||
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 TextedWithTextSources.parseCommandsWithArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
) = textSources?.parseCommandsWithArgs(argsSeparator) ?: emptyMap()
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
) = content.parseCommandsWithArgs(argsSeparator)
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun List<TextSource>.parseCommandsWithArgs(
|
||||
argsSeparator: String
|
||||
): MutableMap<String, Array<String>> = parseCommandsWithArgs(Regex(argsSeparator))
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun TextedWithTextSources.parseCommandsWithArgs(
|
||||
argsSeparator: String
|
||||
) = parseCommandsWithArgs(Regex(argsSeparator))
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithArgs(
|
||||
argsSeparator: String
|
||||
) = parseCommandsWithArgs(Regex(argsSeparator))
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun List<TextSource>.parseCommandsWithNamedArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
): Map<String, List<Pair<String, String>>> {
|
||||
val withArgs = parseCommandsWithArgs(argsSeparator)
|
||||
|
||||
return withArgs.mapValues { (k, v) ->
|
||||
v.flatMap {
|
||||
it.split(nameArgSeparator, 2).map { v -> it to v }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun TextedWithTextSources.parseCommandsWithNamedArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
) = textSources?.parseCommandsWithNamedArgs(argsSeparator = argsSeparator, nameArgSeparator = nameArgSeparator) ?: emptyMap()
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithNamedArgs(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
) = content.parseCommandsWithNamedArgs(argsSeparator = argsSeparator, nameArgSeparator = nameArgSeparator)
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun List<TextSource>.parseCommandsWithNamedArgs(
|
||||
argsSeparator: String,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
): Map<String, List<Pair<String, String>>> = parseCommandsWithNamedArgs(Regex(pattern = argsSeparator), nameArgSeparator)
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun TextedWithTextSources.parseCommandsWithNamedArgs(
|
||||
argsSeparator: String,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
) = parseCommandsWithNamedArgs(argsSeparator = Regex(pattern = argsSeparator), nameArgSeparator = nameArgSeparator)
|
||||
|
||||
/**
|
||||
* Uses [parseCommandsWithArgs] to create base [argsSeparator] split args for commands and map their as k-v pairs.
|
||||
* Sample:
|
||||
*
|
||||
* ```bash
|
||||
* /command args1=value1 arg2=value2 arg1=value3
|
||||
* ```
|
||||
*
|
||||
* Will produce [Map] with one key `command` and the list of three pairs:
|
||||
*
|
||||
* 1. `args1` to `value1`
|
||||
* 2. `args2` to `value2`
|
||||
* 3. `args1` to `value3`
|
||||
*
|
||||
* @return Array of named arguments
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithNamedArgs(
|
||||
argsSeparator: String,
|
||||
nameArgSeparator: Regex = TelegramBotCommandsDefaults.defaultNamesArgsSeparatorRegex,
|
||||
) = parseCommandsWithNamedArgs(argsSeparator = Regex(pattern = argsSeparator), nameArgSeparator = nameArgSeparator)
|
||||
|
||||
|
||||
// Deprecations
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
@Deprecated("Renamed", ReplaceWith("parseCommandsWithArgs(argsSeparator)", "dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs"))
|
||||
fun List<TextSource>.parseCommandsWithParams(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
): MutableMap<String, Array<String>> = parseCommandsWithArgs(argsSeparator)
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
@Deprecated("Renamed", ReplaceWith("parseCommandsWithArgs(argsSeparator)", "dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs"))
|
||||
fun TextedWithTextSources.parseCommandsWithParams(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
) = parseCommandsWithArgs(argsSeparator)
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
@Deprecated("Renamed", ReplaceWith("parseCommandsWithArgs(argsSeparator)", "dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs"))
|
||||
fun ContentMessage<TextContent>.parseCommandsWithParams(
|
||||
argsSeparator: Regex = TelegramBotCommandsDefaults.defaultArgsSeparatorRegex
|
||||
) = parseCommandsWithArgs(argsSeparator)
|
@@ -0,0 +1,53 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.extensions
|
||||
|
||||
import dev.inmo.tgbotapi.abstracts.TextedWithTextSources
|
||||
import dev.inmo.tgbotapi.extensions.utils.botCommandTextSourceOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifBotCommandTextSource
|
||||
import dev.inmo.tgbotapi.extensions.utils.whenBotCommandTextSource
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
import dev.inmo.tgbotapi.types.message.textsources.TextSource
|
||||
|
||||
// Sources
|
||||
|
||||
/**
|
||||
* Parse text sources to find commands with their arguments. This method will skip all the text sources __before__
|
||||
* first command and all following text sources until the next command will be guessed as an args of last found command
|
||||
*/
|
||||
fun List<TextSource>.parseCommandsWithArgsSources(): Map<BotCommandTextSource, Array<TextSource>> {
|
||||
var currentCommandTextSource: BotCommandTextSource? = null
|
||||
val currentArgs = mutableListOf<TextSource>()
|
||||
val result = mutableMapOf<BotCommandTextSource, Array<TextSource>>()
|
||||
|
||||
fun addCurrentCommandToResult() {
|
||||
currentCommandTextSource ?.let {
|
||||
result[it] = currentArgs.toTypedArray()
|
||||
currentArgs.clear()
|
||||
}
|
||||
}
|
||||
|
||||
forEach {
|
||||
it.whenBotCommandTextSource {
|
||||
addCurrentCommandToResult()
|
||||
currentCommandTextSource = it
|
||||
return@forEach
|
||||
}
|
||||
currentArgs.add(it)
|
||||
}
|
||||
addCurrentCommandToResult()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse text sources to find commands with their arguments. This method will skip all the text sources __before__
|
||||
* first command and all following text sources until the next command will be guessed as an args of last found command
|
||||
*/
|
||||
fun TextedWithTextSources.parseCommandsWithArgsSources() = textSources?.parseCommandsWithArgsSources() ?: emptyMap()
|
||||
|
||||
/**
|
||||
* Parse text sources to find commands with their arguments. This method will skip all the text sources __before__
|
||||
* first command and all following text sources until the next command will be guessed as an args of last found command
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithArgsSources() = content.parseCommandsWithArgsSources()
|
@@ -1,54 +0,0 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.extensions
|
||||
|
||||
import dev.inmo.tgbotapi.abstracts.TextedWithTextSources
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
import dev.inmo.tgbotapi.types.message.textsources.TextSource
|
||||
|
||||
|
||||
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 {
|
||||
currentArgs = currentArgs.trim()
|
||||
result[it.command] = if (currentArgs.isNotEmpty()) {
|
||||
currentArgs.split(argsSeparator).toTypedArray()
|
||||
} else {
|
||||
emptyArray()
|
||||
}
|
||||
currentArgs = ""
|
||||
currentBotCommandSource = null
|
||||
}
|
||||
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 TextedWithTextSources.parseCommandsWithParams(
|
||||
argsSeparator: Regex = defaultArgsSeparator
|
||||
) = textSources ?.parseCommandsWithParams(argsSeparator) ?: emptyMap()
|
||||
|
||||
/**
|
||||
* Parse commands and their args. Logic will find command, get all subsequent data as args until new command
|
||||
*/
|
||||
fun ContentMessage<TextContent>.parseCommandsWithParams(
|
||||
argsSeparator: Regex = defaultArgsSeparator
|
||||
) = content.parseCommandsWithParams(argsSeparator)
|
Reference in New Issue
Block a user