1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-29 13:38:43 +00:00

Merge pull request #37 from InsanusMokrassar/0.16.0

0.16.0
This commit is contained in:
InsanusMokrassar 2019-06-08 13:44:44 +08:00 committed by GitHub
commit 0d7fa8a1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 151 additions and 65 deletions

View File

@ -1,5 +1,14 @@
# TelegramBotAPI changelog # TelegramBotAPI changelog
## 0.16.0 Bot API 4.3
* `LoginURL` and `LoginURLInlineKeyboardButton` has been added
* `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface
* `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed
* `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file
* `String#asUsername` method renamed to `String#toUsername`
* Several `toChatId` extensions added
## 0.15.0 ## 0.15.0
* Old `UpdatesPoller` removed (was deprecated) * Old `UpdatesPoller` removed (was deprecated)

View File

@ -1,4 +1,4 @@
project.version = "0.15.0" project.version = "0.16.0"
project.group = "com.github.insanusmokrassar" project.group = "com.github.insanusmokrassar"
buildscript { buildscript {

View File

@ -20,6 +20,8 @@ val ChatId.link: String
typealias UserId = ChatId typealias UserId = ChatId
fun Identifier.toChatId(): ChatId = ChatId(this) fun Identifier.toChatId(): ChatId = ChatId(this)
fun Int.toChatId(): ChatId = toLong().toChatId()
fun Byte.toChatId(): ChatId = toLong().toChatId()
@Serializable(ChatIdentifierSerializer::class) @Serializable(ChatIdentifierSerializer::class)
data class Username( data class Username(
@ -32,7 +34,7 @@ data class Username(
} }
} }
fun String.asUsername(): Username = Username(this) fun String.toUsername(): Username = Username(this)
@Serializer(ChatIdentifier::class) @Serializer(ChatIdentifier::class)
internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> { internal class ChatIdentifierSerializer: KSerializer<ChatIdentifier> {

View File

@ -84,6 +84,14 @@ const val lastErrorDateField = "last_error_date"
const val lastErrorMessageField = "last_error_message" const val lastErrorMessageField = "last_error_message"
const val votesCountField = "voter_count" const val votesCountField = "voter_count"
const val isClosedField = "is_closed" const val isClosedField = "is_closed"
const val loginUrlField = "login_url"
const val forwardTextField = "forward_text"
const val botUsernameField = "bot_username"
const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat"
const val switchInlineQueryField = "switch_inline_query"
const val requestWriteAccessField = "request_write_access"
const val photoUrlField = "photo_url" const val photoUrlField = "photo_url"
@ -192,6 +200,7 @@ const val resultsField = "results"
const val certificateField = "certificate" const val certificateField = "certificate"
const val questionField = "question" const val questionField = "question"
const val optionsField = "options" const val optionsField = "options"
const val payField = "pay"
const val pointField = "point" const val pointField = "point"
const val xShiftField = "x_shift" const val xShiftField = "x_shift"

View File

@ -0,0 +1,16 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class LoginURL(
@SerialName(urlField)
val url: String,
@SerialName(forwardTextField)
val forwardText: String? = null,
@SerialName(botUsernameField)
val botUsername: String? = null,
@SerialName(requestWriteAccessField)
val requestWriteAccess: Boolean? = null
)

View File

@ -1,14 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.callbackDataField
import com.github.insanusmokrassar.TelegramBotAPI.types.textField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class CallbackDataInlineKeyboardButton(
@SerialName(textField)
override val text: String,
@SerialName(callbackDataField)
val callbackData: String
) : InlineKeyboardButton

View File

@ -1,10 +1,53 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
@Serializable(InlineKeyboardButtonSerializer::class) @Serializable(InlineKeyboardButtonSerializer::class)
interface InlineKeyboardButton { sealed class InlineKeyboardButton {
val text: String abstract val text: String
} }
object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> by ContextSerializer(InlineKeyboardButton::class) //TODO:: add check that this button first in a row (it MUST be first in a row)
@Serializable
data class PayInlineKeyboardButton(
override val text: String,
@SerialName(payField)
val pay: Boolean
) : InlineKeyboardButton()
@Serializable
data class CallbackDataInlineKeyboardButton(
@SerialName(textField)
override val text: String,
@SerialName(callbackDataField)
val callbackData: String
) : InlineKeyboardButton()
@Serializable
data class LoginURLInlineKeyboardButton(
override val text: String,
@SerialName(loginUrlField)
val loginUrl: LoginURL
) : InlineKeyboardButton()
@Serializable
data class SwitchInlineQueryCurrentChatInlineKeyboardButton(
override val text: String,
@SerialName(switchInlineQueryCurrentChatField)
val switchInlineQueryCurrentChat: String
) : InlineKeyboardButton()
@Serializable
data class SwitchInlineQueryInlineKeyboardButton(
override val text: String,
@SerialName(switchInlineQueryField)
val switchInlineQuery: String
) : InlineKeyboardButton()
@Serializable
data class URLInlineKeyboardButton(
override val text: String,
@SerialName(urlField)
val url: String
) : InlineKeyboardButton()

View File

@ -0,0 +1,39 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.*
object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton")
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton> {
return when {
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
json[payField] != null -> PayInlineKeyboardButton.serializer()
json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer()
json[urlField] != null -> URLInlineKeyboardButton.serializer()
else -> throw IllegalArgumentException("Can't find correct serializer for inline button serialized as $json")
}
}
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
val json = JsonElementSerializer.deserialize(decoder)
return Json.nonstrict.fromJson(resolveSerializer(json.jsonObject), json)
}
override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) {
when (obj) {
is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, obj)
is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, obj)
is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, obj)
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, obj)
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, obj)
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, obj)
}
}
}

