mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
LocationContent -> LiveLocationContent / StaticLocationContent
This commit is contained in:
parent
1fbaf396aa
commit
46cc37be62
@ -7,6 +7,7 @@
|
|||||||
* `MicroUtils`: `0.5.25` -> `0.5.26`
|
* `MicroUtils`: `0.5.25` -> `0.5.26`
|
||||||
* `Core`:
|
* `Core`:
|
||||||
* New `BotAction` implementation - `CustomBotAction`
|
* New `BotAction` implementation - `CustomBotAction`
|
||||||
|
* `LocationContent` has been divided to two different types: `LiveLocationContent` and `StaticLocationContent`
|
||||||
|
|
||||||
## 0.35.8
|
## 0.35.8
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.types.*
|
|||||||
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
|
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
||||||
import dev.inmo.tgbotapi.types.message.content.LocationContent
|
import dev.inmo.tgbotapi.types.message.content.*
|
||||||
import dev.inmo.tgbotapi.utils.throwRangeError
|
import dev.inmo.tgbotapi.utils.throwRangeError
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
@ -3,35 +3,73 @@ package dev.inmo.tgbotapi.types.message.content
|
|||||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
import dev.inmo.tgbotapi.requests.send.SendLiveLocation
|
import dev.inmo.tgbotapi.requests.send.SendLiveLocation
|
||||||
import dev.inmo.tgbotapi.requests.send.SendStaticLocation
|
import dev.inmo.tgbotapi.requests.send.SendStaticLocation
|
||||||
|
import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
|
||||||
import dev.inmo.tgbotapi.types.ChatIdentifier
|
import dev.inmo.tgbotapi.types.ChatIdentifier
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||||
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
|
import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup
|
||||||
import dev.inmo.tgbotapi.types.location.*
|
import dev.inmo.tgbotapi.types.location.*
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
|
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
|
||||||
|
import kotlinx.serialization.encoding.*
|
||||||
|
|
||||||
@Serializable
|
@Serializable(LocationContentSerializer::class)
|
||||||
data class LocationContent(
|
sealed interface LocationContent : MessageContent {
|
||||||
val location: Location
|
val location: Location
|
||||||
) : MessageContent {
|
|
||||||
|
companion object {
|
||||||
|
operator fun invoke(location: Location): LocationContent {
|
||||||
|
return when (location) {
|
||||||
|
is StaticLocation -> StaticLocationContent(location)
|
||||||
|
is LiveLocation -> LiveLocationContent(location)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializer(LocationContent::class)
|
||||||
|
object LocationContentSerializer : KSerializer<LocationContent> {
|
||||||
|
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("LocationContent") {
|
||||||
|
element(LocationContent::location.name, LocationSerializer.descriptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun deserialize(decoder: Decoder): LocationContent {
|
||||||
|
lateinit var location: Location
|
||||||
|
|
||||||
|
decoder.decodeStructure(descriptor) {
|
||||||
|
while (true) {
|
||||||
|
when (val index = decodeElementIndex(descriptor)) {
|
||||||
|
0 -> location = decodeSerializableElement(descriptor, index, LocationSerializer)
|
||||||
|
CompositeDecoder.DECODE_DONE -> break
|
||||||
|
else -> error("Unexpected index: $index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return LocationContent(location)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, value: LocationContent) {
|
||||||
|
encoder.beginStructure(descriptor).apply {
|
||||||
|
encodeSerializableElement(descriptor, 0, LocationSerializer, value.location)
|
||||||
|
}.endStructure(descriptor)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable(LocationContentSerializer::class)
|
||||||
|
data class LiveLocationContent(
|
||||||
|
override val location: LiveLocation
|
||||||
|
) : LocationContent {
|
||||||
override fun createResend(
|
override fun createResend(
|
||||||
chatId: ChatIdentifier,
|
chatId: ChatIdentifier,
|
||||||
disableNotification: Boolean,
|
disableNotification: Boolean,
|
||||||
replyToMessageId: MessageIdentifier?,
|
replyToMessageId: MessageIdentifier?,
|
||||||
allowSendingWithoutReply: Boolean?,
|
allowSendingWithoutReply: Boolean?,
|
||||||
replyMarkup: KeyboardMarkup?
|
replyMarkup: KeyboardMarkup?
|
||||||
): Request<ContentMessage<LocationContent>> = when (location) {
|
): Request<ContentMessage<LiveLocationContent>> = SendLiveLocation(
|
||||||
is StaticLocation -> SendStaticLocation(
|
|
||||||
chatId,
|
|
||||||
location.latitude,
|
|
||||||
location.longitude,
|
|
||||||
disableNotification,
|
|
||||||
replyToMessageId,
|
|
||||||
allowSendingWithoutReply,
|
|
||||||
replyMarkup
|
|
||||||
)
|
|
||||||
is LiveLocation -> SendLiveLocation(
|
|
||||||
chatId,
|
chatId,
|
||||||
location.latitude,
|
location.latitude,
|
||||||
location.longitude,
|
location.longitude,
|
||||||
@ -43,6 +81,26 @@ data class LocationContent(
|
|||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
allowSendingWithoutReply,
|
allowSendingWithoutReply,
|
||||||
replyMarkup
|
replyMarkup
|
||||||
)
|
) as SendMessageRequest<ContentMessage<LiveLocationContent>>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable(LocationContentSerializer::class)
|
||||||
|
data class StaticLocationContent(
|
||||||
|
override val location: StaticLocation
|
||||||
|
) : LocationContent {
|
||||||
|
override fun createResend(
|
||||||
|
chatId: ChatIdentifier,
|
||||||
|
disableNotification: Boolean,
|
||||||
|
replyToMessageId: MessageIdentifier?,
|
||||||
|
allowSendingWithoutReply: Boolean?,
|
||||||
|
replyMarkup: KeyboardMarkup?
|
||||||
|
): Request<ContentMessage<StaticLocationContent>> = SendStaticLocation(
|
||||||
|
chatId,
|
||||||
|
location.latitude,
|
||||||
|
location.longitude,
|
||||||
|
disableNotification,
|
||||||
|
replyToMessageId,
|
||||||
|
allowSendingWithoutReply,
|
||||||
|
replyMarkup
|
||||||
|
) as SendMessageRequest<ContentMessage<StaticLocationContent>>
|
||||||
}
|
}
|
@ -27,6 +27,7 @@ class BotActionTests {
|
|||||||
FindLocationAction -> example.botAction.actionName
|
FindLocationAction -> example.botAction.actionName
|
||||||
RecordVideoNoteAction -> example.botAction.actionName
|
RecordVideoNoteAction -> example.botAction.actionName
|
||||||
UploadVideoNoteAction -> example.botAction.actionName
|
UploadVideoNoteAction -> example.botAction.actionName
|
||||||
|
is CustomBotAction -> example.botAction.actionName
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -2506,6 +2506,24 @@ inline fun ResendableContent.asLocationContent(): LocationContent? = this as? Lo
|
|||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
inline fun ResendableContent.requireLocationContent(): LocationContent = this as LocationContent
|
inline fun ResendableContent.requireLocationContent(): LocationContent = this as LocationContent
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun <T> ResendableContent.whenLiveLocationContent(block: (LiveLocationContent) -> T) = asLiveLocationContent() ?.let(block)
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun ResendableContent.asLiveLocationContent(): LiveLocationContent? = this as? LiveLocationContent
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun ResendableContent.requireLiveLocationContent(): LiveLocationContent = this as LiveLocationContent
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun <T> ResendableContent.whenStaticLocationContent(block: (StaticLocationContent) -> T) = asStaticLocationContent() ?.let(block)
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun ResendableContent.asStaticLocationContent(): StaticLocationContent? = this as? StaticLocationContent
|
||||||
|
|
||||||
|
@PreviewFeature
|
||||||
|
inline fun ResendableContent.requireStaticLocationContent(): StaticLocationContent = this as StaticLocationContent
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
inline fun <T> ResendableContent.whenPollContent(block: (PollContent) -> T) = asPollContent() ?.let(block)
|
inline fun <T> ResendableContent.whenPollContent(block: (PollContent) -> T) = asPollContent() ?.let(block)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user