diff --git a/library/build.gradle b/library/build.gradle index df95e5a2..4f1d20dc 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -16,6 +16,7 @@ dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.1.13' testImplementation 'org.reflections:reflections:0.9.12' + testImplementation 'org.jetbrains.kotlin:kotlin-reflect' } jar { diff --git a/library/src/main/java/com/pengrad/telegrambot/model/PreparedInlineMessage.kt b/library/src/main/java/com/pengrad/telegrambot/model/PreparedInlineMessage.kt new file mode 100644 index 00000000..7c68320a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/PreparedInlineMessage.kt @@ -0,0 +1,6 @@ +package com.pengrad.telegrambot.model + +data class PreparedInlineMessage( + @get:JvmName("id") val id: String, + @get:JvmName("expirationDate") val expirationDate: Int +) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java b/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java deleted file mode 100644 index 64e81c31..00000000 --- a/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.pengrad.telegrambot.model; - -import java.io.Serializable; - -/** - * Stas Parshin - * 24 May 2017 - */ -public class SuccessfulPayment implements Serializable { - private final static long serialVersionUID = 0L; - - private String currency; - private Integer total_amount; - private String invoice_payload; - private String shipping_option_id; - private OrderInfo order_info; - private String telegram_payment_charge_id; - private String provider_payment_charge_id; - - public String currency() { - return currency; - } - - public Integer totalAmount() { - return total_amount; - } - - public String invoicePayload() { - return invoice_payload; - } - - public String shippingOptionId() { - return shipping_option_id; - } - - public OrderInfo orderInfo() { - return order_info; - } - - public String telegramPaymentChargeId() { - return telegram_payment_charge_id; - } - - public String providerPaymentChargeId() { - return provider_payment_charge_id; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - SuccessfulPayment that = (SuccessfulPayment) o; - - if (currency != null ? !currency.equals(that.currency) : that.currency != null) return false; - if (total_amount != null ? !total_amount.equals(that.total_amount) : that.total_amount != null) return false; - if (invoice_payload != null ? !invoice_payload.equals(that.invoice_payload) : that.invoice_payload != null) - return false; - if (shipping_option_id != null ? !shipping_option_id.equals(that.shipping_option_id) : that.shipping_option_id != null) - return false; - if (order_info != null ? !order_info.equals(that.order_info) : that.order_info != null) return false; - if (telegram_payment_charge_id != null ? !telegram_payment_charge_id.equals(that.telegram_payment_charge_id) : that.telegram_payment_charge_id != null) - return false; - return provider_payment_charge_id != null ? provider_payment_charge_id.equals(that.provider_payment_charge_id) : that.provider_payment_charge_id == null; - - } - - @Override - public int hashCode() { - int result = currency != null ? currency.hashCode() : 0; - result = 31 * result + (total_amount != null ? total_amount.hashCode() : 0); - result = 31 * result + (invoice_payload != null ? invoice_payload.hashCode() : 0); - result = 31 * result + (shipping_option_id != null ? shipping_option_id.hashCode() : 0); - result = 31 * result + (order_info != null ? order_info.hashCode() : 0); - result = 31 * result + (telegram_payment_charge_id != null ? telegram_payment_charge_id.hashCode() : 0); - result = 31 * result + (provider_payment_charge_id != null ? provider_payment_charge_id.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "SuccessfulPayment{" + - "currency='" + currency + '\'' + - ", total_amount=" + total_amount + - ", invoice_payload='" + invoice_payload + '\'' + - ", shipping_option_id='" + shipping_option_id + '\'' + - ", order_info=" + order_info + - ", telegram_payment_charge_id='" + telegram_payment_charge_id + '\'' + - ", provider_payment_charge_id='" + provider_payment_charge_id + '\'' + - '}'; - } -} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.kt b/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.kt new file mode 100644 index 00000000..a888f036 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/SuccessfulPayment.kt @@ -0,0 +1,14 @@ +package com.pengrad.telegrambot.model + +data class SuccessfulPayment( + @get:JvmName("currency") val currency: String, + @get:JvmName("totalAmount") val totalAmount: Int, + @get:JvmName("invoicePayload") val invoicePayload: String, + @get:JvmName("subscriptionExpirationDate") val subscriptionExpirationDate: Int? = null, + @get:JvmName("isRecurring") val isRecurring: Boolean? = null, + @get:JvmName("isFirstRecurring") val isFirstRecurring: Boolean? = null, + @get:JvmName("shippingOptionId") val shippingOptionId: String? = null, + @get:JvmName("orderInfo") val orderInfo: OrderInfo? = null, + @get:JvmName("telegramPaymentChargeId") val telegramPaymentChargeId: String, + @get:JvmName("providerPaymentChargeId") val providerPaymentChargeId: String +) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/gift/Gift.kt b/library/src/main/java/com/pengrad/telegrambot/model/gift/Gift.kt new file mode 100644 index 00000000..eef53655 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/gift/Gift.kt @@ -0,0 +1,11 @@ +package com.pengrad.telegrambot.model.gift + +import com.pengrad.telegrambot.model.Sticker + +data class Gift( + @get:JvmName("id") val id: String, + @get:JvmName("sticker") val sticker: Sticker, + @get:JvmName("starCount") val starCount: Int, + @get:JvmName("totalCount") val totalCount: Int, + @get:JvmName("remainingCount") val remainingCount: Int +) diff --git a/library/src/main/java/com/pengrad/telegrambot/model/gift/Gifts.kt b/library/src/main/java/com/pengrad/telegrambot/model/gift/Gifts.kt new file mode 100644 index 00000000..640c4b0d --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/gift/Gifts.kt @@ -0,0 +1,18 @@ +package com.pengrad.telegrambot.model.gift + +data class Gifts( + @get:JvmName("gifts") val gifts: Array +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Gifts) return false + + if (!gifts.contentEquals(other.gifts)) return false + + return true + } + + override fun hashCode(): Int { + return gifts.contentHashCode() + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerUser.java b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerUser.java index 20cb6a9a..11e6097a 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerUser.java +++ b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerUser.java @@ -2,6 +2,7 @@ import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.chatbackground.BackgroundFillFreeformGradient; +import com.pengrad.telegrambot.model.gift.Gift; import com.pengrad.telegrambot.model.paidmedia.PaidMedia; import java.util.Arrays; @@ -15,6 +16,8 @@ public class TransactionPartnerUser extends TransactionPartner { private String invoice_payload; private PaidMedia[] paid_media; private String paid_media_payload; + private Integer subscription_period; + private Gift gift; public TransactionPartnerUser() { super(TYPE); @@ -36,6 +39,14 @@ public String paidMediaPayload() { return paid_media_payload; } + public Integer subscriptionPeriod() { + return subscription_period; + } + + public Gift gift() { + return gift; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -45,12 +56,22 @@ public boolean equals(Object o) { return Objects.equals(user, that.user) && Objects.equals(invoice_payload, that.invoice_payload) && Objects.deepEquals(paid_media, that.paid_media) - && Objects.equals(paid_media_payload, that.paid_media_payload); + && Objects.equals(paid_media_payload, that.paid_media_payload) + && Objects.equals(subscription_period, that.subscription_period) + && Objects.equals(gift, that.gift); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), user, invoice_payload, Arrays.hashCode(paid_media)); + return Objects.hash( + super.hashCode(), + user, + invoice_payload, + Arrays.hashCode(paid_media), + paid_media_payload, + subscription_period, + gift + ); } @Override @@ -60,6 +81,8 @@ public String toString() { ", invoice_payload='" + invoice_payload + '\'' + ", paid_media=" + Arrays.toString(paid_media) + ", paid_media_payload='" + paid_media_payload + '\'' + + ", subscription_period='" + subscription_period + '\'' + + ", gift=" + gift + '}'; } } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.java b/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.java deleted file mode 100644 index 5661a63d..00000000 --- a/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.pengrad.telegrambot.request; - -import com.pengrad.telegrambot.model.request.LabeledPrice; -import com.pengrad.telegrambot.response.StringResponse; - -/** - * Mirco Ianese - * 21 Jun 2022 - */ -public class CreateInvoiceLink extends BaseRequest { - - public CreateInvoiceLink(String title, String description, String payload, String currency, LabeledPrice... prices) { - super(StringResponse.class); - add("title", title).add("description", description).add("payload", payload) - .add("currency", currency).add("prices", prices); - } - - - /** - * Backward compatibility: API 7.4, parameter "provider_token" became optional - * @deprecated Use constrcutor without 'provider_token' instead - */ - @Deprecated - public CreateInvoiceLink(String title, String description, String payload, String providerToken, - String currency, LabeledPrice... prices) { - super(StringResponse.class); - add("title", title).add("description", description).add("payload", payload) - .add("provider_token", providerToken).add("currency", currency).add("prices", prices); - } - - public CreateInvoiceLink providerToken(String providerToken) { - return add("provider_token", providerToken); - } - - /** - * @param maxTipAmount The maximum accepted amount for tips in the smallest units of the currency - */ - public CreateInvoiceLink maxTipAmount(int maxTipAmount) { - return add("max_tip_amount", maxTipAmount); - } - - /** - * @param suggestedTipAmounts An array of suggested amounts of tip in the smallest units of the currency. At most 4 suggested tip amounts can be specified. The suggested tip amounts must be positive, passed in a strictly increased order and must not exceed max_tip_amount. - */ - public CreateInvoiceLink suggestedTipAmounts(Integer[] suggestedTipAmounts) { - return add("suggested_tip_amounts", suggestedTipAmounts); - } - - public CreateInvoiceLink providerData(String providerData) { - return add("provider_data", providerData); - } - - public CreateInvoiceLink photoUrl(String photoUrl) { - return add("photo_url", photoUrl); - } - - public CreateInvoiceLink photoSize(Integer photoSize) { - return add("photo_size", photoSize); - } - - public CreateInvoiceLink photoWidth(Integer photoWidth) { - return add("photo_width", photoWidth); - } - - public CreateInvoiceLink photoHeight(Integer photoHeight) { - return add("photo_height", photoHeight); - } - - public CreateInvoiceLink needName(boolean needName) { - return add("need_name", needName); - } - - public CreateInvoiceLink needPhoneNumber(boolean needPhoneNumber) { - return add("need_phone_number", needPhoneNumber); - } - - public CreateInvoiceLink needEmail(boolean needEmail) { - return add("need_email", needEmail); - } - - public CreateInvoiceLink needShippingAddress(boolean needShippingAddress) { - return add("need_shipping_address", needShippingAddress); - } - - public CreateInvoiceLink sendEmailToProvider(boolean sendEmailToProvider) { - return add("send_email_to_provider", sendEmailToProvider); - } - - public CreateInvoiceLink sendPhoneNumberToProvider(boolean sendPhoneNumberToProvider) { - return add("send_phone_number_to_provider", sendPhoneNumberToProvider); - } - - public CreateInvoiceLink isFlexible(boolean isFlexible) { - return add("is_flexible", isFlexible); - } - -} diff --git a/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.kt b/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.kt new file mode 100644 index 00000000..4e96dd1c --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.kt @@ -0,0 +1,90 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.model.request.LabeledPrice +import com.pengrad.telegrambot.response.StringResponse +import com.pengrad.telegrambot.utility.kotlin.optionalRequestParameter +import com.pengrad.telegrambot.utility.kotlin.requestParameter + +class CreateInvoiceLink( + title: String, + description: String, + payload: String, + currency: String, + vararg prices: LabeledPrice +) : KBaseRequest(StringResponse::class) { + + /** + * Backward compatibility: API 7.4, parameter "provider_token" became optional + * @deprecated Use constructor without 'provider_token' instead + */ + @Deprecated("Use constructor without 'providerToken' parameter", ReplaceWith("CreateInvoiceLink(title, description, payload, currency, *prices)")) + constructor( + title: String, + description: String, + payload: String, + providerToken: String, + currency: String, + vararg prices: LabeledPrice + ) : this(title, description, payload, currency, *prices) { + this.providerToken = providerToken + } + + val title: String by requestParameter(title) + val description: String by requestParameter(description) + val payload: String by requestParameter(payload) + val currency: String by requestParameter(currency) + val prices: Array by requestParameter(arrayOf(*prices)) + + var providerToken: String? by optionalRequestParameter() + var subscriptionPeriod: Int? by optionalRequestParameter() + var businessConnectionId: String? by optionalRequestParameter() + var maxTipAmount: Int? by optionalRequestParameter() + var suggestedTipAmounts: Array? by optionalRequestParameter() + var providerData: String? by optionalRequestParameter() + var photoUrl: String? by optionalRequestParameter() + var photoSize: Int? by optionalRequestParameter() + var photoWidth: Int? by optionalRequestParameter() + var photoHeight: Int? by optionalRequestParameter() + var needName: Boolean? by optionalRequestParameter() + var needPhoneNumber: Boolean? by optionalRequestParameter() + var needEmail: Boolean? by optionalRequestParameter() + var needShippingAddress: Boolean? by optionalRequestParameter() + var sendEmailToProvider: Boolean? by optionalRequestParameter() + var sendPhoneNumberToProvider: Boolean? by optionalRequestParameter() + var isFlexible: Boolean? by optionalRequestParameter() + + fun providerToken(providerToken: String) = apply { this.providerToken = providerToken } + + fun subscriptionPeriod(subscriptionPeriod: Int) = apply { this.subscriptionPeriod = subscriptionPeriod } + + fun businessConnectionId(businessConnectionId: String) = apply { this.businessConnectionId = businessConnectionId } + + fun maxTipAmount(maxTipAmount: Int) = apply { this.maxTipAmount = maxTipAmount } + + fun suggestedTipAmounts(suggestedTipAmounts: Array) = apply { this.suggestedTipAmounts = suggestedTipAmounts } + + fun providerData(providerData: String) = apply { this.providerData = providerData } + + fun photoUrl(photoUrl: String) = apply { this.photoUrl = photoUrl } + + fun photoSize(photoSize: Int) = apply { this.photoSize = photoSize } + + fun photoWidth(photoWidth: Int) = apply { this.photoWidth = photoWidth } + + fun photoHeight(photoHeight: Int) = apply { this.photoHeight = photoHeight } + + fun needName(needName: Boolean) = apply { this.needName = needName } + + fun needPhoneNumber(needPhoneNumber: Boolean) = apply { this.needPhoneNumber = needPhoneNumber } + + fun needEmail(needEmail: Boolean) = apply { this.needEmail = needEmail } + + fun needShippingAddress(needShippingAddress: Boolean) = apply { this.needShippingAddress = needShippingAddress } + + fun sendEmailToProvider(sendEmailToProvider: Boolean) = apply { this.sendEmailToProvider = sendEmailToProvider } + + fun sendPhoneNumberToProvider(sendPhoneNumberToProvider: Boolean) = apply { this.sendPhoneNumberToProvider = sendPhoneNumberToProvider } + + fun isFlexible(isFlexible: Boolean) = apply { this.isFlexible = isFlexible } + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/EditUserStarSubscription.kt b/library/src/main/java/com/pengrad/telegrambot/request/EditUserStarSubscription.kt new file mode 100644 index 00000000..23061060 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/EditUserStarSubscription.kt @@ -0,0 +1,16 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.response.BaseResponse +import com.pengrad.telegrambot.utility.kotlin.requestParameter + +class EditUserStarSubscription( + userId: Long, + telegramPaymentChargeId: String, + isCanceled: Boolean +) : KBaseRequest(BaseResponse::class) { + + val userId: Long by requestParameter(userId) + val telegramPaymentChargeId: String by requestParameter(telegramPaymentChargeId) + val isCanceled: Boolean by requestParameter(isCanceled) + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/GetAvailableGifts.kt b/library/src/main/java/com/pengrad/telegrambot/request/GetAvailableGifts.kt new file mode 100644 index 00000000..dc2868cd --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/GetAvailableGifts.kt @@ -0,0 +1,7 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.response.GetAvailableGiftsResponse + +class GetAvailableGifts : KBaseRequest( + GetAvailableGiftsResponse::class +) \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/KBaseRequest.kt b/library/src/main/java/com/pengrad/telegrambot/request/KBaseRequest.kt new file mode 100644 index 00000000..c732d542 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/KBaseRequest.kt @@ -0,0 +1,14 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.response.BaseResponse +import kotlin.reflect.KClass + +abstract class KBaseRequest, R : BaseResponse>( + clazz: KClass +) : BaseRequest(clazz.java) { + + internal fun addParameter(name: String, value: Any?): T { + return add(name, value) + } + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SavePreparedInlineMessage.kt b/library/src/main/java/com/pengrad/telegrambot/request/SavePreparedInlineMessage.kt new file mode 100644 index 00000000..51561b39 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SavePreparedInlineMessage.kt @@ -0,0 +1,32 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.model.request.InlineQueryResult +import com.pengrad.telegrambot.response.BaseResponse +import com.pengrad.telegrambot.response.SavePreparedInlineMessageResponse +import com.pengrad.telegrambot.utility.kotlin.optionalRequestParameter +import com.pengrad.telegrambot.utility.kotlin.requestParameter + +class SavePreparedInlineMessage( + userId: Long, + result: InlineQueryResult<*> +) : KBaseRequest( + SavePreparedInlineMessageResponse::class +) { + + val userId: Long by requestParameter(userId) + val result: InlineQueryResult<*> by requestParameter(result) + + var allowUserChats: Boolean? by optionalRequestParameter() + var allowBotChats: Boolean? by optionalRequestParameter() + var allowGroupChats: Boolean? by optionalRequestParameter() + var allowChannelChats: Boolean? by optionalRequestParameter() + + fun allowUserChats(allowUserChats: Boolean) = apply { this.allowUserChats = allowUserChats } + + fun allowBotChats(allowBotChats: Boolean) = apply { this.allowBotChats = allowBotChats } + + fun allowGroupChats(allowGroupChats: Boolean) = apply { this.allowGroupChats = allowGroupChats } + + fun allowChannelChats(allowChannelChats: Boolean) = apply { this.allowChannelChats = allowChannelChats } + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendGift.kt b/library/src/main/java/com/pengrad/telegrambot/request/SendGift.kt new file mode 100644 index 00000000..0abb5f2f --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendGift.kt @@ -0,0 +1,27 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.model.MessageEntity +import com.pengrad.telegrambot.model.request.ParseMode +import com.pengrad.telegrambot.response.BaseResponse +import com.pengrad.telegrambot.utility.kotlin.optionalRequestParameter +import com.pengrad.telegrambot.utility.kotlin.requestParameter + +class SendGift( + userId: Long, + giftId: String, +) : KBaseRequest(BaseResponse::class) { + + val userId: Long by requestParameter(userId) + val giftId: String by requestParameter(giftId) + + var text: String? by optionalRequestParameter() + var textParseMode: ParseMode? by optionalRequestParameter(valueMapper = { it?.name }) + var textEntities: Array? by optionalRequestParameter() + + fun text(text: String) = apply { this.text = text } + + fun textParseMode(parseMode: ParseMode) = apply { this.textParseMode = textParseMode } + + fun textEntities(textEntities: Array) = apply { this.textEntities = textEntities } + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SetUserEmojiStatus.kt b/library/src/main/java/com/pengrad/telegrambot/request/SetUserEmojiStatus.kt new file mode 100644 index 00000000..9a790e59 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/request/SetUserEmojiStatus.kt @@ -0,0 +1,16 @@ +package com.pengrad.telegrambot.request + +import com.pengrad.telegrambot.response.BaseResponse +import com.pengrad.telegrambot.utility.kotlin.requestParameter + +class SetUserEmojiStatus( + userId: Long, + emojiStatusCustomEmojiId: String, + emojiStatusExpirationDate: Int +) : KBaseRequest(BaseResponse::class) { + + val userId: Long by requestParameter(userId) + val emojiStatusCustomEmojiId: String by requestParameter(emojiStatusCustomEmojiId) + val emojiStatusExpirationDate: Int by requestParameter(emojiStatusExpirationDate) + +} \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/response/GetAvailableGiftsResponse.kt b/library/src/main/java/com/pengrad/telegrambot/response/GetAvailableGiftsResponse.kt new file mode 100644 index 00000000..264273ee --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/response/GetAvailableGiftsResponse.kt @@ -0,0 +1,5 @@ +package com.pengrad.telegrambot.response + +import com.pengrad.telegrambot.model.gift.Gifts + +data class GetAvailableGiftsResponse(val result: Gifts) : BaseResponse() diff --git a/library/src/main/java/com/pengrad/telegrambot/response/SavePreparedInlineMessageResponse.kt b/library/src/main/java/com/pengrad/telegrambot/response/SavePreparedInlineMessageResponse.kt new file mode 100644 index 00000000..1d50f83b --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/response/SavePreparedInlineMessageResponse.kt @@ -0,0 +1,7 @@ +package com.pengrad.telegrambot.response + +import com.pengrad.telegrambot.model.PreparedInlineMessage + +data class SavePreparedInlineMessageResponse( + val result: PreparedInlineMessage +) : BaseResponse() \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/kotlin/RequestParameterDelegate.kt b/library/src/main/java/com/pengrad/telegrambot/utility/kotlin/RequestParameterDelegate.kt new file mode 100644 index 00000000..e1da6fd1 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/utility/kotlin/RequestParameterDelegate.kt @@ -0,0 +1,69 @@ +package com.pengrad.telegrambot.utility.kotlin + +import com.pengrad.telegrambot.request.KBaseRequest +import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +typealias RequestParameterValueMapper = (I) -> O + +fun , V : Any> requestParameter( + initialValue: V, + customParameterName: String? = null +) : RequestParameterDelegate = RequestParameterDelegate( + value = initialValue, + customParameterName = customParameterName +) + +fun , V : Any?> optionalRequestParameter( + initialValue: V? = null, + customParameterName: String? = null, + valueMapper: RequestParameterValueMapper? = null +) : RequestParameterDelegate = RequestParameterDelegate( + value = initialValue, + customParameterName = customParameterName, + valueMapper = valueMapper +) + +class RequestParameterDelegate, V>( + private var value: V, + private val customParameterName: String? = null, + private val valueMapper: RequestParameterValueMapper? = null +) : ReadWriteProperty, PropertyDelegateProvider> { + + override operator fun provideDelegate(thisRef: T, property: KProperty<*>): RequestParameterDelegate { + if (value != null) { + updateRequestValue(thisRef, property, value) + } + return this + } + + override operator fun getValue(thisRef: T, property: KProperty<*>): V { + return value + } + + override operator fun setValue(thisRef: T, property: KProperty<*>, value: V) { + updateRequestValue(thisRef, property, value) + this.value = value + } + + private fun updateRequestValue(thisRef: T, property: KProperty<*>, value: V) { + thisRef.addParameter( + name = customParameterName ?: convertToSnakeCase(property.name), + value = valueMapper?.invoke(value) ?: value + ) + } + +} + +fun convertToSnakeCase(input: String): String { + return input.replace(Regex("([a-z])([A-Z])")) { + "${it.groupValues[1]}_${it.groupValues[2]}" + }.lowercase() +} + +fun main() { + println(convertToSnakeCase("userTEST")) // user_id + println(convertToSnakeCase("text")) // text + println(convertToSnakeCase("isCancelled")) // is_cancelled +} \ No newline at end of file diff --git a/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java b/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java index e097e13d..a7b85314 100644 --- a/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/PaymentsTest.java @@ -169,7 +169,7 @@ public void testSuccessfulPayment() { assertNotNull(payment); assertEquals("USD", payment.currency()); - assertEquals(Integer.valueOf(200), payment.totalAmount()); + assertEquals(200, payment.totalAmount()); assertEquals("my_payload", payment.invoicePayload()); assertEquals("2", payment.shippingOptionId()); assertEquals("tcid", payment.telegramPaymentChargeId()); diff --git a/library/src/test/java/com/pengrad/telegrambot/ResponseTest.java b/library/src/test/java/com/pengrad/telegrambot/ResponseTest.java index 9813a84a..53069de1 100644 --- a/library/src/test/java/com/pengrad/telegrambot/ResponseTest.java +++ b/library/src/test/java/com/pengrad/telegrambot/ResponseTest.java @@ -1,6 +1,8 @@ package com.pengrad.telegrambot; import com.pengrad.telegrambot.response.BaseResponse; +import kotlin.jvm.JvmClassMappingKt; +import kotlin.reflect.KClass; import org.junit.Before; import org.junit.Test; import org.reflections.Reflections; @@ -33,6 +35,12 @@ public void setClasses() { @Test public void testToString() throws IllegalAccessException, InstantiationException, InvocationTargetException { for (Class c : classes) { + KClass kclass = JvmClassMappingKt.getKotlinClass(c); + + if (kclass.isData()) { + continue; + } + Optional> constructor = Arrays.stream(c.getDeclaredConstructors()).filter(dc -> dc.getParameterCount() == 0).findFirst(); assertTrue("No default constructor in " + c.getSimpleName(), constructor.isPresent()); constructor.get().setAccessible(true);