mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-11-18 13:00:18 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 88eebdc448 | |||
| 8c76283db5 | |||
| 7668c48081 | |||
| 35d2135f73 | |||
| 1cb0e096be | |||
| 31f7c7f31b | |||
| 82d3b3bc48 | |||
| b3734a5c2a | |||
| 55b8736d50 | |||
| 3334fd3ca6 | |||
|
|
e2416b405a | ||
| 14f012fbfa | |||
| 1ff55057f2 | |||
| 71b5e33dbc | |||
| 08d9d183f4 | |||
| 7183634fd6 | |||
| cf9f270651 | |||
| bd87938e9c | |||
| ba76eaeb90 | |||
| d8492ae168 | |||
| 0db85232d3 | |||
| bcf2325be8 | |||
| 51174a13de | |||
| dfc1fa4d7e | |||
| 10a1d1cb38 | |||
| 6c39dc4d06 | |||
| 4877b8958e | |||
| db9c460e66 |
46
CHANGELOG.md
46
CHANGELOG.md
@@ -38,6 +38,52 @@
|
||||
and size of retrieved updates is equal to 100 (max count of retrieved updates)
|
||||
* Extensions `getUpdates` now will receive only not nullable `limit` parameter
|
||||
|
||||
### 0.26.3
|
||||
|
||||
* `TelegramBotAPI`:
|
||||
* `CallbackGameInlineKeyboardButton` was added
|
||||
([Issue-79](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/79),
|
||||
[PR-80](https://github.com/InsanusMokrassar/TelegramBotAPI/pull/80))
|
||||
* `UnknownInlineKeyboardButton` was added. It is unavailable for creating, but you can receive it, for example, in
|
||||
`InlineQueryResult`
|
||||
* `Update` now will be created even if was `SerializationException` inside of creating the update instance - in this
|
||||
case will be created `UnknownUpdateType`
|
||||
* `UnknownUpdateType$rawJson` value now is included (`JsonElement`)
|
||||
* **EXPERIMENTALLY** `BaseEditMessageUpdate#data` now is `CommonMessage<*>`
|
||||
* Suspend inline function `handleSafely` was added
|
||||
* `KtorRequestsExecutor` now use `handleSafely` instead of `try` with `supervisorScope`
|
||||
* `UpdatesPolling` now use `handleSafely` instead of `try` with `supervisorScope`
|
||||
|
||||
### 0.26.2
|
||||
|
||||
* `TelegramBotAPI`:
|
||||
* Now `EditMediaGroupUpdate` also extends `BaseEditMessageUpdate`
|
||||
* **EXPERIMENTALLY** Now all `TextSource` realisations will contain `source` field as a property inside of them
|
||||
* `TelegramBotAPI-extensions-api`:
|
||||
* `startGettingFlowsUpdates` extension which do not require filter (but return a new one) was added
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* Subproject was added
|
||||
* `filterBaseMessageUpdates`, `filterSentMediaGroupUpdates` and `filterEditMediaGroupUpdates` extensions was added
|
||||
* `filterCommandsWithArgs`, `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added
|
||||
* `asContentMessagesFlow`, `asChatEventsFlow` and `asUnknownMessagesFlow` extensions was added
|
||||
* `withContentType` extension was added
|
||||
* `onlyAnimationContentMessages` extension was added
|
||||
* `onlyAudioContentMessages` extension was added
|
||||
* `onlyContactContentMessages` extension was added
|
||||
* `onlyDiceContentMessages` extension was added
|
||||
* `onlyDocumentContentMessages` extension was added
|
||||
* `onlyGameContentMessages` extension was added
|
||||
* `onlyInvoiceContentMessages` extension was added
|
||||
* `onlyLocationContentMessages` extension was added
|
||||
* `onlyPhotoContentMessages` extension was added
|
||||
* `onlyPollContentMessages` extension was added
|
||||
* `onlyStickerContentMessages` extension was added
|
||||
* `onlyTextContentMessages` extension was added
|
||||
* `onlyVenueContentMessages` extension was added
|
||||
* `onlyVideoContentMessages` extension was added
|
||||
* `onlyVideoNoteContentMessages` extension was added
|
||||
* `onlyVoiceContentMessages` extension was added
|
||||
|
||||
### 0.26.1
|
||||
|
||||
* `TelegramBotAPI`:
|
||||
|
||||
26
README.md
26
README.md
@@ -1,27 +1,35 @@
|
||||
# TelegramBotAPI
|
||||
|
||||
| Common info | [](https://github.com/KotlinBy/awesome-kotlin) [](https://t.me/InMoTelegramBotAPI) [](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) |
|
||||
| -----------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| -------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| TelegramBotAPI status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) |
|
||||
| TelegramBotAPI Extensions status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) |
|
||||
|
||||
| TelegramBotAPI Util Extensions status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) |
|
||||
|
||||
It is a complex of libraries for working with `TelegramBotAPI` in type-safe and strict way as much as it possible. In
|
||||
the list of this complex currently next projects:
|
||||
|
||||
* [TelegramBotAPI](TelegramBotAPI/README.md) - core of library. In fact it is independent library and can be used alone
|
||||
without any additional library
|
||||
without any additional library
|
||||
* [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) - contains extensions (mostly for
|
||||
`RequestsExecutor`), which allows to use the core library in more pleasant way
|
||||
`RequestsExecutor`), which allows to use the core library in more pleasant way
|
||||
* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions for more comfortable
|
||||
work with commands, updates and other different things
|
||||
|
||||
Most part of some specific solves or unuseful
|
||||
moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api).
|
||||
|
||||
## Ok, where should I start?
|
||||
|
||||
Firstly, look at the [TelegramBotAPI](TelegramBotAPI/README.md). Here you can find all information about currently
|
||||
covered Telegram Bot API and other things. After this you can look at the
|
||||
[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md).
|
||||
In most cases, the most simple way will be to implement
|
||||
[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) and
|
||||
[TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) for the reason that they contains more
|
||||
simple tools. If you want to dive deeper in the core of library or develop something for it - welcome to
|
||||
[TelegramBotAPI](TelegramBotAPI/README.md).
|
||||
|
||||
Anyway, all libraries are very typical inside of them. For example, any request in TelegramBotAPI look like
|
||||
`requestsExecutor.execute(SomeRequest())`.
|
||||
Anyway, all libraries are very typical inside of them. Examples:
|
||||
|
||||
* In `TelegramBotAPI` common request look like `requestsExecutor.execute(SomeRequest())`
|
||||
* `TelegramBotAPI-extensions-api` typical syntax look like `requestsExecutor.someRequest()` (in most cases it would be
|
||||
better to use `bot` name instead of `requestsExecutor`)
|
||||
* `TelegramBotAPI-extensions-utils` will look like `filter.filterBaseMessageUpdates(chatId).filterExactCommands(Regex("^.*$"))...`
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
# TelegramBotAPI extensions
|
||||
|
||||
[](https://github.com/KotlinBy/awesome-kotlin)
|
||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion)
|
||||
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api)
|
||||
[](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI-extensions-api_master__publishing/)
|
||||
|
||||
## What is it?
|
||||
|
||||
@@ -99,3 +97,21 @@ filter.messageFlow.mapNotNull {
|
||||
CoroutineScope(Dispatchers.Default)
|
||||
)
|
||||
```
|
||||
|
||||
### Alternative way
|
||||
|
||||
There is an alternative way to get updates. In fact it is almost the same, but could be more useful for some cases:
|
||||
|
||||
```kotlin
|
||||
val filter = bot.startGettingOfUpdates(
|
||||
scope = CoroutineScope(Dispatchers.Default)
|
||||
) { // Here as reveiver will be FlowsUpdatesFilter
|
||||
messageFlow.mapNotNull {
|
||||
it.data as? ContentMessage<*>
|
||||
}.onEach {
|
||||
println(it)
|
||||
}.launchIn(
|
||||
CoroutineScope(Dispatchers.Default)
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
||||
import io.ktor.client.features.HttpRequestTimeoutException
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
@@ -23,8 +25,14 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
var lastUpdateIdentifier: UpdateIdentifier? = null
|
||||
|
||||
while (isActive) {
|
||||
try {
|
||||
supervisorScope {
|
||||
handleSafely(
|
||||
{ e ->
|
||||
exceptionsHandler ?.invoke(e)
|
||||
if (e is RequestException) {
|
||||
delay(1000L)
|
||||
}
|
||||
}
|
||||
) {
|
||||
val updates = getUpdates(
|
||||
offset = lastUpdateIdentifier?.plus(1),
|
||||
timeout = timeoutSeconds,
|
||||
@@ -44,7 +52,7 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
}
|
||||
}
|
||||
|
||||
supervisorScope {
|
||||
handleSafely {
|
||||
for (update in updates) {
|
||||
updatesReceiver(update)
|
||||
|
||||
@@ -52,17 +60,27 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: HttpRequestTimeoutException) {
|
||||
exceptionsHandler ?.invoke(e) // it is ok due to mechanism of long polling
|
||||
} catch (e: RequestException) {
|
||||
exceptionsHandler ?.invoke(e) // it is not ok, but in most cases it will mean that there is some limit for requests count
|
||||
delay(1000L)
|
||||
} catch (e: Exception) {
|
||||
exceptionsHandler ?.invoke(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will create a new one [FlowsUpdatesFilter]. This method could be unsafe due to the fact that it will start
|
||||
* getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call
|
||||
* [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows. To avoid it, you can pass
|
||||
* [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting
|
||||
*/
|
||||
@PreviewFeature
|
||||
fun RequestsExecutor.startGettingFlowsUpdates(
|
||||
timeoutSeconds: Seconds = 30,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
|
||||
flowsUpdatesFilterUpdatesKeeperCount: Int = 64,
|
||||
flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {}
|
||||
): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply {
|
||||
flowUpdatesPreset()
|
||||
startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver)
|
||||
}
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
updatesFilter: UpdatesFilter,
|
||||
timeoutSeconds: Seconds = 30,
|
||||
|
||||
81
TelegramBotAPI-extensions-utils/README.md
Normal file
81
TelegramBotAPI-extensions-utils/README.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# TelegramBotAPI Util Extensions
|
||||
|
||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion)
|
||||
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils)
|
||||
|
||||
## What is it?
|
||||
|
||||
It is wrapper library for [TelegramBotAPI](../TelegramBotAPI/README.md). Currently, this library contains some usefull filters for commands, updates types and different others.
|
||||
|
||||
## How to implement library?
|
||||
|
||||
Common ways to implement this library are presented here. In some cases it will require additional steps
|
||||
like inserting of additional libraries (like `kotlin stdlib`). In the examples will be used variable
|
||||
`telegrambotapi-extensions-utils_version`, which must be set up by developer. Available versions are presented on
|
||||
[bintray](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils), next version is last published:
|
||||
|
||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion)
|
||||
|
||||
### Maven
|
||||
|
||||
Dependency config presented here:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.github.insanusmokrassar</groupId>
|
||||
<artifactId>TelegramBotAPI-extensions-utils</artifactId>
|
||||
<version>${telegrambotapi-extensions-utils_version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### Gradle
|
||||
|
||||
To use last versions you will need to add one line in repositories block of your `build.gradle`:
|
||||
|
||||
`jcenter()` or `mavenCentral()`
|
||||
|
||||
And add next line to your dependencies block:
|
||||
|
||||
```groovy
|
||||
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version"
|
||||
```
|
||||
|
||||
or for old gradle:
|
||||
|
||||
```groovy
|
||||
compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version"
|
||||
```
|
||||
|
||||
## How to use?
|
||||
|
||||
Here will be presented several examples of usage. In all cases it is expected that you have created your bot and filter:
|
||||
|
||||
```kotlin
|
||||
val bot: RequestsExecutor = KtorRequestsExecutor(
|
||||
TelegramAPIUrlsKeeper(BOT_TOKEN)
|
||||
)
|
||||
val filter = FlowsUpdatesFilter(64)
|
||||
```
|
||||
|
||||
Alternative way to use the things below:
|
||||
|
||||
```kotlin
|
||||
val filter = bot.startGettingUpdates(
|
||||
scope = CoroutineScope(Dispatchers.Default)
|
||||
) {
|
||||
// place code from examples here with replacing of `filter` by `this`
|
||||
}
|
||||
```
|
||||
|
||||
### Getting of only text incoming messages
|
||||
|
||||
```kotlin
|
||||
filter.asContentMessagesFlow().onlyTextContentMessages().onEach {
|
||||
println(it.content)
|
||||
println(it.fullEntitiesList())
|
||||
}.launchIn(
|
||||
CoroutineScope(Dispatchers.Default)
|
||||
)
|
||||
```
|
||||
|
||||
As a result, each received message which will be just text message will be printed out with full list of its internal entities
|
||||
48
TelegramBotAPI-extensions-utils/build.gradle
Normal file
48
TelegramBotAPI-extensions-utils/build.gradle
Normal file
@@ -0,0 +1,48 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
|
||||
}
|
||||
|
||||
project.version = "$library_version"
|
||||
project.group = "$library_group"
|
||||
|
||||
apply from: "publish.gradle"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js()
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") {
|
||||
api "${project.group}:TelegramBotAPI:$library_version"
|
||||
} else {
|
||||
implementation project(":TelegramBotAPI")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
TelegramBotAPI-extensions-utils/maven.publish.gradle
Normal file
57
TelegramBotAPI-extensions-utils/maven.publish.gradle
Normal file
@@ -0,0 +1,57 @@
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
task javadocsJar(type: Jar) {
|
||||
classifier = 'javadoc'
|
||||
}
|
||||
|
||||
afterEvaluate {
|
||||
project.publishing.publications.all {
|
||||
// rename artifacts
|
||||
groupId "${project.group}"
|
||||
if (it.name.contains('kotlinMultiplatform')) {
|
||||
artifactId = "${project.name}"
|
||||
} else {
|
||||
artifactId = "${project.name}-$name"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications.all {
|
||||
artifact javadocsJar
|
||||
|
||||
pom.withXml {
|
||||
asNode().children().last() + {
|
||||
resolveStrategy = Closure.DELEGATE_FIRST
|
||||
|
||||
description "Util extensions for more useful work with updates and other things"
|
||||
name "Telegram Bot API Utility Extensions"
|
||||
url "https://insanusmokrassar.github.io/TelegramBotAPI"
|
||||
|
||||
scm {
|
||||
developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||
url "https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||
}
|
||||
|
||||
developers {
|
||||
|
||||
developer {
|
||||
id "InsanusMokrassar"
|
||||
name "Ovsiannikov Aleksei"
|
||||
email "ovsyannikov.alexey95@gmail.com"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
licenses {
|
||||
|
||||
license {
|
||||
name "Apache Software License 2.0"
|
||||
url "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Utility Extensions","description":"Util extensions for more useful work with updates and other things","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"}
|
||||
55
TelegramBotAPI-extensions-utils/publish.gradle
Normal file
55
TelegramBotAPI-extensions-utils/publish.gradle
Normal file
@@ -0,0 +1,55 @@
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
|
||||
apply from: "maven.publish.gradle"
|
||||
|
||||
bintray {
|
||||
user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
||||
key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
||||
filesSpec {
|
||||
from "${buildDir}/publications/"
|
||||
eachFile {
|
||||
String directorySubname = it.getFile().parentFile.name
|
||||
if (it.getName() == "module.json") {
|
||||
if (directorySubname == "kotlinMultiplatform") {
|
||||
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module")
|
||||
} else {
|
||||
it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module")
|
||||
}
|
||||
} else {
|
||||
if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") {
|
||||
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom")
|
||||
} else {
|
||||
it.exclude()
|
||||
}
|
||||
}
|
||||
}
|
||||
into "${project.group}".replace(".", "/")
|
||||
}
|
||||
pkg {
|
||||
repo = "StandardRepository"
|
||||
name = "${project.name}"
|
||||
vcsUrl = "https://github.com/InsanusMokrassar/TelegramBotAPI"
|
||||
licenses = ["Apache-2.0"]
|
||||
version {
|
||||
name = "${project.version}"
|
||||
released = new Date()
|
||||
vcsTag = "${project.version}"
|
||||
gpg {
|
||||
sign = true
|
||||
passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bintrayUpload.doFirst {
|
||||
publications = publishing.publications.collect {
|
||||
if (it.name.contains('kotlinMultiplatform')) {
|
||||
null
|
||||
} else {
|
||||
it.name
|
||||
}
|
||||
} - null
|
||||
}
|
||||
|
||||
bintrayUpload.dependsOn publishToMavenLocal
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
|
||||
fun <T : CallbackQuery> Flow<T>.onlyMessageDataCallbackQueries() = mapNotNull {
|
||||
it as? MessageDataCallbackQuery
|
||||
}
|
||||
fun <T : CallbackQuery> Flow<T>.onlyInlineMessageIdDataCallbackQueries() = mapNotNull {
|
||||
it as? InlineMessageIdDataCallbackQuery
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun <T : MessageContent> Flow<ContentMessage<*>>.withContentType(contentType: KClass<T>) = mapNotNull {
|
||||
if (contentType.isInstance(it.content)) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
it as ContentMessage<T>
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun Flow<ContentMessage<*>>.onlyAnimationContentMessages() = withContentType(AnimationContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyAudioContentMessages() = withContentType(AudioContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyContactContentMessages() = withContentType(ContactContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyDiceContentMessages() = withContentType(DiceContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyDocumentContentMessages() = withContentType(DocumentContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyGameContentMessages() = withContentType(GameContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyInvoiceContentMessages() = withContentType(InvoiceContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyLocationContentMessages() = withContentType(LocationContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyPhotoContentMessages() = withContentType(PhotoContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyPollContentMessages() = withContentType(PollContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyStickerContentMessages() = withContentType(StickerContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyTextContentMessages() = withContentType(TextContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyVenueContentMessages() = withContentType(VenueContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyVideoContentMessages() = withContentType(VideoContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyVideoNoteContentMessages() = withContentType(VideoNoteContent::class)
|
||||
fun Flow<ContentMessage<*>>.onlyVoiceContentMessages() = withContentType(VoiceContent::class)
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.CallbackQueryUpdate
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
|
||||
fun Flow<CallbackQueryUpdate>.asDataCallbackQueryFlow() = mapNotNull {
|
||||
it.data as? DataCallbackQuery
|
||||
}
|
||||
fun Flow<CallbackQueryUpdate>.asGameShortNameCallbackQueryFlow() = mapNotNull {
|
||||
it.data as? GameShortNameCallbackQuery
|
||||
}
|
||||
fun Flow<CallbackQueryUpdate>.asUnknownCallbackQueryFlow() = mapNotNull {
|
||||
it.data as? UnknownCallbackQueryType
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.filterExactCommands(
|
||||
commandRegex: Regex
|
||||
) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage ->
|
||||
(contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||
}
|
||||
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.filterCommandsInsideTextMessages(
|
||||
commandRegex: Regex
|
||||
) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage ->
|
||||
contentMessage.content.fullEntitiesList().any {
|
||||
(it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Result [Flow] will emit all [TextSource]s to the collector ONLY IN CASE if first [TextSource] is
|
||||
* [BotCommandTextSource] and its [BotCommandTextSource.command] is [Regex.matches] to incoming [commandRegex]. Internal
|
||||
* behaviour contains next rules: all incoming text sources will be passed as is, [RegularTextSource] will be divided
|
||||
* by " " for several [RegularTextSource] which will contains not empty args without spaces
|
||||
*/
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.filterCommandsWithArgs(
|
||||
commandRegex: Regex
|
||||
): Flow<List<TextSource>> = asContentMessagesFlow().onlyTextContentMessages().mapNotNull { contentMessage ->
|
||||
val allEntities = contentMessage.content.fullEntitiesList()
|
||||
(allEntities.firstOrNull() as? BotCommandTextSource) ?.let {
|
||||
if (commandRegex.matches(it.command)) {
|
||||
allEntities.flatMap {
|
||||
when (it) {
|
||||
is RegularTextSource -> it.source.split(" ").mapNotNull { regularTextSourcePart ->
|
||||
if (regularTextSourcePart.isNotBlank()) {
|
||||
RegularTextSource(regularTextSourcePart)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
else -> listOf(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.asContentMessagesFlow() = mapNotNull {
|
||||
it.data as? ContentMessage<*>
|
||||
}
|
||||
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.asChatEventsFlow() = mapNotNull {
|
||||
it.data as? ChatEventMessage
|
||||
}
|
||||
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.asUnknownMessagesFlow() = mapNotNull {
|
||||
it.data as? UnknownMessageType
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
|
||||
fun <T : BaseMessageUpdate> Flow<T>.filterBaseMessageUpdates(chatId: ChatId): Flow<T> = filter {
|
||||
it.data.chat.id == chatId
|
||||
}
|
||||
fun <T : BaseMessageUpdate> Flow<T>.filterBaseMessageUpdates(chat: Chat): Flow<T> = filterBaseMessageUpdates(chat.id)
|
||||
|
||||
|
||||
fun <T : SentMediaGroupUpdate> Flow<T>.filterSentMediaGroupUpdates(chatId: ChatId): Flow<T> = filter {
|
||||
it.data.first().chat.id == chatId
|
||||
}
|
||||
fun <T : SentMediaGroupUpdate> Flow<T>.filterSentMediaGroupUpdates(chat: Chat): Flow<T> = filterSentMediaGroupUpdates(chat.id)
|
||||
@@ -1,9 +1,7 @@
|
||||
# TelegramBotAPI
|
||||
|
||||
[](https://github.com/KotlinBy/awesome-kotlin)
|
||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion)
|
||||
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI)
|
||||
[](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI_master__publishing/)
|
||||
|
||||
## What is it?
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ interface TextSource {
|
||||
val asMarkdownSource: String
|
||||
val asMarkdownV2Source: String
|
||||
val asHtmlSource: String
|
||||
val source: String
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.RequestL
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.receive
|
||||
@@ -17,7 +17,6 @@ import io.ktor.client.features.*
|
||||
import io.ktor.client.statement.HttpStatement
|
||||
import io.ktor.client.statement.readText
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
class KtorRequestsExecutor(
|
||||
@@ -43,8 +42,21 @@ class KtorRequestsExecutor(
|
||||
}
|
||||
|
||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||
return try {
|
||||
supervisorScope {
|
||||
return handleSafely(
|
||||
{ e ->
|
||||
throw if (e is ClientRequestException) {
|
||||
val content = e.response.readText()
|
||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||
newRequestException(
|
||||
responseObject,
|
||||
content,
|
||||
"Can't get result object from $content"
|
||||
)
|
||||
} else {
|
||||
e
|
||||
}
|
||||
}
|
||||
) {
|
||||
requestsLimiter.limit {
|
||||
var statement: HttpStatement? = null
|
||||
for (factory in callsFactories) {
|
||||
@@ -81,17 +93,6 @@ class KtorRequestsExecutor(
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (e: ClientRequestException) {
|
||||
val content = e.response.readText()
|
||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||
throw newRequestException(
|
||||
responseObject,
|
||||
content,
|
||||
"Can't get result object from $content"
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
|
||||
@@ -82,6 +82,7 @@ const val containsMasksField = "contains_masks"
|
||||
const val resultIdField = "result_id"
|
||||
const val inlineMessageIdField = "inline_message_id"
|
||||
const val callbackDataField = "callback_data"
|
||||
const val callbackGameField = "callback_game"
|
||||
const val callbackQueryIdField = "callback_query_id"
|
||||
const val inlineQueryIdField = "inline_query_id"
|
||||
const val inlineKeyboardField = "inline_keyboard"
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class BoldTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
private val commandRegex = Regex("[/!][^@\\s]*")
|
||||
|
||||
class BotCommandTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
val command: String by lazy {
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class CashTagTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class CodeTextSource(
|
||||
source: String
|
||||
override val source: String
|
||||
) : TextSource {
|
||||
override val asMarkdownSource: String by lazy { source.codeMarkdown() }
|
||||
override val asMarkdownV2Source: String by lazy { source.codeMarkdownV2() }
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class EMailTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -12,7 +12,7 @@ private val String.withoutSharp
|
||||
}
|
||||
|
||||
class HashTagTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy {
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class ItalicTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -12,7 +12,7 @@ private val String.withoutCommercialAt
|
||||
}
|
||||
|
||||
class MentionTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy {
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class PhoneNumberTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class PreTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
val language: String? = null
|
||||
) : TextSource {
|
||||
override val asMarkdownSource: String by lazy { source.preMarkdown(language) }
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class RegularTextSource(
|
||||
source: String
|
||||
override val source: String
|
||||
) : TextSource {
|
||||
override val asMarkdownSource: String by lazy { source.regularMarkdown() }
|
||||
override val asMarkdownV2Source: String by lazy { source.regularMarkdownV2() }
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class StrikethroughTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class TextLinkTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
url: String
|
||||
) : TextSource {
|
||||
override val asMarkdownSource: String by lazy { source.linkMarkdown(url) }
|
||||
|
||||
@@ -6,7 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateCh
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class TextMentionTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
privateChat: PrivateChat,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class URLTextSource(
|
||||
source: String
|
||||
override val source: String
|
||||
) : TextSource {
|
||||
override val asMarkdownSource: String by lazy { source.linkMarkdown(source) }
|
||||
override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(source) }
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||
|
||||
class UnderlineTextSource(
|
||||
source: String,
|
||||
override val source: String,
|
||||
textParts: List<TextPart>
|
||||
) : MultilevelTextSource {
|
||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.games.CallbackGame
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
|
||||
@Serializable(InlineKeyboardButtonSerializer::class)
|
||||
sealed class InlineKeyboardButton {
|
||||
abstract val text: String
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class UnknownInlineKeyboardButton internal constructor(
|
||||
override val text: String,
|
||||
val rawData: JsonElement
|
||||
) : InlineKeyboardButton()
|
||||
|
||||
@Serializable
|
||||
data class PayInlineKeyboardButton(
|
||||
override val text: String,
|
||||
@@ -24,6 +33,14 @@ data class CallbackDataInlineKeyboardButton(
|
||||
val callbackData: String
|
||||
) : InlineKeyboardButton()
|
||||
|
||||
@Serializable
|
||||
data class CallbackGameInlineKeyboardButton(
|
||||
@SerialName(textField)
|
||||
override val text: String,
|
||||
@SerialName(callbackGameField)
|
||||
val callbackGame: CallbackGame
|
||||
) : InlineKeyboardButton()
|
||||
|
||||
@Serializable
|
||||
data class LoginURLInlineKeyboardButton(
|
||||
override val text: String,
|
||||
|
||||
@@ -3,8 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardB
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.JsonElementSerializer
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
|
||||
override val descriptor: SerialDescriptor = SerialDescriptor(
|
||||
@@ -12,22 +11,25 @@ internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButto
|
||||
PolymorphicKind.SEALED
|
||||
)
|
||||
|
||||
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton> {
|
||||
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton>? {
|
||||
return when {
|
||||
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
|
||||
json[callbackGameField] != null -> CallbackGameInlineKeyboardButton.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")
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
|
||||
val json = JsonElementSerializer.deserialize(decoder)
|
||||
|
||||
return nonstrictJsonFormat.fromJson(resolveSerializer(json.jsonObject), json)
|
||||
return (json as? JsonObject) ?.let { resolveSerializer(it) } ?.let {
|
||||
nonstrictJsonFormat.fromJson(it, json)
|
||||
} ?: UnknownInlineKeyboardButton("", json)
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
|
||||
@@ -38,6 +40,8 @@ internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButto
|
||||
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||
is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||
is UnknownInlineKeyboardButton -> JsonElementSerializer.serialize(encoder, value.rawData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
||||
|
||||
data class EditChannelPostUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: Message
|
||||
override val data: CommonMessage<*>
|
||||
) : BaseEditMessageUpdate
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
||||
|
||||
data class EditMessageUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: Message
|
||||
override val data: CommonMessage<*>
|
||||
) : BaseEditMessageUpdate
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
|
||||
|
||||
interface MediaGroupUpdate : Update
|
||||
|
||||
@@ -11,7 +10,7 @@ interface SentMediaGroupUpdate: MediaGroupUpdate {
|
||||
val origins: List<BaseMessageUpdate>
|
||||
}
|
||||
|
||||
interface EditMediaGroupUpdate : MediaGroupUpdate {
|
||||
interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate {
|
||||
override val data: MediaGroupMessage
|
||||
val origin: BaseMessageUpdate
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.RawCallbac
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.RawInlineQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
|
||||
@@ -13,8 +13,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
|
||||
@Serializable
|
||||
@@ -22,11 +21,11 @@ internal data class RawUpdate constructor(
|
||||
@SerialName(updateIdField)
|
||||
val updateId: UpdateIdentifier,
|
||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||
private val edited_message: Message? = null,
|
||||
private val edited_message: CommonMessage<*>? = null,
|
||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||
private val message: Message? = null,
|
||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||
private val edited_channel_post: Message? = null,
|
||||
private val edited_channel_post: CommonMessage<*>? = null,
|
||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||
private val channel_post: Message? = null,
|
||||
private val inline_query: RawInlineQuery? = null,
|
||||
@@ -42,7 +41,8 @@ internal data class RawUpdate constructor(
|
||||
* @return One of children of [Update] interface or null in case of unknown type of update
|
||||
*/
|
||||
fun asUpdate(raw: JsonElement): Update {
|
||||
return initedUpdate ?: when {
|
||||
return initedUpdate ?: try {
|
||||
when {
|
||||
edited_message != null -> EditMessageUpdate(updateId, edited_message)
|
||||
message != null -> MessageUpdate(updateId, message)
|
||||
edited_channel_post != null -> EditChannelPostUpdate(updateId, edited_channel_post)
|
||||
@@ -60,7 +60,15 @@ internal data class RawUpdate constructor(
|
||||
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
||||
else -> UnknownUpdateType(
|
||||
updateId,
|
||||
raw.toString()
|
||||
raw.toString(),
|
||||
raw
|
||||
)
|
||||
}
|
||||
} catch (e: SerializationException) {
|
||||
UnknownUpdateType(
|
||||
updateId,
|
||||
raw.toString(),
|
||||
raw
|
||||
)
|
||||
}.also {
|
||||
initedUpdate = it
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
||||
|
||||
interface BaseEditMessageUpdate : BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||
|
||||
interface BaseEditMessageUpdate : BaseMessageUpdate {
|
||||
override val data: CommonMessage<*>
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonElementSerializer
|
||||
|
||||
interface Update {
|
||||
@@ -13,7 +14,8 @@ interface Update {
|
||||
|
||||
data class UnknownUpdateType(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: String
|
||||
override val data: String,
|
||||
val rawJson: JsonElement
|
||||
) : Update
|
||||
|
||||
internal object UpdateSerializerWithoutDeserialization : KSerializer<Update> {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
|
||||
/**
|
||||
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
|
||||
*
|
||||
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
|
||||
* exception will be available for catching
|
||||
*/
|
||||
suspend inline fun <T> handleSafely(
|
||||
noinline onException: suspend (Exception) -> T = { throw it },
|
||||
noinline block: suspend CoroutineScope.() -> T
|
||||
): T {
|
||||
return try {
|
||||
supervisorScope(block)
|
||||
} catch (e: Exception) {
|
||||
onException(e)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ uuid_version=0.1.0
|
||||
ktor_version=1.3.2
|
||||
|
||||
library_group=com.github.insanusmokrassar
|
||||
library_version=0.26.1
|
||||
library_version=0.26.3
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
include ":TelegramBotAPI"
|
||||
include ":TelegramBotAPI-extensions-api"
|
||||
include ":TelegramBotAPI-extensions-utils"
|
||||
|
||||
Reference in New Issue
Block a user