1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

include support of ietf language codes

This commit is contained in:
InsanusMokrassar 2021-08-05 23:36:01 +06:00
parent e25ce57f6a
commit 6b89c94ef1
12 changed files with 107 additions and 15 deletions

View File

@ -7,6 +7,8 @@
* `Klock`: `2.2.0` -> `2.3.1`
* `Ktor`: `1.6.1` -> `1.6.2`
* `MicroUtils`: `0.5.16` -> `0.5.18`
* `Core`:
* Support of strongly-typed ietf language codes has been added
* `API`:
* New extension `TelegramBot#downloadFile` for any `MediaContent`
* `Behaviour Builder`:

View File

@ -51,6 +51,7 @@ kotlin {
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
api "dev.inmo:micro_utils.serialization.encapsulator:$micro_utils_version"
api "dev.inmo:micro_utils.serialization.typed_serializer:$micro_utils_version"
api "dev.inmo:micro_utils.language_codes:$micro_utils_version"
api "io.ktor:ktor-client-core:$ktor_version"
}

View File

@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.CommonAbstracts
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
interface WithOptionalLanguageCode {
val ietfLanguageCode: IetfLanguageCode?
val languageCode: String?
get() = ietfLanguageCode ?.code
}
interface WithLanguageCode : WithOptionalLanguageCode {
override val ietfLanguageCode: IetfLanguageCode
override val languageCode: String
get() = ietfLanguageCode.code
}

View File