View File

@ -1,10 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.Serializable
//TODO:: add check that this button first in a row (it MUST be first in a row)
@Serializable
data class PayInlineKeyboardButton(
override val text: String,
val pay: Boolean
) : InlineKeyboardButton

View File

@ -1,11 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SwitchInlineQueryCurrentChatInlineKeyboardButton(
override val text: String,
@SerialName("switch_inline_query_currentChat")
val switchInlineQueryCurrentChat: String
) : InlineKeyboardButton

View File

@ -1,11 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SwitchInlineQueryInlineKeyboardButton(
override val text: String,
@SerialName("switch_inline_query")
val switchInlineQuery: String
) : InlineKeyboardButton

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.Serializable
@Serializable
data class URLInlineKeyboardButton(
override val text: String,
val url: String
) : InlineKeyboardButton

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
@ -16,5 +17,6 @@ data class ChannelMediaGroupMessage(
override val content: MediaGroupContent, override val content: MediaGroupContent,
override val editDate: DateTime?, override val editDate: DateTime?,
override val forwarded: ForwardedMessage?, override val forwarded: ForwardedMessage?,
override val replyTo: Message? override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage ) : MediaGroupMessage

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.AuthorSignature import com.github.insanusmokrassar.TelegramBotAPI.types.AuthorSignature
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
@ -16,5 +17,6 @@ data class ChannelMessage<T: MessageContent>(
override val editDate: DateTime?, override val editDate: DateTime?,
override val forwarded: ForwardedMessage?, override val forwarded: ForwardedMessage?,
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?,
val authorSignature: AuthorSignature? val authorSignature: AuthorSignature?
) : CommonMessage<T> ) : CommonMessage<T>

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.message package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent
@ -15,5 +16,6 @@ data class CommonMediaGroupMessage(
override val content: MediaGroupContent, override val content: MediaGroupContent,
override val editDate: DateTime?, override val editDate: DateTime?,
override val forwarded: ForwardedMessage?, override val forwarded: ForwardedMessage?,
override val replyTo: Message? override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage, FromUserMessage ) : MediaGroupMessage, FromUserMessage

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.User import com.github.insanusmokrassar.TelegramBotAPI.types.User
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
@ -17,5 +18,6 @@ data class CommonMessageImpl<T: MessageContent>(
override val editDate: DateTime?, override val editDate: DateTime?,
override val forwarded: ForwardedMessage?, override val forwarded: ForwardedMessage?,
override val replyTo: Message?, override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?,
val paymentInfo: PaymentInfo? val paymentInfo: PaymentInfo?
) : Message, CommonMessage<T>, FromUserMessage ) : Message, CommonMessage<T>, FromUserMessage

View File

@ -3,6 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntities import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntities
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntitiesSerializer import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntitiesSerializer
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.* import com.github.insanusmokrassar.TelegramBotAPI.types.chat.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
@ -82,7 +83,9 @@ data class RawMessage(
private val connected_website: String? = null, private val connected_website: String? = null,
// passport property // passport property
private val passport_data: Unit? = null private val passport_data: Unit? = null,
private val reply_markup: InlineKeyboardMarkup? = null
) { ) {
@Transient @Transient
private val content: MessageContent? by lazy { private val content: MessageContent? by lazy {
@ -227,7 +230,8 @@ data class RawMessage(
}, },
edit_date ?.asDate, edit_date ?.asDate,
forwarded, forwarded,
reply_to_message ?.asMessage reply_to_message ?.asMessage,
reply_markup
) )
else -> CommonMediaGroupMessage( else -> CommonMediaGroupMessage(
messageId, messageId,
@ -242,7 +246,8 @@ data class RawMessage(
}, },
edit_date ?.asDate, edit_date ?.asDate,
forwarded, forwarded,
reply_to_message ?.asMessage reply_to_message ?.asMessage,
reply_markup
) )
} }
} ?: when (chat) { } ?: when (chat) {
@ -254,6 +259,7 @@ data class RawMessage(
edit_date ?.asDate, edit_date ?.asDate,
forwarded, forwarded,
reply_to_message ?.asMessage, reply_to_message ?.asMessage,
reply_markup,
author_signature author_signature
) )
else -> CommonMessageImpl( else -> CommonMessageImpl(
@ -265,6 +271,7 @@ data class RawMessage(
edit_date ?.asDate, edit_date ?.asDate,
forwarded, forwarded,
reply_to_message ?.asMessage, reply_to_message ?.asMessage,
reply_markup,
paymentInfo paymentInfo
) )
} }

View File

@ -0,0 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
interface AbleToBeMarkedUp {
val replyMarkup: InlineKeyboardMarkup?
}

View File

@ -6,4 +6,5 @@ interface CommonMessage<T: MessageContent> : Message,
AbleToBeForwardedMessage, AbleToBeForwardedMessage,
AbleToBeEditedMessage, AbleToBeEditedMessage,
AbleToReplyMessage, AbleToReplyMessage,
AbleToBeMarkedUp,
ContentMessage<T> ContentMessage<T>