1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-11-29 02:45:44 +00:00

Compare commits

..

11 Commits

9 changed files with 112 additions and 35 deletions

View File

@@ -38,6 +38,16 @@
and size of retrieved updates is equal to 100 (max count of retrieved updates) and size of retrieved updates is equal to 100 (max count of retrieved updates)
* Extensions `getUpdates` now will receive only not nullable `limit` parameter * Extensions `getUpdates` now will receive only not nullable `limit` parameter
### 0.26.4
* `TelegramBotAPI`:
* Now any getting of updates will return `UnknownUpdateType` when inside of deserialization will be
`SerializationException` or `NotImplemented` error
* `CallbackGame` currently is an object
* It is possible to use `CallbackGame` for now
* `CallbackGameInlineKeyboardButton` now will not accept `callbackGame` as income object
* Now it is possible to pass exception handler in webhook
### 0.26.3 ### 0.26.3
* `TelegramBotAPI`: * `TelegramBotAPI`:

View File

@@ -19,6 +19,47 @@ work with commands, updates and other different things
Most part of some specific solves or unuseful Most part of some specific solves or unuseful
moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api). moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api).
## JavaScript notes
In case if you are want to use this library inside of browser, you will need additional settings (thanks for help to [Alexander Nozik](https://research.jetbrains.org/researchers/altavir)):
<details>
<summary>Gradle build script help</summary>
```groovy
dependencies {
/* ... */
implementation "com.github.insanusmokrassar:TelegramBotAPI:$tgbot_api_version"
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$tgbot_api_version" // optional
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$tgbot_api_version" // optional
/* Block of dependencies for correct building in browser */
implementation(npm("fs"))
implementation(npm("bufferutil"))
implementation(npm("utf-8-validate"))
implementation(npm("abort-controller"))
implementation(npm("text-encoding"))
}
/* ... */
kotlin {
target {
browser {
/* Block for fix of exception in absence of some functionality, https://github.com/ktorio/ktor/issues/1339 */
dceTask {
dceOptions {
keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
}
}
}
}
}
```
</details>
## Ok, where should I start? ## Ok, where should I start?
In most cases, the most simple way will be to implement In most cases, the most simple way will be to implement

View File

@@ -3,12 +3,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutSerialization
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.ListSerializer
private val updatesListSerializer = ListSerializer( private val updatesListSerializer = ListSerializer(
UpdateSerializerWithoutDeserialization UpdateSerializerWithoutSerialization
) )
@Serializable @Serializable

View File

@@ -36,10 +36,11 @@ data class CallbackDataInlineKeyboardButton(
@Serializable @Serializable
data class CallbackGameInlineKeyboardButton( data class CallbackGameInlineKeyboardButton(
@SerialName(textField) @SerialName(textField)
override val text: String, override val text: String
) : InlineKeyboardButton() {
@SerialName(callbackGameField) @SerialName(callbackGameField)
val callbackGame: CallbackGame private val callbackGame = CallbackGame
) : InlineKeyboardButton() }
@Serializable @Serializable
data class LoginURLInlineKeyboardButton( data class LoginURLInlineKeyboardButton(

View File

@@ -3,8 +3,4 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.games
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
class CallbackGame { object CallbackGame
init {
TODO()
}
}

View File

@@ -64,12 +64,16 @@ internal data class RawUpdate constructor(
raw raw
) )
} }
} catch (e: SerializationException) { } catch (e: Error) {
UnknownUpdateType( when (e) {
updateId, is SerializationException,
raw.toString(), is NotImplementedError -> UnknownUpdateType(
raw updateId,
) raw.toString(),
raw
)
else -> throw e
}
}.also { }.also {
initedUpdate = it initedUpdate = it
} }

View File

@@ -18,7 +18,7 @@ data class UnknownUpdateType(
val rawJson: JsonElement val rawJson: JsonElement
) : Update ) : Update
internal object UpdateSerializerWithoutDeserialization : KSerializer<Update> { internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder) override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder)

View File

@@ -4,15 +4,11 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.utils.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
import io.ktor.application.call import io.ktor.application.call
import io.ktor.request.receiveText import io.ktor.request.receiveText
import io.ktor.response.respond import io.ktor.response.respond
@@ -45,6 +41,7 @@ suspend fun RequestsExecutor.setWebhook(
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
): Job { ): Job {
val executeDeferred = certificate ?.let { val executeDeferred = certificate ?.let {
@@ -74,12 +71,18 @@ suspend fun RequestsExecutor.setWebhook(
module { module {
routing { routing {
post(listenRoute) { post(listenRoute) {
val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) handleSafely(
val update = nonstrictJsonFormat.fromJson( {
RawUpdate.serializer(), exceptionsHandler ?.invoke(it)
asJson }
) ) {
updatesChannel.send(update.asUpdate(asJson)) val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
val update = nonstrictJsonFormat.fromJson(
UpdateDeserializationStrategy,
asJson
)
updatesChannel.send(update)
}
call.respond("Ok") call.respond("Ok")
} }
} }
@@ -113,11 +116,6 @@ suspend fun RequestsExecutor.setWebhook(
launch { launch {
for (update in updatesChannel) { for (update in updatesChannel) {
val data = update.data val data = update.data
if (data is MediaGroupUpdate) {
} else {
}
when (data) { when (data) {
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
else -> block(update) else -> block(update)
@@ -139,6 +137,33 @@ suspend fun RequestsExecutor.setWebhook(
} }
} }
suspend fun RequestsExecutor.setWebhook(
url: String,
port: Int,
engineFactory: ApplicationEngineFactory<*, *>,
listenHost: String = "0.0.0.0",
listenRoute: String = "/",
certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null,
block: UpdateReceiver<Update>
) = setWebhook(
url,
port,
engineFactory,
listenHost,
listenRoute,
certificate,
privateKeyConfig,
scope,
allowedUpdates,
maxAllowedConnections,
null,
block
)
suspend fun RequestsExecutor.setWebhook( suspend fun RequestsExecutor.setWebhook(
url: String, url: String,
port: Int, port: Int,

View File

@@ -7,6 +7,6 @@ uuid_version=0.1.0
ktor_version=1.3.2 ktor_version=1.3.2
library_group=com.github.insanusmokrassar library_group=com.github.insanusmokrassar
library_version=0.26.3 library_version=0.26.4
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4