1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-01 23:45:25 +00:00
tgbotapi/TelegramBotAPI-core/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt

86 lines
3.2 KiB
Kotlin
Raw Normal View History

2018-12-26 08:07:24 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media
2019-01-17 02:43:51 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.*
2018-12-26 08:07:24 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.PhotoContent
2020-06-01 05:53:33 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.throwRangeError
2018-12-26 08:07:24 +00:00
import kotlinx.serialization.*
fun SendPhoto(
chatId: ChatIdentifier,
photo: InputFile,
caption: String? = null,
parseMode: ParseMode? = null,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
replyMarkup: KeyboardMarkup? = null
): Request<ContentMessage<PhotoContent>> {
2018-12-26 08:07:24 +00:00
val data = SendPhotoData(
chatId,
(photo as? FileId) ?.fileId,
caption,
parseMode,
disableNotification,
replyToMessageId,
replyMarkup
)
return data.photo ?.let {
data
} ?: MultipartRequestImpl(
data,
SendPhotoFiles(photo as MultipartFile)
)
}
private val commonResultDeserializer: DeserializationStrategy<ContentMessage<PhotoContent>>
= TelegramBotAPIMessageDeserializationStrategyClass()
2018-12-26 08:07:24 +00:00
@Serializable
data class SendPhotoData internal constructor(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(photoField)
val photo: String? = null,
@SerialName(captionField)
override val text: String? = null,
@SerialName(parseModeField)
override val parseMode: ParseMode? = null,
@SerialName(disableNotificationField)
override val disableNotification: Boolean = false,
@SerialName(replyToMessageIdField)
override val replyToMessageId: MessageIdentifier? = null,
@SerialName(replyMarkupField)
override val replyMarkup: KeyboardMarkup? = null
) : DataRequest<ContentMessage<PhotoContent>>,
SendMessageRequest<ContentMessage<PhotoContent>>,
ReplyingMarkupSendMessageRequest<ContentMessage<PhotoContent>>,
TextableSendMessageRequest<ContentMessage<PhotoContent>>
2018-12-26 08:07:24 +00:00
{
init {
text ?.let {
if (it.length !in captionLength) {
2020-06-01 05:53:33 +00:00
throwRangeError("Caption length", captionLength, it.length)
}
}
}
2018-12-26 08:07:24 +00:00
override fun method(): String = "sendPhoto"
override val resultDeserializer: DeserializationStrategy<ContentMessage<PhotoContent>>
get() = commonResultDeserializer
2019-12-02 08:35:37 +00:00
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
2018-12-26 08:07:24 +00:00
}
data class SendPhotoFiles internal constructor(
val photo: MultipartFile
) : Files by mapOf(
photoField to photo
)