@ -1,9 +1,9 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.tgbotapi.CommonAbstracts.WithOptionalLanguageCode
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.commands.BotCommandScope
sealed interface MyCommandsRequest<T : Any> : SimpleRequest<T> {
sealed interface MyCommandsRequest<T : Any> : SimpleRequest<T>, WithOptionalLanguageCode {
val scope: BotCommandScope
val languageCode: String?
}

View File

@ -1,8 +1,9 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.micro_utils.language_codes.IetfLanguageCodeSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.commands.*
import dev.inmo.tgbotapi.types.languageCodeField
import dev.inmo.tgbotapi.types.scopeField
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@ -12,11 +13,20 @@ data class DeleteMyCommands(
@Serializable(BotCommandScopeSerializer::class)
override val scope: BotCommandScope = BotCommandScopeDefault,
@SerialName(languageCodeField)
override val languageCode: String? = null
@Serializable(IetfLanguageCodeSerializer::class)
override val ietfLanguageCode: IetfLanguageCode? = null
) : MyCommandsRequest<Boolean> {
override fun method(): String = "deleteMyCommands"
override val requestSerializer: SerializationStrategy<DeleteMyCommands> = serializer()
override val resultDeserializer: DeserializationStrategy<Boolean> = Boolean.serializer()
constructor(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String?
) : this(
scope,
languageCode ?.let(::IetfLanguageCode)
)
companion object : MyCommandsRequest<Boolean> by DeleteMyCommands()
}

View File

@ -1,5 +1,7 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.micro_utils.language_codes.IetfLanguageCodeSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.commands.*
import kotlinx.serialization.*
@ -13,7 +15,8 @@ data class GetMyCommands(
@Serializable(BotCommandScopeSerializer::class)
override val scope: BotCommandScope = BotCommandScopeDefault,
@SerialName(languageCodeField)
override val languageCode: String? = null
@Serializable(IetfLanguageCodeSerializer::class)
override val ietfLanguageCode: IetfLanguageCode? = null
) : MyCommandsRequest<List<BotCommand>> {
override fun method(): String = "getMyCommands"
override val resultDeserializer: DeserializationStrategy<List<BotCommand>>
@ -21,5 +24,13 @@ data class GetMyCommands(
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
constructor(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String?
) : this(
scope,
languageCode ?.let(::IetfLanguageCode)
)
companion object : MyCommandsRequest<List<BotCommand>> by GetMyCommands()
}

View File

@ -1,5 +1,7 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.micro_utils.language_codes.IetfLanguageCodeSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.commands.*
import kotlinx.serialization.*
@ -13,7 +15,8 @@ class SetMyCommands(
@Serializable(BotCommandScopeSerializer::class)
override val scope: BotCommandScope = BotCommandScopeDefault,
@SerialName(languageCodeField)
override val languageCode: String? = null
@Serializable(IetfLanguageCodeSerializer::class)
override val ietfLanguageCode: IetfLanguageCode? = null
) : MyCommandsRequest<Boolean> {
override fun method(): String = "setMyCommands"
override val resultDeserializer: DeserializationStrategy<Boolean>
@ -21,6 +24,16 @@ class SetMyCommands(
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
constructor(
commands: List<BotCommand>,
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String?
) : this(
commands,
scope,
languageCode ?.let(::IetfLanguageCode)
)
init {
if (commands.size !in botCommandsLimit) {
error("Bot commands list size able to be in range $botCommandsLimit, but incoming size is ${commands.size}")

View File

@ -1,5 +1,8 @@
package dev.inmo.tgbotapi.types
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.micro_utils.language_codes.IetfLanguageCodeSerializer
import dev.inmo.tgbotapi.CommonAbstracts.WithOptionalLanguageCode
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
import dev.inmo.tgbotapi.types.chat.extended.ExtendedPrivateChatImpl
import dev.inmo.tgbotapi.utils.*
@ -22,8 +25,17 @@ data class CommonUser(
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(languageCodeField)
val languageCode: String? = null
) : User()
@Serializable(IetfLanguageCodeSerializer::class)
override val ietfLanguageCode: IetfLanguageCode? = null
) : User(), WithOptionalLanguageCode {
constructor(
id: UserId,
firstName: String,
lastName: String = "",
username: Username? = null,
languageCode: String
) : this(id, firstName, lastName, username, IetfLanguageCode(languageCode))
}
@PreviewFeature
typealias ExtendedUser = ExtendedPrivateChatImpl

View File

@ -1,7 +1,8 @@
package dev.inmo.tgbotapi.types
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.tgbotapi.CommonAbstracts.WithLanguageCode
import java.util.*
fun CommonUser.javaLocale(): Locale? = languageCode ?.let {
Locale.forLanguageTag(it)
}
fun IetfLanguageCode?.javaLocale() = this ?.code ?.let { Locale.forLanguageTag(it) }
fun WithLanguageCode?.javaLocale() = this ?.ietfLanguageCode.javaLocale()

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.DeleteMyCommands
import dev.inmo.tgbotapi.types.commands.BotCommandScope
@ -7,5 +8,10 @@ import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault
suspend fun TelegramBot.deleteMyCommands(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
languageCode: IetfLanguageCode?
) = execute(DeleteMyCommands(scope, languageCode))
suspend fun TelegramBot.deleteMyCommands(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
) = deleteMyCommands(scope, languageCode ?.let(::IetfLanguageCode))

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.GetMyCommands
import dev.inmo.tgbotapi.types.commands.BotCommandScope
@ -7,5 +8,10 @@ import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault
suspend fun TelegramBot.getMyCommands(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
languageCode: IetfLanguageCode? = null
) = execute(GetMyCommands(scope, languageCode))
suspend fun TelegramBot.getMyCommands(
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
) = getMyCommands(scope, languageCode ?.let(::IetfLanguageCode))

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.SetMyCommands
import dev.inmo.tgbotapi.types.BotCommand
@ -9,9 +10,21 @@ import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault
suspend fun TelegramBot.setMyCommands(
commands: List<BotCommand>,
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
languageCode: IetfLanguageCode?
) = execute(SetMyCommands(commands, scope, languageCode))
suspend fun TelegramBot.setMyCommands(
vararg commands: BotCommand,
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: IetfLanguageCode?
) = setMyCommands(commands.toList(), scope, languageCode)
suspend fun TelegramBot.setMyCommands(
commands: List<BotCommand>,
scope: BotCommandScope = BotCommandScopeDefault,
languageCode: String? = null
) = setMyCommands(commands, scope, languageCode ?.let(::IetfLanguageCode))
suspend fun TelegramBot.setMyCommands(
vararg commands: BotCommand,
scope: BotCommandScope = BotCommandScopeDefault,