elbekD / kt-telegram-bot

Telegram Bot Library for Kotlin language
MIT License
103 stars 19 forks source link

Parsing InlineKeyboardMarkup object by kotlinx.serialization.json.Json causes SerializationException #56

Closed foxtrotdev closed 1 year ago

foxtrotdev commented 1 year ago

I noticed that parsing InlineKeyboardMarkup object by kotlinx.serialization.json.Json causes SerializationException.

Crash log:

Unknown exception: kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.

Cause of problem:

private val markupToString = { a: Any -> toJson(a) } // com.elbekd.bot.internal.TelegramClient

Sample:

private val markupToString = { a: Any -> kotlinx.serialization.json.Json.encodeToString(a) }

val inlineKeyboardMarkup = InlineKeyboardMarkup(listOf(listOf(
    InlineKeyboardButton("Google", "https://www.google.com)
)))

val encodeToString = markupToString { inlineKeyboardMarkup } // crash!

It could be collect while sending photo:

telegramBot.sendPhoto(
    ChatId.IntegerId(TELEGRAM_CHANNEL),
    SendingFile(destinationFile),
    "Hello world",
    replyMarkup = inlineKeyboardMarkup
)

Library used:

implementation 'com.github.elbekD:kt-telegram-bot:2.1.7'
foxtrotdev commented 1 year ago

This could fix the problem:

inline fun <reified T> markupToStringV2(a: T) : String = Json.encodeToString(a)
elbekD commented 1 year ago

thx. fixed in 2.1.8