1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-03 04:46:16 +00:00

Compare commits

..

69 Commits

Author SHA1 Message Date
0ca815760b content messages renames 2021-02-07 16:54:48 +06:00
4748b6813a ExceptionsOnlyLimiter now will not use Result to provide success/failure state of result 2021-02-07 12:40:05 +06:00
a2ea15d4b0 update wrapper version 2021-02-07 11:02:13 +06:00
27dc302f5d update for compiling 2021-02-06 11:53:30 +06:00
baf8ed3a77 now JVM part will be compiled with IR 2021-02-06 00:52:39 +06:00
18913af3c3 update publishing scripts 2021-02-06 00:50:51 +06:00
51871ea94c update micro_utils version 2021-02-05 16:08:45 +06:00
6a625d7b9f update publishing scripts 2021-02-05 16:08:28 +06:00
0b93b472a3 update klock 2021-02-05 14:22:42 +06:00
86bfe043a5 update dependencies 2021-02-03 23:05:17 +06:00
fa18e7299d 0.32.4 2021-02-03 22:59:37 +06:00
c986025075 Merge pull request #277 from InsanusMokrassar/0.32.3
0.32.3
2021-02-02 11:44:53 +06:00
e8a7ea9ce4 inline qieries expectators and waiters 2021-02-02 11:17:51 +06:00
30f35e5488 start 0.32.3 2021-02-02 11:07:30 +06:00
b626974ea7 Create dependabot.yml 2021-02-01 23:22:26 +06:00
128b782a27 Merge pull request #276 from InsanusMokrassar/0.32.2
0.32.2
2021-02-01 21:43:21 +06:00
dc83b8d77a fix of #275 and several other internal problems 2021-02-01 21:09:11 +06:00
2aac6c0bde start 0.32.2 2021-02-01 21:04:54 +06:00
b637d0d2a6 Merge pull request #274 from InsanusMokrassar/0.32.1
0.32.1
2021-01-30 21:06:15 +06:00
d12cba2343 replacing of withContent and requireWithContent and extending of extensions receivers types 2021-01-30 20:59:26 +06:00
450c7e6474 fix of #273 2021-01-30 20:05:07 +06:00
a35fb40a66 fix of #272 2021-01-30 19:35:53 +06:00
8731aa2c26 start 0.32.1 2021-01-30 19:33:38 +06:00
32e305537d Merge pull request #260 from InsanusMokrassar/0.32.0
0.32.0
2021-01-29 17:00:06 +06:00
ec0a10e36e fillup changelog 2021-01-29 13:28:18 +06:00
131ec4d6d5 renames of EncryptedPassportElements 2021-01-29 12:44:13 +06:00
ecc608f51a add additional variant of doInDecryptionContextWithPKCS8Key 2021-01-29 10:44:11 +06:00
040654f131 refill readme 2021-01-29 01:18:44 +06:00
9e73d0c461 add opportunity to manually set up delay for media groups webhooks 2021-01-29 00:56:47 +06:00
d67c80bc99 ClassCasts update for SecureValue 2021-01-28 22:21:21 +06:00
b0eef4f82d update ktor 2021-01-28 22:06:52 +06:00
fbe91a6321 complete preview tools for passport 2021-01-28 22:03:14 +06:00
15066c9d63 add SecureData and DecryptedCredentials 2021-01-28 17:08:25 +06:00
e0bf67d8f9 global update of passport 2021-01-28 16:03:30 +06:00
1dc3ce2fb5 add passport in API 2021-01-25 10:43:51 +06:00
0c71133969 add expectations and triggers for passport 2021-01-25 10:29:11 +06:00
1359dd549f Create regular-build.yml 2021-01-25 04:42:23 +06:00
c425e2ecc3 update classcasts 2021-01-23 21:10:57 +06:00
4d63e3a17d complete tools for handling of encrypted data 2021-01-23 20:50:46 +06:00
d34deade0d temporal state (currently tools for files decryption in JS do work 2021-01-23 18:49:41 +06:00
2f52ad45a4 add tools for passport files hashes calculation 2021-01-23 12:21:57 +06:00
1d99e632a4 fix of SetPassportDataErrors request parent interface 2021-01-22 23:40:33 +06:00
cdcfaf5a29 fixes in passport errors serialization and replacing of PassportElementError 2021-01-22 23:14:24 +06:00
f3590762f3 add PassportElementError realization 2021-01-22 23:01:37 +06:00
e0ff14b7fe update gradle wrapper 2021-01-22 21:57:23 +06:00
9983e111ec update EncryptedElementSerializer 2021-01-22 21:53:37 +06:00
08b8710772 add asAction and several variants of oneOf 2021-01-19 20:19:28 +06:00
fc71e028c4 improving work of behaviour_builder 2021-01-19 18:50:45 +06:00
30a4a7bd8b update micro_utils dependency 2021-01-19 13:52:06 +06:00
b973278b0a add note about breaking changes 2021-01-19 11:00:12 +06:00
ae2f4579e2 start 0.32.0 as a migration from 0.31.1 due to breaking changes in MediaGroupMessage type 2021-01-19 10:52:30 +06:00
a4bf6911c7 Update micro_utils version note 2021-01-19 00:40:29 +06:00
73b3daa68b Update micro_utils dependency 2021-01-19 00:39:46 +06:00
09748615ae optimize imports 2021-01-18 23:43:21 +06:00
2dc8521aed now all data field are predecrypted with Base64StringSerializer 2021-01-17 16:44:56 +06:00
81de59f37c fix of translations in UtilityBill 2021-01-17 16:34:05 +06:00
fb61a94c5e fix defaults in encrypted passport data 2021-01-17 16:30:25 +06:00
26fd5e51bf filesSize in PassportFile is nullable for now 2021-01-17 16:24:33 +06:00
c1ab9da4c4 fix error with deserialization of passport data 2021-01-17 16:11:38 +06:00
6b414d64b0 remove redundant imports 2021-01-17 15:45:20 +06:00
5a3edc2b44 add passportMessages extension 2021-01-17 15:43:08 +06:00
6dcdc2ab7f now PassportMessage contains user too 2021-01-17 15:35:25 +06:00
a2ae4f71de new PassportMessage type 2021-01-15 16:27:22 +06:00
5d87b86afe update passport data to use Base64StringSerializer 2021-01-15 16:02:16 +06:00
ef22735894 add base64 serialization dependency and update micro_utils up to 0.4.17 2021-01-14 21:27:58 +06:00
c0ea479fe3 add primitives for Telegram Passport API 2021-01-14 14:03:25 +06:00
0846e816e9 start 0.31.1 2021-01-14 12:37:31 +06:00
d837c9d605 update publication scripts 2021-01-13 14:21:35 +06:00
d1993842c3 Merge pull request #257 from InsanusMokrassar/0.31.0
0.31.0
2021-01-10 13:08:00 +06:00
125 changed files with 2637 additions and 403 deletions

11
.github/dependabot.yml vendored Normal file
View File

@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "gradle" # See documentation for possible values
directory: "/tgbotapi.core" # Location of package manifests
schedule:
interval: "daily"

16
.github/workflows/regular-build.yml vendored Normal file
View File

@@ -0,0 +1,16 @@
name: Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build

View File

@@ -1,5 +1,97 @@
# TelegramBotAPI changelog
## 0.32.4
* `Common`:
* `Version`:
* `Kotlin`: `1.4.21` -> `1.4.30`
* `Klock`: `2.0.4` -> `2.0.6`
* `MicroUtils`: `0.4.23` -> `0.4.24`
* `Core`:
* Renames:
* `ChannelMessage` -> `ChannelContentMessage`
* `PublicMessage` -> `PublicContentMessage`
* `GroupMessage` -> `GroupContentMessage`
* `FromChannelGroupMessage` -> `FromChannelGroupContentMessage`
* `AnonymousGroupMessage` -> `AnonymousGroupContentMessage`
* `CommonGroupMessage` -> `CommonGroupContentMessage`
* `PrivateMessage` -> `PrivateContentMessage`
* `Extensions Utils`:
* Renames of extensions in `ClassCasts` according to changes in `Core`
## 0.32.3
* `Behaviour Builder`:
* Add expectators and waiters for inline queries
## 0.32.2
* `Core`:
* Fix of [#275](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/275)
## 0.32.1
* `Core`:
* Fix of [#272](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/272)
* `Utils`:
* Fix of [#273](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/273)
## 0.32.0
**THIS UPDATE CONTAINS BREAKING CHANGES**
* `Common`:
* `Version`:
* `MicroUtils`: `0.4.16` -> `0.4.23`
* `Klock`: `0.2.3` -> `0.2.4`
* `Ktor`: `1.5.0` -> `1.5.1`
* `Core`:
* **BREAKING CHANGE** Now `MediaGroupMessage` have a generic type related to `MediaGroupContent`
* Methods and types related to `MediaGroupMessage` have been modified according to their meanings
* **Important Change** `FlowsUpdatesFilter` now is an interface. Old class has been renamed to
`DefaultFlowsUpdatesFilter` and factory method `FlowsUpdatesFilter` has been added
* **PASSPORT** Full support of `Telegram Passport API`
* `PassportData`
* All variants of `EncryptedPassportElement`
* All variants of `SecureValue`
* All variants of `PassportElementError`
* New request `SetPassportDataErrors`
* `Credentials`:
* `EncryptedCredentials`
* `DeryptedCredentials`
* `EndDataCredentials`
* `Behaviour Builder`:
* Trigger and expectation extensions for `MessageContent` (`onContentMessage` and `waitContentMessage`)
* `onMediaGroup` has been replaced
* `waitMediaGroup` has been added
* `onVisualMediaGroup` now is just an alternative to `onVisualGallery`
* `command` and `onCommand` expectations has been added for commands `String` variant
* New extensions `BehaviourContext#oneOf`, `BehaviourContext#parallel` and `Deferred<T>#withAction`
* Several renames:
* `waitAudioMediaGroup` -> `waitAudioMediaGroupContent`
* `waitDocumentMediaGroup` -> `waitDocumentMediaGroupContent`
* `waitMediaGroup` -> `waitAnyMediaGroupContent`
* `waitVisualMediaGroup` -> `waitVisualMediaGroupContent`
* New extensions `BehaviourContext#waitPassportMessagesWith` and `BehaviourContext#waitAnyPassportMessages`
* New extensions `BehaviourContext#onPassportMessage` and `BehaviourContext#onPassportMessageWith`
* `Utils`:
* New `ClassCasts` for
* `Message`
* **PASSPORT** `EncryptedPassportElement`
* **PASSPORT** `PassportElementError`
* **PASSPORT** `SecureValue`
* Several tools for decryption have been added:
* `AESDecryptor` is available for `JVM` platform
* Extensions `EncryptedCredentials#decryptWithPKCS8PrivateKey` are available for `JVM`
platform
* Extensions `EndDataCredentials#decryptData` and `FileCredentials#decryptFile` have been added
* Several extensions `createDecryptor`
* Several extensions `doInDecryptionContextWithPKCS8Key`
* New extension `Flow#passportMessages`
* In most of webhook setting up functions/methods now available parameter `mediaGroupsDebounceTimeMillis`
* `API`:
* **PASSPORT** New extensions `TelegramBot#setPassportDataErrors`
## 0.31.0
**THIS UPDATE CONTAINS BREAKING CHANGES**

View File

@@ -5,18 +5,18 @@ kotlin.js.generate.externals=true
kotlin.incremental=true
kotlin.incremental.js=true
kotlin_version=1.4.21
kotlin_version=1.4.30
kotlin_coroutines_version=1.4.2
kotlin_serialisation_runtime_version=1.0.1
klock_version=2.0.3
kotlin_serialisation_runtime_version=1.1.0-RC
klock_version=2.0.6
uuid_version=0.2.3
ktor_version=1.5.0
ktor_version=1.5.1
micro_utils_version=0.4.16
micro_utils_version=0.4.24
javax_activation_version=1.1.1
library_group=dev.inmo
library_version=0.31.0
library_version=0.32.4
github_release_plugin_version=2.2.12

View File

@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip

View File

@@ -11,9 +11,6 @@ moments are describing by official [Telegram Bot API](https://core.telegram.org/
## Compatibility
This version compatible with [4th of November 2020 update of TelegramBotAPI (version 5.0)](https://core.telegram.org/bots/api#november-4-2020).
There is only one exception of implemented functionality - Telegram Passport API, which was presented in
[August 2018 update of TelegramBotAPI](https://core.telegram.org/bots/api-changelog#august-27-2018) update. It will be implemented
as soon as possible.
## How to implement library?
@@ -149,3 +146,18 @@ Here was used `okhttp` realisation of client, but there are several others engin
available on ktor.io site for [client](https://ktor.io/clients/http-client/engines.html) and [server](https://ktor.io/quickstart/artifacts.html)
engines.
### Passport
In case you wish to work with `Telegram Passport`, currently there are several useful things, but most part of working
with decryption and handling is available only on JVM. Next snippet contains example of data decryption on JVM platform:
```kotlin
passportMessage.passportData.doInDecryptionContextWithPKCS8Key(privateKey) {
val passportDataSecureValue = passport ?.data ?: return@doInDecryptionContextWithPKCS8Key
val passportData = (passportMessage.passportData.data.firstOrNull { it is CommonPassport } ?: return@doInDecryptionContextWithPKCS8Key) as CommonPassport
val decrypted = passportDataSecureValue.decrypt(
passportData.data
) ?.decodeToString() ?: return@doInDecryptionContextWithPKCS8Key
println(decrypted)
}
```

View File

@@ -29,7 +29,9 @@ repositories {
}
kotlin {
jvm()
jvm {
compilations.main.kotlinOptions.useIR = true
}
js(BOTH) {
browser()
nodejs()
@@ -46,7 +48,10 @@ kotlin {
api "com.soywiz.korlibs.klock:klock:$klock_version"
api "com.benasher44:uuid:$uuid_version"
api "dev.inmo:micro_utils.crypto:$micro_utils_version"
api "dev.inmo:micro_utils.coroutines:$micro_utils_version"
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
api "dev.inmo:micro_utils.serialization.encapsulator:$micro_utils_version"
api "io.ktor:ktor-client-core:$ktor_version"
}
@@ -73,7 +78,6 @@ kotlin {
implementation kotlin('test-junit')
}
}
jsTest {
dependencies {
implementation kotlin('test-junit')

View File

@@ -1 +1 @@
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Core","description":"Library for Object-Oriented and type-safe work with Telegram Bot API","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Core","description":"Library for Object-Oriented and type-safe work with Telegram Bot API","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","includeGpgSigning":true,"publishToMavenCentral":true,"developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}

View File

@@ -1,21 +1,10 @@
apply plugin: 'maven-publish'
apply plugin: 'signing'
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
@@ -59,7 +48,22 @@ publishing {
password = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
}
}
maven {
name = "sonatype"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
}
}
}
}
}
}
signing {
useGpgCmd()
sign publishing.publications
}

View File

@@ -17,7 +17,9 @@ class CommonLimiter(
@Transient
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
) : RequestLimiter {
@Transient
private val quotaSemaphore = Semaphore(lockCount)
@Transient
private val counterRegeneratorJob = scope.launch {
val regenDelay: MilliSeconds = (regenTime.toDouble() / lockCount).roundToLong()
while (isActive) {

View File

@@ -37,11 +37,12 @@ class ExceptionsOnlyLimiter(
override suspend fun <T> limit(block: suspend () -> T): T {
while (true) {
lockState.first { !it }
var throwable: Throwable? = null
val result = safely({
when (it) {
throwable = when (it) {
is TooMuchRequestsException -> {
lock(it.retryAfter.leftToRetry)
Result.failure(it)
it
}
is ClientRequestException -> {
if (it.response.status == HttpStatusCode.TooManyRequests) {
@@ -49,15 +50,16 @@ class ExceptionsOnlyLimiter(
} else {
throw it
}
Result.failure(it)
it
}
else -> throw it
}
null
}) {
Result.success(block())
block()
}
if (result.isSuccess) {
return result.getOrNull()!!
if (throwable == null) {
return result!!
}
}
}

View File

@@ -0,0 +1,22 @@
package dev.inmo.tgbotapi.requests
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.PassportElementError
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class SetPassportDataErrors(
@SerialName(userIdField)
val user: UserId,
@SerialName(errorsField)
val errors: List<PassportElementError>
) : SimpleRequest<Boolean> {
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
override fun method(): String = "setPassportDataErrors"
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -16,7 +16,7 @@ data class AnswerCallbackQuery(
val showAlert: Boolean? = null,
@SerialName(urlField)
val url: String? = null,
@SerialName(cachedTimeField)
@SerialName(cacheTimeField)
val cachedTimeSeconds: Int? = null
) : SimpleRequest<Boolean> {
override fun method(): String = "answerCallbackQuery"

View File

@@ -16,7 +16,7 @@ data class AnswerInlineQuery(
@Serializable(InlineQueryAnswersResultsSerializer::class)
@SerialName(resultsField)
val results: List<InlineQueryResult> = emptyList(),
@SerialName(cachedTimeField)
@SerialName(cacheTimeField)
val cachedTime: Int? = null,
@SerialName(isPersonalField)
val isPersonal: Boolean? = null,

View File

@@ -18,6 +18,8 @@ data class SetChatPhoto (
override fun method(): String = "setChatPhoto"
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
@Transient
override val mediaMap: Map<String, MultipartFile> = mapOf(photoField to photo)
@Transient
override val paramsJson: JsonObject = toJson(serializer())
}

View File

@@ -10,6 +10,7 @@ import dev.inmo.tgbotapi.utils.throwRangeError
import kotlinx.serialization.*
private val commonResultDeserializer = TelegramBotAPIMessageDeserializationStrategyClass<ContentMessage<LocationContent>>()
const val editMessageLiveLocationMethod = "editMessageLiveLocation"
@Serializable
data class EditChatMessageLiveLocation(
@@ -30,7 +31,7 @@ data class EditChatMessageLiveLocation(
@SerialName(replyMarkupField)
override val replyMarkup: InlineKeyboardMarkup? = null
) : EditChatMessage<LocationContent>, EditReplyMessage, EditLocationMessage {
override fun method(): String = "editMessageLiveLocation"
override fun method(): String = editMessageLiveLocationMethod
override val resultDeserializer: DeserializationStrategy<ContentMessage<LocationContent>>
get() = commonResultDeserializer
override val requestSerializer: SerializationStrategy<*>

View File

@@ -23,7 +23,7 @@ data class EditInlineMessageLiveLocation(
@SerialName(replyMarkupField)
override val replyMarkup: InlineKeyboardMarkup? = null
) : EditInlineMessage, EditReplyMessage, EditLocationMessage {
override fun method(): String = "editMessageLiveLocation"
override fun method(): String = editMessageLiveLocationMethod
override val requestSerializer: SerializationStrategy<*>
get() = serializer()

View File

@@ -10,6 +10,7 @@ import dev.inmo.tgbotapi.types.message.content.LocationContent
import kotlinx.serialization.*
private val commonResultDeserializer = TelegramBotAPIMessageDeserializationStrategyClass<ContentMessage<LocationContent>>()
const val stopMessageLiveLocationMethod = "stopMessageLiveLocation"
@Serializable
data class StopChatMessageLiveLocation(
@@ -20,7 +21,7 @@ data class StopChatMessageLiveLocation(
@SerialName(replyMarkupField)
override val replyMarkup: InlineKeyboardMarkup? = null
) : EditChatMessage<LocationContent>, EditReplyMessage {
override fun method(): String = "stopMessageLiveLocation"
override fun method(): String = stopMessageLiveLocationMethod
override val resultDeserializer: DeserializationStrategy<ContentMessage<LocationContent>>
get() = commonResultDeserializer
override val requestSerializer: SerializationStrategy<*>

View File

@@ -13,7 +13,7 @@ data class StopInlineMessageLiveLocation(
@SerialName(replyMarkupField)
override val replyMarkup: InlineKeyboardMarkup? = null
) : EditInlineMessage, EditReplyMessage {
override fun method(): String = "stopMessageLiveLocation"
override fun method(): String = stopMessageLiveLocationMethod
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -2,7 +2,6 @@ package dev.inmo.tgbotapi.requests.edit.text
import dev.inmo.tgbotapi.CommonAbstracts.*
import dev.inmo.tgbotapi.requests.edit.abstracts.*
import dev.inmo.tgbotapi.requests.edit.media.editMessageMediaMethod
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.MessageEntity.*
import dev.inmo.tgbotapi.types.ParseMode.ParseMode
@@ -55,10 +54,10 @@ data class EditInlineMessageText internal constructor(
override val replyMarkup: InlineKeyboardMarkup? = null
) : EditInlineMessage, EditTextChatMessage, EditReplyMessage, EditDisableWebPagePreviewMessage {
override val entities: List<TextSource>? by lazy {
rawEntities ?.asTextParts(text ?: return@lazy null) ?.justTextSources()
rawEntities ?.asTextParts(text) ?.justTextSources()
}
override fun method(): String = editMessageMediaMethod
override fun method(): String = editMessageTextMethod
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -8,6 +8,10 @@ import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.InputMedia.*
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.abstracts.VisualMediaGroupContent
import dev.inmo.tgbotapi.types.message.content.media.AudioContent
import dev.inmo.tgbotapi.types.message.content.media.DocumentContent
import dev.inmo.tgbotapi.utils.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer
@@ -17,13 +21,13 @@ const val rawSendingMediaGroupsWarning = "Media groups contains restrictions rel
" types. Currently it is possible to combine photo + video OR audio OR documents"
@RiskFeature(rawSendingMediaGroupsWarning)
fun SendMediaGroup(
fun <T : MediaGroupContent> SendMediaGroup(
chatId: ChatIdentifier,
media: List<MediaGroupMemberInputMedia>,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
): Request<List<MediaGroupMessage>> {
): Request<List<MediaGroupMessage<T>>> {
if (media.size !in mediaCountInMediaGroup) {
throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size)
}
@@ -47,14 +51,14 @@ fun SendMediaGroup(
allowSendingWithoutReply
)
return if (files.isEmpty()) {
return (if (files.isEmpty()) {
data
} else {
MultipartRequestImpl(
data,
SendMediaGroupFiles(files)
)
}
}) as Request<List<MediaGroupMessage<T>>>
}
/**
@@ -69,7 +73,7 @@ inline fun SendPlaylist(
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
) = SendMediaGroup<AudioContent>(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
/**
* Use this method to be sure that you are correctly sending documents media group
@@ -83,7 +87,7 @@ inline fun SendDocumentsGroup(
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
) = SendMediaGroup<DocumentContent>(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
/**
* Use this method to be sure that you are correctly sending visual media group
@@ -98,9 +102,9 @@ inline fun SendVisualMediaGroup(
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = SendMediaGroup(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
) = SendMediaGroup<VisualMediaGroupContent>(chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply)
private val messagesListSerializer: KSerializer<List<MediaGroupMessage>>
private val messagesListSerializer: KSerializer<List<MediaGroupMessage<MediaGroupContent>>>
= ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass())
@Serializable
@@ -114,7 +118,7 @@ data class SendMediaGroupData internal constructor(
override val replyToMessageId: MessageIdentifier? = null,
@SerialName(allowSendingWithoutReplyField)
override val allowSendingWithoutReply: Boolean? = null
) : DataRequest<List<MediaGroupMessage>>, SendMessageRequest<List<MediaGroupMessage>> {
) : DataRequest<List<MediaGroupMessage<MediaGroupContent>>>, SendMessageRequest<List<MediaGroupMessage<MediaGroupContent>>> {
@SerialName(mediaField)
private val convertedMedia: String
get() = buildJsonArray {
@@ -127,7 +131,7 @@ data class SendMediaGroupData internal constructor(
override fun method(): String = "sendMediaGroup"
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override val resultDeserializer: DeserializationStrategy<List<MediaGroupMessage>>
override val resultDeserializer: DeserializationStrategy<List<MediaGroupMessage<MediaGroupContent>>>
get() = messagesListSerializer
}

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.requests.send.polls
import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan
import dev.inmo.tgbotapi.CommonAbstracts.*
import dev.inmo.tgbotapi.requests.send.abstracts.ReplyingMarkupSendMessageRequest
import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest
@@ -16,6 +17,11 @@ import kotlinx.serialization.*
private val commonResultDeserializer: DeserializationStrategy<ContentMessage<PollContent>> = TelegramBotAPIMessageDeserializationStrategyClass()
private inline val ApproximateScheduledCloseInfo.openPeriod
get() = openDuration.millisecondsLong.div(1000)
private inline val ExactScheduledCloseInfo.closeDate
get() = closeDateTime.unixMillisLong.div(1000)
private fun checkPollInfo(
question: String,
options: List<String>
@@ -138,12 +144,23 @@ sealed class SendPoll : SendMessageRequest<ContentMessage<PollContent>>,
abstract val options: List<String>
abstract val isAnonymous: Boolean
abstract val isClosed: Boolean
abstract val closeInfo: ScheduledCloseInfo?
abstract val type: String
internal abstract val openPeriod: LongSeconds?
internal abstract val closeDate: LongSeconds?
protected val creationDate = DateTime.now()
open val closeInfo: ScheduledCloseInfo?
get() {
val openPeriod = openPeriod
val closeDate = closeDate
return when {
openPeriod != null -> openPeriod.asApproximateScheduledCloseInfo(creationDate)
closeDate != null -> closeDate.asExactScheduledCloseInfo
else -> null
}
}
override fun method(): String = "sendPoll"
override val resultDeserializer: DeserializationStrategy<ContentMessage<PollContent>>
get() = commonResultDeserializer
@@ -163,8 +180,10 @@ data class SendRegularPoll(
override val isClosed: Boolean = false,
@SerialName(allowsMultipleAnswersField)
val allowMultipleAnswers: Boolean = false,
@Transient
override val closeInfo: ScheduledCloseInfo? = null,
@SerialName(openPeriodField)
override val openPeriod: LongSeconds?= null,
@SerialName(closeDateField)
override val closeDate: LongSeconds?,
@SerialName(disableNotificationField)
override val disableNotification: Boolean = false,
@SerialName(replyToMessageIdField)
@@ -178,20 +197,39 @@ data class SendRegularPoll(
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
@SerialName(openPeriodField)
override val openPeriod: LongSeconds?
= (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.millisecondsLong ?.div(1000)
@SerialName(closeDateField)
override val closeDate: LongSeconds?
= (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000)
init {
checkPollInfo(question, options)
closeInfo ?.checkSendData()
}
}
fun SendRegularPoll(
chatId: ChatIdentifier,
question: String,
options: List<String>,
isAnonymous: Boolean = true,
isClosed: Boolean = false,
allowMultipleAnswers: Boolean = false,
closeInfo: ScheduledCloseInfo? = null,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null,
replyMarkup: KeyboardMarkup? = null
) = SendRegularPoll(
chatId,
question,
options,
isAnonymous,
isClosed,
allowMultipleAnswers,
(closeInfo as? ApproximateScheduledCloseInfo) ?.openPeriod,
(closeInfo as? ExactScheduledCloseInfo) ?.closeDate,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
)
fun SendQuizPoll(
chatId: ChatIdentifier,
question: String,
@@ -253,6 +291,39 @@ fun SendQuizPoll(
replyMarkup
)
internal fun SendQuizPoll(
chatId: ChatIdentifier,
question: String,
options: List<String>,
correctOptionId: Int,
isAnonymous: Boolean = true,
isClosed: Boolean = false,
explanation: String? = null,
parseMode: ParseMode? = null,
rawEntities: List<RawMessageEntity>? = null,
closeInfo: ScheduledCloseInfo? = null,
disableNotification: Boolean = false,
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null,
replyMarkup: KeyboardMarkup? = null
) = SendQuizPoll(
chatId,
question,
options,
correctOptionId,
isAnonymous,
isClosed,
explanation,
parseMode,
rawEntities,
(closeInfo as? ApproximateScheduledCloseInfo) ?.openPeriod,
(closeInfo as? ExactScheduledCloseInfo) ?.closeDate,
disableNotification,
replyToMessageId,
allowSendingWithoutReply,
replyMarkup
)
@Serializable
data class SendQuizPoll internal constructor(
@SerialName(chatIdField)
@@ -273,8 +344,10 @@ data class SendQuizPoll internal constructor(
override val parseMode: ParseMode? = null,
@SerialName(explanationEntitiesField)
private val rawEntities: List<RawMessageEntity>? = null,
@Transient
override val closeInfo: ScheduledCloseInfo? = null,
@SerialName(openPeriodField)
override val openPeriod: LongSeconds? = null,
@SerialName(closeDateField)
override val closeDate: LongSeconds? = null,
@SerialName(disableNotificationField)
override val disableNotification: Boolean = false,
@SerialName(replyToMessageIdField)
@@ -291,14 +364,6 @@ data class SendQuizPoll internal constructor(
rawEntities ?.asTextParts(explanation ?: return@lazy null) ?.justTextSources()
}
@SerialName(openPeriodField)
override val openPeriod: LongSeconds?
= (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.millisecondsLong ?.div(1000)
@SerialName(closeDateField)
override val closeDate: LongSeconds?
= (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000)
init {
checkPollInfo(question, options)
closeInfo ?.checkSendData()

View File

@@ -123,7 +123,7 @@ const val callbackQueryIdField = "callback_query_id"
const val inlineQueryIdField = "inline_query_id"
const val inlineKeyboardField = "inline_keyboard"
const val showAlertField = "show_alert"
const val cachedTimeField = "cached_time"
const val cacheTimeField = "cache_time"
const val foursquareIdField = "foursquare_id"
const val foursquareTypeField = "foursquare_type"
const val googlePlaceIdField = "google_place_id"
@@ -357,3 +357,39 @@ const val forceField = "force"
const val regularPollType = "regular"
const val quizPollType = "quiz"
const val dataField = "data"
const val credentialsField = "credentials"
const val hashField = "hash"
const val translationField = "translation"
const val translationFileField = "translation_file"
const val fileField = "file"
const val filesField = "files"
const val translationFilesField = "translation_files"
const val frontSideField = "front_side"
const val reverseSideField = "reverse_side"
const val selfieField = "selfie"
const val secretField = "secret"
const val errorsField = "errors"
const val sourceField = "source"
const val fieldNameField = "field_name"
const val dataHashField = "data_hash"
const val fileHashField = "file_hash"
const val fileHashesField = "file_hashes"
const val messageField = "message"
const val unspecifiedField = "unspecified"
const val secureDataField = "secure_data"
const val nonceField = "nonce"
const val personalDetailsField = "personal_details"
const val passportField = "passport"
const val internalPassportField = "internal_passport"
const val driverLicenseField = "driver_license"
const val identityCardField = "identity_card"
const val utilityBillField = "utility_bill"
const val bankStatementField = "bank_statement"
const val rentalAgreementField = "rental_agreement"
const val passportRegistrationField = "passport_registration"
const val temporaryRegistrationField = "temporary_registration"

View File

@@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.types.FileUniqueId
internal const val fileIdField = "file_id"
internal const val fileSizeField = "file_size"
internal const val fileDateField = "file_date"
internal const val filePathField = "file_path"
/**

View File

@@ -9,14 +9,14 @@ import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
data class ChannelMediaGroupMessage(
data class ChannelMediaGroupMessage<T : MediaGroupContent>(
override val messageId: MessageIdentifier,
override val chat: Chat,
override val date: DateTime,
override val mediaGroupId: MediaGroupIdentifier,
override val content: MediaGroupContent,
override val content: T,
override val editDate: DateTime?,
override val forwardInfo: ForwardInfo?,
override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage
) : MediaGroupMessage<T>

View File

@@ -4,11 +4,11 @@ import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat
import dev.inmo.tgbotapi.types.message.abstracts.ChannelMessage
import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
data class ChannelMessageImpl<T: MessageContent>(
data class ChannelContentMessageImpl<T: MessageContent>(
override val messageId: MessageIdentifier,
override val chat: ChannelChat,
override val content: T,
@@ -19,4 +19,6 @@ data class ChannelMessageImpl<T: MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?,
override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature?
) : ChannelMessage<T>
) : ChannelContentMessage<T>
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("ChannelContentMessageImpl", "dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl"))
typealias ChannelMessageImpl<T> = ChannelContentMessageImpl<T>

View File

@@ -7,15 +7,15 @@ import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
data class CommonMediaGroupMessage(
data class CommonMediaGroupMessage<T : MediaGroupContent>(
override val messageId: MessageIdentifier,
override val user: User,
override val chat: Chat,
override val date: DateTime,
override val mediaGroupId: MediaGroupIdentifier,
override val content: MediaGroupContent,
override val content: T,
override val editDate: DateTime?,
override val forwardInfo: ForwardInfo?,
override val replyTo: Message?,
override val replyMarkup: InlineKeyboardMarkup?
) : MediaGroupMessage, FromUserMessage
) : MediaGroupMessage<T>, FromUserMessage

View File

@@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.chat.abstracts.GroupChat
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
data class FromChannelGroupMessageImpl<T : MessageContent>(
data class FromChannelGroupContentMessageImpl<T : MessageContent>(
override val chat: GroupChat,
override val channel: ChannelChat,
override val messageId: MessageIdentifier,
@@ -20,9 +20,11 @@ data class FromChannelGroupMessageImpl<T : MessageContent>(
override val content: T,
override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature?
) : FromChannelGroupMessage<T>
) : FromChannelGroupContentMessage<T>
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("FromChannelGroupContentMessageImpl", "dev.inmo.tgbotapi.types.message.FromChannelGroupContentMessageImpl"))
typealias FromChannelGroupMessageImpl<T> = FromChannelGroupContentMessageImpl<T>
data class AnonymousGroupMessageImpl<T : MessageContent>(
data class AnonymousGroupContentMessageImpl<T : MessageContent>(
override val chat: GroupChat,
override val messageId: MessageIdentifier,
override val date: DateTime,
@@ -33,9 +35,11 @@ data class AnonymousGroupMessageImpl<T : MessageContent>(
override val content: T,
override val senderBot: CommonBot?,
override val authorSignature: AuthorSignature?
) : AnonymousGroupMessage<T>
) : AnonymousGroupContentMessage<T>
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("AnonymousGroupContentMessageImpl", "dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl"))
typealias AnonymousGroupMessageImpl<T> = AnonymousGroupContentMessageImpl<T>
data class CommonGroupMessageImpl<T : MessageContent>(
data class CommonGroupContentMessageImpl<T : MessageContent>(
override val chat: GroupChat,
override val messageId: MessageIdentifier,
override val user: User,
@@ -46,4 +50,6 @@ data class CommonGroupMessageImpl<T : MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?,
override val content: T,
override val senderBot: CommonBot?
) : CommonGroupMessage<T>
) : CommonGroupContentMessage<T>
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("CommonGroupContentMessageImpl", "dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl"))
typealias CommonGroupMessageImpl<T> = CommonGroupContentMessageImpl<T>

View File

@@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.types.message
import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.types.MessageIdentifier
import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.passport.PassportData
data class PassportMessage(
override val messageId: MessageIdentifier,
override val chat: Chat,
override val user: User,
override val date: DateTime,
val passportData: PassportData
) : Message, FromUserMessage

View File

@@ -4,12 +4,11 @@ import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.abstracts.PrivateMessage
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentInfo
data class PrivateMessageImpl<T: MessageContent>(
data class PrivateContentMessageImpl<T: MessageContent>(
override val messageId: MessageIdentifier,
override val user: User,
override val chat: Chat,
@@ -21,4 +20,6 @@ data class PrivateMessageImpl<T: MessageContent>(
override val replyMarkup: InlineKeyboardMarkup?,
override val senderBot: CommonBot?,
val paymentInfo: SuccessfulPaymentInfo?
) : PrivateMessage<T>
) : PrivateContentMessage<T>
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("PrivateContentMessageImpl", "dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl"))
typealias PrivateMessageImpl<T> = PrivateContentMessageImpl<T>

View File

@@ -18,6 +18,7 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.content.media.*
import dev.inmo.tgbotapi.types.message.payments.InvoiceContent
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentInfo
import dev.inmo.tgbotapi.types.passport.PassportData
import dev.inmo.tgbotapi.types.payments.Invoice
import dev.inmo.tgbotapi.types.payments.SuccessfulPayment
import dev.inmo.tgbotapi.types.polls.Poll
@@ -84,7 +85,7 @@ internal data class RawMessage(
private val connected_website: String? = null,
// passport property
private val passport_data: Unit? = null,
private val passport_data: PassportData? = null,
private val proximity_alert_triggered: ProximityAlertTriggered? = null,
private val reply_markup: InlineKeyboardMarkup? = null
@@ -255,7 +256,7 @@ internal data class RawMessage(
}
} ?: when (chat) {
is PublicChat -> when (chat) {
is ChannelChat -> ChannelMessageImpl(
is ChannelChat -> ChannelContentMessageImpl(
messageId,
chat,
content,
@@ -268,7 +269,7 @@ internal data class RawMessage(
author_signature
)
is GroupChat -> when (sender_chat) {
is ChannelChat -> FromChannelGroupMessageImpl(
is ChannelChat -> FromChannelGroupContentMessageImpl(
chat,
sender_chat,
messageId,
@@ -281,7 +282,7 @@ internal data class RawMessage(
via_bot,
author_signature
)
is GroupChat -> AnonymousGroupMessageImpl(
is GroupChat -> AnonymousGroupContentMessageImpl(
chat,
messageId,
date.asDate,
@@ -293,7 +294,7 @@ internal data class RawMessage(
via_bot,
author_signature
)
null -> CommonGroupMessageImpl(
null -> CommonGroupContentMessageImpl(
chat,
messageId,
from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
@@ -309,7 +310,7 @@ internal data class RawMessage(
}
else -> error("Unknown type of public chat: $chat")
}
is PrivateChat -> PrivateMessageImpl(
is PrivateChat -> PrivateContentMessageImpl(
messageId,
from ?: error("Was detected common message, but owner (sender) of the message was not found"),
chat,
@@ -324,6 +325,14 @@ internal data class RawMessage(
)
else -> error("Unknown type of chat: $chat")
}
} ?: passport_data ?.let{
PassportMessage(
messageId,
chat,
from ?: error("For passport must be provided user, but got null"),
date.asDate,
passport_data
)
} ?: error("Was not found supported type of data")
} catch (e: Exception) {
UnknownMessageType(

View File

@@ -4,8 +4,10 @@ import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage
interface ChannelMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, SignedMessage, WithSenderChatMessage {
interface ChannelContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, SignedMessage, WithSenderChatMessage {
override val chat: ChannelChat
override val senderChat: ChannelChat
get() = chat
}
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("ChannelContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage"))
typealias ChannelMessage<T> = ChannelContentMessage<T>

View File

@@ -4,17 +4,28 @@ import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat
import dev.inmo.tgbotapi.types.chat.abstracts.GroupChat
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
interface GroupMessage<T : MessageContent> : PublicMessage<T> {
interface GroupContentMessage<T : MessageContent> : PublicContentMessage<T> {
override val chat: GroupChat
}
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("GroupContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.GroupContentMessage"))
typealias GroupMessage<T> = GroupContentMessage<T>
interface FromChannelGroupMessage<T : MessageContent> : GroupMessage<T>, SignedMessage, WithSenderChatMessage {
interface FromChannelGroupContentMessage<T : MessageContent> : GroupContentMessage<T>, SignedMessage, WithSenderChatMessage {
val channel: ChannelChat
override val senderChat: ChannelChat
get() = channel
}
interface AnonymousGroupMessage<T : MessageContent> : GroupMessage<T>, SignedMessage, WithSenderChatMessage {
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("FromChannelGroupContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.FromChannelGroupContentMessage"))
typealias FromChannelGroupMessage<T> = FromChannelGroupContentMessage<T>
interface AnonymousGroupContentMessage<T : MessageContent> : GroupContentMessage<T>, SignedMessage, WithSenderChatMessage {
override val senderChat: GroupChat
get() = chat
}
interface CommonGroupMessage<T : MessageContent> : GroupMessage<T>, FromUserMessage
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("AnonymousGroupContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.AnonymousGroupContentMessage"))
typealias AnonymousGroupMessage<T> = AnonymousGroupContentMessage<T>
interface CommonGroupContentMessage<T : MessageContent> : GroupContentMessage<T>, FromUserMessage
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("CommonGroupContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage"))
typealias CommonGroupMessage<T> = CommonGroupContentMessage<T>

View File

@@ -3,6 +3,6 @@ package dev.inmo.tgbotapi.types.message.abstracts
import dev.inmo.tgbotapi.types.MediaGroupIdentifier
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
interface MediaGroupMessage : CommonMessage<MediaGroupContent> {
interface MediaGroupMessage<T : MediaGroupContent> : CommonMessage<T> {
val mediaGroupId: MediaGroupIdentifier
}

View File

@@ -3,4 +3,6 @@ package dev.inmo.tgbotapi.types.message.abstracts
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage
interface PrivateMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, FromUserMessage
interface PrivateContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, FromUserMessage
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("PrivateContentMessage", "dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage"))
typealias PrivateMessage<T> = PrivateContentMessage<T>

View File

@@ -4,6 +4,8 @@ import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage
interface PublicMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T> {
interface PublicContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T> {
override val chat: PublicChat
}
@Deprecated("Renamed due to ambiguity of naming", ReplaceWith("PublicContentMessage", "dev.inmo.tgbotapi.types.message.PublicContentMessage"))
typealias PublicMessage<T> = PublicContentMessage<T>

View File

@@ -1,12 +1,18 @@
package dev.inmo.tgbotapi.types.message.content.abstracts
import dev.inmo.tgbotapi.CommonAbstracts.CaptionedInput
import dev.inmo.tgbotapi.types.InputMedia.MediaGroupMemberInputMedia
import dev.inmo.tgbotapi.types.InputMedia.*
interface MediaGroupContent : MediaContent, CaptionedInput {
fun toMediaGroupMemberInputMedia(): MediaGroupMemberInputMedia
}
interface VisualMediaGroupContent : MediaGroupContent
interface AudioMediaGroupContent : MediaGroupContent
interface DocumentMediaGroupContent : MediaGroupContent
interface VisualMediaGroupContent : MediaGroupContent {
override fun toMediaGroupMemberInputMedia(): VisualMediaGroupMemberInputMedia
}
interface AudioMediaGroupContent : MediaGroupContent {
override fun toMediaGroupMemberInputMedia(): AudioMediaGroupMemberInputMedia
}
interface DocumentMediaGroupContent : MediaGroupContent {
override fun toMediaGroupMemberInputMedia(): DocumentMediaGroupMemberInputMedia
}

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.passport
import dev.inmo.tgbotapi.types.credentialsField
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedCredentials
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class PassportData(
@SerialName(dataField)
val data: List<EncryptedPassportElement>,
@SerialName(credentialsField)
val credentials: EncryptedCredentials
)

View File

@@ -0,0 +1,266 @@
@file:Suppress("unused", "EXPERIMENTAL_API_USAGE")
package dev.inmo.tgbotapi.types.passport
import dev.inmo.micro_utils.crypto.MD5
import dev.inmo.micro_utils.crypto.md5
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.*
import dev.inmo.tgbotapi.types.passport.encrypted.type
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
val ByteArray.passportFileHash: MD5
get() = md5()
@Serializable(PassportElementErrorSerializer::class)
sealed class PassportElementError {
abstract val source: String
abstract val type: String
abstract val message: String
}
data class UnknownPassportElementError(
val raw: JsonObject
) : PassportElementError() {
override val source: String = raw[sourceField] ?.jsonPrimitive ?.contentOrNull ?: ""
override val type: String = raw[typeField] ?.jsonPrimitive ?.contentOrNull ?: ""
override val message: String = raw[messageField] ?.jsonPrimitive ?.contentOrNull ?: ""
}
object PassportElementErrorSerializer : KSerializer<PassportElementError> {
private val jsonObjectSerializer = JsonObject.serializer()
override val descriptor: SerialDescriptor
get() = jsonObjectSerializer.descriptor
override fun deserialize(decoder: Decoder): PassportElementError {
val json = jsonObjectSerializer.deserialize(decoder)
return when (json[sourceField] ?.jsonPrimitive ?.content) {
"dataField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorDataField.serializer(), json)
"frontSideField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorFrontSide.serializer(), json)
"reverseSideField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorReverseSide.serializer(), json)
"selfieField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorSelfie.serializer(), json)
"fileField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementFileError.serializer(), json)
"filesField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementFilesError.serializer(), json)
"translationFileField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorTranslationFile.serializer(), json)
"translationFilesField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorTranslationFiles.serializer(), json)
"unspecifiedField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorUnspecified.serializer(), json)
else -> UnknownPassportElementError(json)
}
}
override fun serialize(encoder: Encoder, value: PassportElementError) {
val neverMindAboutThisVariable = when (value) {
is PassportElementErrorFrontSide -> PassportElementErrorFrontSide.serializer().serialize(encoder, value)
is PassportElementErrorReverseSide -> PassportElementErrorReverseSide.serializer().serialize(encoder, value)
is PassportElementErrorSelfie -> PassportElementErrorSelfie.serializer().serialize(encoder, value)
is PassportElementErrorFile -> PassportElementErrorFile.serializer().serialize(encoder, value)
is PassportElementErrorTranslationFile -> PassportElementErrorTranslationFile.serializer().serialize(encoder, value)
is PassportElementErrorUnspecified -> PassportElementErrorUnspecified.serializer().serialize(encoder, value)
is PassportElementErrorDataField -> PassportElementErrorDataField.serializer().serialize(encoder, value)
is PassportElementErrorFiles -> PassportElementErrorFiles.serializer().serialize(encoder, value)
is PassportElementErrorTranslationFiles -> PassportElementErrorTranslationFiles.serializer().serialize(encoder, value)
is UnknownPassportElementError -> jsonObjectSerializer.serialize(encoder, value.raw)
}
}
}
@Serializable
sealed class PassportSingleElementError : PassportElementError() {
abstract val elementHash: PassportElementHash
}
@Serializable
sealed class PassportMultipleElementsError : PassportElementError() {
abstract val elementsHashes: List<PassportElementHash>
}
@Serializable
sealed class PassportElementFileError : PassportSingleElementError()
@Serializable
sealed class PassportElementFilesError : PassportMultipleElementsError()
@Serializable
data class PassportElementErrorDataField(
@SerialName(typeField)
override val type: String,
@SerialName(fieldNameField)
val fieldName: String,
@SerialName(dataHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportSingleElementError() {
@SerialName(sourceField)
@Required
override val source: String = dataField
}
fun EncryptedPassportElementWithData.createDataError(field: String, message: String) = PassportElementErrorDataField(
type,
field,
hash,
message
)
@Serializable
data class PassportElementErrorFrontSide(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = frontSideField
}
fun EncryptedPassportElementWithFrontSide.createFrontSideError(message: String, unencryptedFileHash: PassportElementHash) = PassportElementErrorFrontSide(
type,
unencryptedFileHash,
message
)
@Serializable
data class PassportElementErrorReverseSide(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = reverseSideField
}
fun EncryptedPassportElementWithReverseSide.createReverseSideError(message: String, unencryptedFileHash: PassportElementHash) = PassportElementErrorReverseSide(
type,
unencryptedFileHash,
message
)
@Serializable
data class PassportElementErrorSelfie(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = selfieField
}
fun EncryptedPassportElementWithSelfie.createSelfieError(message: String, unencryptedFileHash: PassportElementHash) = PassportElementErrorSelfie(
type,
unencryptedFileHash,
message
)
@Serializable
data class PassportElementErrorFile(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = fileField
}
fun EncryptedPassportElementWithFilesCollection.createFileError(message: String, unencryptedFileHash: PassportElementHash) = PassportElementErrorFile(
type,
unencryptedFileHash,
message
)
@Serializable
data class PassportElementErrorFiles(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashesField)
override val elementsHashes: List<@Serializable(Base64BytesToFromStringSerializer::class) PassportElementHash>,
@SerialName(messageField)
override val message: String
) : PassportElementFilesError() {
@SerialName(sourceField)
@Required
override val source: String = filesField
}
fun EncryptedPassportElementWithFilesCollection.createFilesError(message: String, unencryptedFileHashes: List<PassportElementHash>) = PassportElementErrorFiles(
type,
unencryptedFileHashes,
message
)
@Serializable
data class PassportElementErrorTranslationFile(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = translationFileField
}
fun EncryptedPassportElementTranslatable.createFileError(message: String, unencryptedFileHash: PassportElementHash) = PassportElementErrorTranslationFile(
type,
unencryptedFileHash,
message
)
@Serializable
data class PassportElementErrorTranslationFiles(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashesField)
override val elementsHashes: List<@Serializable(Base64BytesToFromStringSerializer::class) PassportElementHash>,
@SerialName(messageField)
override val message: String
) : PassportElementFilesError() {
@SerialName(sourceField)
@Required
override val source: String = translationFilesField
}
fun EncryptedPassportElementTranslatable.createFilesError(message: String, unencryptedFileHashes: List<PassportElementHash>) = PassportElementErrorTranslationFiles(
type,
unencryptedFileHashes,
message
)
@Serializable
data class PassportElementErrorUnspecified(
@SerialName(typeField)
override val type: String,
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val elementHash: PassportElementHash,
@SerialName(messageField)
override val message: String
) : PassportElementFileError() {
@SerialName(sourceField)
@Required
override val source: String = unspecifiedField
}
fun EncryptedPassportElement.createUnspecifiedError(message: String, elementHash: PassportElementHash) = PassportElementErrorUnspecified(
type,
elementHash,
message
)

View File

@@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.types.passport.credentials
import dev.inmo.tgbotapi.types.nonceField
import dev.inmo.tgbotapi.types.passport.decrypted.SecureData
import dev.inmo.tgbotapi.types.secureDataField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class DecryptedCredentials(
@SerialName(secureDataField)
val secureData: SecureData,
@SerialName(nonceField)
val nonce: String
)

View File

@@ -0,0 +1,23 @@
package dev.inmo.tgbotapi.types.passport.credentials
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
typealias EncryptedByBotPublicKeyData = SourceBytes
typealias EncryptedData = SourceBytes
@Serializable
data class EncryptedCredentials(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
val data: EncryptedData,
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
val hash: SourceBytes,
@SerialName(secretField)
@Serializable(Base64BytesToFromStringSerializer::class)
val secret: EncryptedByBotPublicKeyData
)

View File

@@ -0,0 +1,35 @@
package dev.inmo.tgbotapi.types.passport.credentials
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
sealed class EndDataCredentials {
@Serializable(Base64BytesToFromStringSerializer::class)
abstract val hash: SourceBytes
@Serializable(Base64BytesToFromStringSerializer::class)
abstract val secret: SourceBytes
}
@Serializable
data class DataCredentials(
@SerialName(dataHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: SourceBytes,
@SerialName(secretField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val secret: SourceBytes
) : EndDataCredentials()
@Serializable
data class FileCredentials(
@SerialName(fileHashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: SourceBytes,
@SerialName(secretField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val secret: SourceBytes
) : EndDataCredentials()

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.credentials.DataCredentials
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithData
import kotlinx.serialization.*
@Serializable
data class AddressSecureValue(
@SerialName(dataField)
override val data: DataCredentials
) : SecureValueWithData {
@Transient
override val credentials: List<EndDataCredentials> = listOf(data)
}

View File

@@ -0,0 +1,41 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.credentials.*
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
sealed class IdentityWithReverseSideSecureValue : SecureValueIdentity, SecureValueWithData, SecureValueWithTranslations, SecureValueWithReverseSide {
override val credentials: List<EndDataCredentials>
get() = listOfNotNull(data, frontSide, reverseSide, selfie) + translation
}
@Serializable
data class DriverLicenseSecureValue(
@SerialName(dataField)
override val data: DataCredentials? = null,
@SerialName(frontSideField)
override val frontSide: FileCredentials? = null,
@SerialName(reverseSideField)
override val reverseSide: FileCredentials? = null,
@SerialName(selfieField)
override val selfie: FileCredentials? = null,
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList()
) : IdentityWithReverseSideSecureValue()
@Serializable
data class IdentityCardSecureValue(
@SerialName(dataField)
override val data: DataCredentials? = null,
@SerialName(frontSideField)
override val frontSide: FileCredentials? = null,
@SerialName(reverseSideField)
override val reverseSide: FileCredentials? = null,
@SerialName(selfieField)
override val selfie: FileCredentials? = null,
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList()
) : IdentityWithReverseSideSecureValue()

View File

@@ -0,0 +1,56 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.filesField
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
import dev.inmo.tgbotapi.types.passport.credentials.FileCredentials
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithFiles
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithTranslations
import dev.inmo.tgbotapi.types.translationField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
sealed class OtherDocumentsSecureValue : SecureValueWithTranslations, SecureValueWithFiles {
override val credentials: List<EndDataCredentials>
get() = translation + files
}
@Serializable
data class UtilityBillSecureValue(
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList(),
@SerialName(filesField)
override val files: List<FileCredentials> = emptyList()
) : OtherDocumentsSecureValue()
@Serializable
data class BankStatementSecureValue(
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList(),
@SerialName(filesField)
override val files: List<FileCredentials> = emptyList()
) : OtherDocumentsSecureValue()
@Serializable
data class RentalAgreementSecureValue(
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList(),
@SerialName(filesField)
override val files: List<FileCredentials> = emptyList()
) : OtherDocumentsSecureValue()
@Serializable
data class PassportRegistrationSecureValue(
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList(),
@SerialName(filesField)
override val files: List<FileCredentials> = emptyList()
) : OtherDocumentsSecureValue()
@Serializable
data class TemporalRegistrationSecureValue(
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList(),
@SerialName(filesField)
override val files: List<FileCredentials> = emptyList()
) : OtherDocumentsSecureValue()

View File

@@ -0,0 +1,38 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.credentials.*
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
sealed class PassportSecureValue : SecureValueIdentity, SecureValueWithData, SecureValueWithTranslations {
override val credentials: List<EndDataCredentials>
get() = listOfNotNull(data, frontSide, selfie) + translation
}
@Serializable
data class CommonPassportSecureValue(
@SerialName(dataField)
override val data: DataCredentials? = null,
@SerialName(frontSideField)
override val frontSide: FileCredentials? = null,
@SerialName(selfieField)
override val selfie: FileCredentials? = null,
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList()
) : PassportSecureValue()
@Serializable
data class InternalPassportSecureValue(
@SerialName(dataField)
override val data: DataCredentials? = null,
@SerialName(frontSideField)
override val frontSide: FileCredentials? = null,
@SerialName(selfieField)
override val selfie: FileCredentials? = null,
@SerialName(translationField)
override val translation: List<FileCredentials> = emptyList()
) : PassportSecureValue()

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.credentials.DataCredentials
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithData
import kotlinx.serialization.*
@Serializable
data class PersonalDetailsSecureValue(
@SerialName(dataField)
override val data: DataCredentials
) : SecureValueWithData {
@Transient
override val credentials: List<EndDataCredentials> = listOf(data)
}

View File

@@ -0,0 +1,42 @@
package dev.inmo.tgbotapi.types.passport.decrypted
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SecureData(
@SerialName(personalDetailsField)
val personalDetails: PersonalDetailsSecureValue? = null,
@SerialName(passportField)
val passport: CommonPassportSecureValue? = null,
@SerialName(internalPassportField)
val internalPassport: InternalPassportSecureValue? = null,
@SerialName(driverLicenseField)
val driverLicense: DriverLicenseSecureValue? = null,
@SerialName(identityCardField)
val identityCard: IdentityCardSecureValue? = null,
@SerialName(utilityBillField)
val utilityBill: UtilityBillSecureValue? = null,
@SerialName(bankStatementField)
val bankStatement: BankStatementSecureValue? = null,
@SerialName(rentalAgreementField)
val rentalAgreement: RentalAgreementSecureValue? = null,
@SerialName(passportRegistrationField)
val passportRegistration: PassportRegistrationSecureValue? = null,
@SerialName(temporaryRegistrationField)
val temporaryRegistration: TemporalRegistrationSecureValue? = null,
) {
val allCredentials by lazy {
(personalDetails ?.credentials ?: emptyList()) +
(passport ?.credentials ?: emptyList()) +
(internalPassport ?.credentials ?: emptyList()) +
(driverLicense ?.credentials ?: emptyList()) +
(identityCard ?.credentials ?: emptyList()) +
(utilityBill ?.credentials ?: emptyList()) +
(bankStatement ?.credentials ?: emptyList()) +
(rentalAgreement ?.credentials ?: emptyList()) +
(passportRegistration ?.credentials ?: emptyList()) +
(temporaryRegistration ?.credentials ?: emptyList())
}
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
interface SecureValue {
val credentials: List<EndDataCredentials>
}

View File

@@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.FileCredentials
interface SecureValueIdentity : SecureValue {
val frontSide: FileCredentials?
val selfie: FileCredentials?
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.DataCredentials
interface SecureValueWithData : SecureValue {
val data: DataCredentials?
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.FileCredentials
interface SecureValueWithFiles : SecureValue {
val files: List<FileCredentials>
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.FileCredentials
interface SecureValueWithReverseSide : SecureValue {
val reverseSide: FileCredentials?
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.FileCredentials
interface SecureValueWithTranslations : SecureValue {
val translation: List<FileCredentials>
}

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.emailField
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.PassportElementHash
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElementWithEmail
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class Email(
@SerialName(emailField)
override val email: String,
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithEmail

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.PassportElementHash
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElementWithData
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class EncryptedAddress(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithData

View File

@@ -0,0 +1,64 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.crypto.decodeBase64
import dev.inmo.micro_utils.serialization.encapsulator.Encapsulator
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.UnknownEncryptedPassportElement
import dev.inmo.tgbotapi.types.typeField
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
val encryptedElementsClassesByTypes = mapOf(
"personal_details" to Encapsulator(EncryptedPersonalDetails::class, EncryptedPersonalDetails.serializer()),
"passport" to Encapsulator(CommonPassport::class, CommonPassport.serializer()),
"driver_license" to Encapsulator(DriverLicense::class, DriverLicense.serializer()),
"identity_card" to Encapsulator(IdentityCard::class, IdentityCard.serializer()),
"internal_passport" to Encapsulator(InternalPassport::class, InternalPassport.serializer()),
"address" to Encapsulator(EncryptedAddress::class, EncryptedAddress.serializer()),
"utility_bill" to Encapsulator(UtilityBill::class, UtilityBill.serializer()),
"bank_statement" to Encapsulator(BankStatement::class, BankStatement.serializer()),
"rental_agreement" to Encapsulator(RentalAgreement::class, RentalAgreement.serializer()),
"passport_registration" to Encapsulator(PassportRegistration::class, PassportRegistration.serializer()),
"temporary_registration" to Encapsulator(TemporaryRegistration::class, TemporaryRegistration.serializer()),
"phone_number" to Encapsulator(PhoneNumber::class, PhoneNumber.serializer()),
"email" to Encapsulator(Email::class, Email.serializer())
)
@RiskFeature("Remember that this method may return \"unknown\" in case if encrypted element was not defined in library")
val EncryptedPassportElement.type: String
get() = encryptedElementsClassesByTypes.keys.firstOrNull { encryptedElementsClassesByTypes.getValue(it).klass.isInstance(this) } ?: "unknown"
@Serializer(EncryptedPassportElement::class)
object EncryptedElementSerializer : KSerializer<EncryptedPassportElement> {
private val jsonSerializer = JsonObject.serializer()
override val descriptor: SerialDescriptor = jsonSerializer.descriptor
override fun deserialize(decoder: Decoder): EncryptedPassportElement {
val json = jsonSerializer.deserialize(decoder)
return json[typeField] ?.jsonPrimitive ?.content ?.let { type ->
encryptedElementsClassesByTypes[type] ?.serializer ?.let { deserializer ->
nonstrictJsonFormat.decodeFromJsonElement(deserializer, json)
}
} ?: UnknownEncryptedPassportElement(json, json[hashField] ?.jsonPrimitive ?.content ?.decodeBase64() ?: byteArrayOf())
}
override fun serialize(encoder: Encoder, value: EncryptedPassportElement) {
val json = value.let {
encryptedElementsClassesByTypes.forEach { (key, encapsulator) ->
val json = encapsulator.encapsulate(value) { data ->
nonstrictJsonFormat.encodeToJsonElement(this as KSerializer<EncryptedPassportElement>, data).jsonObject
} ?: return@forEach
return@let JsonObject(json + (typeField to JsonPrimitive(key)))
}
(value as? UnknownEncryptedPassportElement) ?.rawJson ?: return
}
jsonSerializer.serialize(encoder, json)
}
}

View File

@@ -0,0 +1,62 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class EncryptedPassportElementWithTranslatableFilesCollection : EncryptedPassportElementTranslatable, EncryptedPassportElementWithFilesCollection
@Serializable
data class UtilityBill(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableFilesCollection()
@Serializable
data class BankStatement(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableFilesCollection()
@Serializable
data class RentalAgreement(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableFilesCollection()
@Serializable
data class PassportRegistration(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableFilesCollection()
@Serializable
data class TemporaryRegistration(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableFilesCollection()

View File

@@ -0,0 +1,47 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class EncryptedPassportElementWithTranslatableIDDocument : EncryptedPassportElementWithData, EncryptedPassportElementWithFrontSide, EncryptedPassportElementWithReverseSide, EncryptedPassportElementWithSelfie, EncryptedPassportElementTranslatable
@Serializable
data class DriverLicense(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@SerialName(frontSideField)
override val frontSide: PassportFile? = null,
@SerialName(reverseSideField)
override val reverseSide: PassportFile? = null,
@SerialName(selfieField)
override val selfie: PassportFile? = null,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableIDDocument()
@Serializable
data class IdentityCard(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@SerialName(frontSideField)
override val frontSide: PassportFile? = null,
@SerialName(reverseSideField)
override val reverseSide: PassportFile? = null,
@SerialName(selfieField)
override val selfie: PassportFile? = null,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithTranslatableIDDocument()

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.PassportElementHash
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElementWithData
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class EncryptedPersonalDetails(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithData

View File

@@ -0,0 +1,42 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class Passport : EncryptedPassportElementWithData, EncryptedPassportElementWithFrontSide, EncryptedPassportElementWithSelfie, EncryptedPassportElementTranslatable
@Serializable
data class CommonPassport(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@SerialName(frontSideField)
override val frontSide: PassportFile? = null,
@SerialName(selfieField)
override val selfie: PassportFile? = null,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : Passport()
@Serializable
data class InternalPassport(
@SerialName(dataField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val data: EncryptedData,
@SerialName(frontSideField)
override val frontSide: PassportFile? = null,
@SerialName(selfieField)
override val selfie: PassportFile? = null,
@SerialName(translationField)
override val translations: List<PassportFile> = emptyList(),
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : Passport()

View File

@@ -0,0 +1,23 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* This object represents a file uploaded to Telegram Passport. Currently all Telegram Passport files are in JPEG format
* when decrypted and don't exceed 10MB.
*/
@Serializable
data class PassportFile(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(fileDateField)
val uploadingDate: TelegramDate,
@SerialName(fileSizeField)
override val fileSize: Long? = null
) : TelegramMediaFile

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.types.passport.encrypted
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.PassportElementHash
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElementWithPhoneNumber
import dev.inmo.tgbotapi.types.phoneNumberField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class PhoneNumber(
@SerialName(phoneNumberField)
override val phoneNumber: String,
@SerialName(hashField)
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElementWithPhoneNumber

View File

@@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
typealias PassportElementHash = SourceBytes
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElement {
val hash: PassportElementHash
}
@Serializable(EncryptedElementSerializer::class)
data class UnknownEncryptedPassportElement(
val rawJson: JsonObject,
@Serializable(Base64BytesToFromStringSerializer::class)
override val hash: PassportElementHash
) : EncryptedPassportElement

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementTranslatable : EncryptedPassportElement {
val translations: List<PassportFile>
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithData : EncryptedPassportElement {
val data: EncryptedData
}

View File

@@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithEmail : EncryptedPassportElement {
val email: String
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithFilesCollection : EncryptedPassportElement {
val files: List<PassportFile>
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithFrontSide : EncryptedPassportElement {
val frontSide: PassportFile?
}

View File

@@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithPhoneNumber : EncryptedPassportElement {
val phoneNumber: String
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithReverseSide : EncryptedPassportElement {
val reverseSide: PassportFile?
}

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElementWithSelfie : EncryptedPassportElement {
val selfie: PassportFile?
}

View File

@@ -33,6 +33,9 @@ val LongSeconds.asApproximateScheduledCloseInfo
get() = ApproximateScheduledCloseInfo(
TimeSpan(this * 1000.0)
)
fun LongSeconds.asApproximateScheduledCloseInfo(startPoint: DateTime) = ApproximateScheduledCloseInfo(
TimeSpan(this * 1000.0), startPoint
)
val LongSeconds.asExactScheduledCloseInfo
get() = ExactScheduledCloseInfo(
DateTime(unixMillis = this * 1000.0)

View File

@@ -2,11 +2,12 @@ package dev.inmo.tgbotapi.types.update.MediaGroupUpdates
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
data class ChannelPostMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
override val data: List<MediaGroupMessage<MediaGroupContent>> = origins.mapNotNull { it.data as? MediaGroupMessage<MediaGroupContent> }
}

View File

@@ -2,11 +2,12 @@ package dev.inmo.tgbotapi.types.update.MediaGroupUpdates
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.update.EditChannelPostUpdate
data class EditChannelPostMediaGroupUpdate(
override val origin: EditChannelPostUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage = origin.data as MediaGroupMessage
override val data: MediaGroupMessage<MediaGroupContent> = origin.data as MediaGroupMessage<MediaGroupContent>
}

View File

@@ -2,11 +2,12 @@ package dev.inmo.tgbotapi.types.update.MediaGroupUpdates
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.update.EditMessageUpdate
data class EditMessageMediaGroupUpdate(
override val origin: EditMessageUpdate
) : EditMediaGroupUpdate {
override val updateId: UpdateIdentifier = origin.updateId
override val data: MediaGroupMessage = origin.data as MediaGroupMessage
override val data: MediaGroupMessage<MediaGroupContent> = origin.data as MediaGroupMessage<MediaGroupContent>
}

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.update.MediaGroupUpdates
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.update.abstracts.*
/**
@@ -13,11 +14,11 @@ import dev.inmo.tgbotapi.types.update.abstracts.*
interface MediaGroupUpdate : Update
interface SentMediaGroupUpdate: MediaGroupUpdate {
override val data: List<MediaGroupMessage>
override val data: List<MediaGroupMessage<MediaGroupContent>>
val origins: List<BaseMessageUpdate>
}
interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate {
override val data: MediaGroupMessage
override val data: MediaGroupMessage<MediaGroupContent>
val origin: BaseMessageUpdate
}

View File

@@ -2,11 +2,12 @@ package dev.inmo.tgbotapi.types.update.MediaGroupUpdates
import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
data class MessageMediaGroupUpdate(
override val origins: List<BaseMessageUpdate>
) : SentMediaGroupUpdate {
override val updateId: UpdateIdentifier = origins.last().updateId
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
override val data: List<MediaGroupMessage<MediaGroupContent>> = origins.mapNotNull { it.data as? MediaGroupMessage<MediaGroupContent> }
}

View File

@@ -7,15 +7,47 @@ import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.coroutines.flow.*
@Suppress("EXPERIMENTAL_API_USAGE", "unused")
class FlowsUpdatesFilter(
interface FlowsUpdatesFilter : UpdatesFilter {
override val allowedUpdates: List<String>
get() = ALL_UPDATES_LIST
val allUpdatesFlow: Flow<Update>
val allUpdatesWithoutMediaGroupsGroupingFlow: Flow<Update>
val messageFlow: Flow<MessageUpdate>
val messageMediaGroupFlow: Flow<MessageMediaGroupUpdate>
val editedMessageFlow: Flow<EditMessageUpdate>
val editedMessageMediaGroupFlow: Flow<EditMessageMediaGroupUpdate>
val channelPostFlow: Flow<ChannelPostUpdate>
val channelPostMediaGroupFlow: Flow<ChannelPostMediaGroupUpdate>
val editedChannelPostFlow: Flow<EditChannelPostUpdate>
val editedChannelPostMediaGroupFlow: Flow<EditChannelPostMediaGroupUpdate>
val chosenInlineResultFlow: Flow<ChosenInlineResultUpdate>
val inlineQueryFlow: Flow<InlineQueryUpdate>
val callbackQueryFlow: Flow<CallbackQueryUpdate>
val shippingQueryFlow: Flow<ShippingQueryUpdate>
val preCheckoutQueryFlow: Flow<PreCheckoutQueryUpdate>
val pollFlow: Flow<PollUpdate>
val pollAnswerFlow: Flow<PollAnswerUpdate>
val unknownUpdateTypeFlow: Flow<UnknownUpdate>
}
/**
* Creates [DefaultFlowsUpdatesFilter]
*/
@Suppress("FunctionName")
fun FlowsUpdatesFilter(
broadcastChannelsSize: Int = 100
): UpdatesFilter {
) = DefaultFlowsUpdatesFilter(broadcastChannelsSize)
@Suppress("EXPERIMENTAL_API_USAGE", "unused")
class DefaultFlowsUpdatesFilter(
broadcastChannelsSize: Int = 100
): FlowsUpdatesFilter {
private val updatesSharedFlow = MutableSharedFlow<Update>(extraBufferCapacity = broadcastChannelsSize)
@Suppress("MemberVisibilityCanBePrivate")
val allUpdatesFlow: Flow<Update> = updatesSharedFlow.asSharedFlow()
override val allUpdatesFlow: Flow<Update> = updatesSharedFlow.asSharedFlow()
@Suppress("MemberVisibilityCanBePrivate")
val allUpdatesWithoutMediaGroupsGroupingFlow: Flow<Update> = updatesSharedFlow.flatMapConcat {
override val allUpdatesWithoutMediaGroupsGroupingFlow: Flow<Update> = allUpdatesFlow.flatMapConcat {
when (it) {
is SentMediaGroupUpdate -> it.origins.asFlow()
is EditMediaGroupUpdate -> flowOf(it.origin)
@@ -23,26 +55,24 @@ class FlowsUpdatesFilter(
}
}
override val allowedUpdates: List<String>
get() = ALL_UPDATES_LIST
override val asUpdateReceiver: UpdateReceiver<Update> = {
updatesSharedFlow.emit(it)
}
val messageFlow: Flow<MessageUpdate> = allUpdatesFlow.filterIsInstance()
val messageMediaGroupFlow: Flow<MessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
val editedMessageFlow: Flow<EditMessageUpdate> = allUpdatesFlow.filterIsInstance()
val editedMessageMediaGroupFlow: Flow<EditMessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
val channelPostFlow: Flow<ChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
val channelPostMediaGroupFlow: Flow<ChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
val editedChannelPostFlow: Flow<EditChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
val editedChannelPostMediaGroupFlow: Flow<EditChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
val chosenInlineResultFlow: Flow<ChosenInlineResultUpdate> = allUpdatesFlow.filterIsInstance()
val inlineQueryFlow: Flow<InlineQueryUpdate> = allUpdatesFlow.filterIsInstance()
val callbackQueryFlow: Flow<CallbackQueryUpdate> = allUpdatesFlow.filterIsInstance()
val shippingQueryFlow: Flow<ShippingQueryUpdate> = allUpdatesFlow.filterIsInstance()
val preCheckoutQueryFlow: Flow<PreCheckoutQueryUpdate> = allUpdatesFlow.filterIsInstance()
val pollFlow: Flow<PollUpdate> = allUpdatesFlow.filterIsInstance()
val pollAnswerFlow: Flow<PollAnswerUpdate> = allUpdatesFlow.filterIsInstance()
val unknownUpdateTypeFlow: Flow<UnknownUpdate> = allUpdatesFlow.filterIsInstance()
override val messageFlow: Flow<MessageUpdate> = allUpdatesFlow.filterIsInstance()
override val messageMediaGroupFlow: Flow<MessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val editedMessageFlow: Flow<EditMessageUpdate> = allUpdatesFlow.filterIsInstance()
override val editedMessageMediaGroupFlow: Flow<EditMessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val channelPostFlow: Flow<ChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
override val channelPostMediaGroupFlow: Flow<ChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val editedChannelPostFlow: Flow<EditChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
override val editedChannelPostMediaGroupFlow: Flow<EditChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val chosenInlineResultFlow: Flow<ChosenInlineResultUpdate> = allUpdatesFlow.filterIsInstance()
override val inlineQueryFlow: Flow<InlineQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val callbackQueryFlow: Flow<CallbackQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val shippingQueryFlow: Flow<ShippingQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val preCheckoutQueryFlow: Flow<PreCheckoutQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val pollFlow: Flow<PollUpdate> = allUpdatesFlow.filterIsInstance()
override val pollAnswerFlow: Flow<PollAnswerUpdate> = allUpdatesFlow.filterIsInstance()
override val unknownUpdateTypeFlow: Flow<UnknownUpdate> = allUpdatesFlow.filterIsInstance()
}

View File

@@ -1,9 +1,10 @@
package dev.inmo.tgbotapi.utils.internal
import dev.inmo.tgbotapi.CommonAbstracts.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.ParseMode.*
import dev.inmo.tgbotapi.types.captionLength
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.textLength
internal fun createFormattedText(
entities: TextSourcesList,

View File

@@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.DownloadFile
import dev.inmo.tgbotapi.requests.get.GetFile
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedCredentials
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.json.JsonObject
interface Decryptor {
fun decrypt(data: EncryptedData): SourceBytes
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.types.passport
import dev.inmo.micro_utils.crypto.MD5
import java.io.File
val File.passportFileHash: MD5
get() = readBytes().passportFileHash

View File

@@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedData
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class AESDecryptor(key: SourceBytes, private val iv: ByteArray) : Decryptor {
private val key = SecretKeySpec(key, "AES");
override fun decrypt(data: EncryptedData): SourceBytes {
return Cipher.getInstance("AES/CBC/NOPADDING").run {
init(Cipher.DECRYPT_MODE, key, IvParameterSpec(this@AESDecryptor.iv))
val decryptedCredentials = doFinal(data)
val padding = decryptedCredentials.first()
decryptedCredentials.copyOfRange(padding.toInt(), decryptedCredentials.size)
}
}
}

View File

@@ -0,0 +1,29 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.decodeBase64
import dev.inmo.tgbotapi.types.passport.credentials.DecryptedCredentials
import dev.inmo.tgbotapi.types.passport.credentials.EncryptedCredentials
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import java.security.*
import java.security.spec.PKCS8EncodedKeySpec
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
private val regexToRemoveFromKey = Regex("(-----(BEGIN|END) ((?:.*? KEY)|CERTIFICATE)-----|[\\s])")
fun EncryptedCredentials.decryptWithPKCS8PrivateKey(privateKey: PrivateKey): DecryptedCredentials {
val decrypted = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding").run {
init(Cipher.DECRYPT_MODE, privateKey)
doFinal(secret)
}
val dataDecryptor = (decrypted to hash).createDecryptor()
val decryptedCredentials = dataDecryptor.decrypt(data).decodeToString()
return nonstrictJsonFormat.decodeFromString(DecryptedCredentials.serializer(), decryptedCredentials)
}
fun EncryptedCredentials.decryptWithPKCS8PrivateKey(key: String) = decryptWithPKCS8PrivateKey(
KeyFactory.getInstance("RSA").generatePrivate(
PKCS8EncodedKeySpec(key.replace(regexToRemoveFromKey, "").decodeBase64())
)
)

View File

@@ -0,0 +1,28 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.DownloadFile
import dev.inmo.tgbotapi.requests.get.GetFile
import dev.inmo.tgbotapi.types.passport.credentials.*
import dev.inmo.tgbotapi.types.passport.encrypted.PassportFile
fun EndDataCredentials.decryptData(
bytes: EncryptedData
): SourceBytes {
return createDecryptor().decrypt(bytes)
}
fun FileCredentials.decryptFile(
fileBytes: ByteArray
): SourceBytes {
return createDecryptor().decrypt(fileBytes)
}
suspend fun FileCredentials.decryptFile(
bot: TelegramBot,
passportFile: PassportFile
): SourceBytes {
val pathedFile = bot.execute(GetFile(passportFile.fileId))
val bytes = bot.execute(DownloadFile(pathedFile.filePath))
return decryptFile(bytes)
}

View File

@@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
import java.security.MessageDigest
fun Pair<SourceBytes, SourceBytes>.createDecryptor(): Decryptor {
val secretHash = MessageDigest.getInstance("SHA-512").digest(first + second)
val key = secretHash.copyOf(32)
val iv = secretHash.copyOfRange(32, 48)
return AESDecryptor(key, iv)
}
fun EndDataCredentials.createDecryptor() = (secret to hash).createDecryptor()

View File

@@ -0,0 +1,24 @@
package dev.inmo.tgbotapi.utils.passport
import dev.inmo.tgbotapi.types.passport.PassportData
import dev.inmo.tgbotapi.types.passport.decrypted.SecureData
import java.security.PrivateKey
inline fun <T> PassportData.doInDecryptionContextWithPKCS8Key(
pkcs8Key: PrivateKey,
expectedNonce: String? = null,
crossinline block: SecureData.() -> T
): T {
val decryptedCredentials = credentials.decryptWithPKCS8PrivateKey(pkcs8Key)
expectedNonce ?.let { require(expectedNonce == decryptedCredentials.nonce) }
return decryptedCredentials.secureData.run(block)
}
inline fun <T> PassportData.doInDecryptionContextWithPKCS8Key(
pkcs8Key: String,
expectedNonce: String? = null,
crossinline block: SecureData.() -> T
): T {
val decryptedCredentials = credentials.decryptWithPKCS8PrivateKey(pkcs8Key)
expectedNonce ?.let { require(expectedNonce == decryptedCredentials.nonce) }
return decryptedCredentials.secureData.run(block)
}

View File

@@ -29,7 +29,9 @@ repositories {
}
kotlin {
jvm()
jvm {
compilations.main.kotlinOptions.useIR = true
}
js(BOTH) {
browser()
nodejs()

View File

@@ -1 +1 @@
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Extensions for API","description":"API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference","url":"https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-api","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Extensions for API","description":"API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference","url":"https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-api","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","includeGpgSigning":true,"publishToMavenCentral":true,"developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}

View File

@@ -1,21 +1,10 @@
apply plugin: 'maven-publish'
apply plugin: 'signing'
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
@@ -59,7 +48,22 @@ publishing {
password = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
}
}
maven {
name = "sonatype"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
}
}
}
}
}
}
signing {
useGpgCmd()
sign publishing.publications
}

View File

@@ -23,7 +23,7 @@ internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>()
for (update in this) {
val data = (update.data as? MediaGroupMessage)
val data = (update.data as? MediaGroupMessage<*>)
if (data == null) {
resultUpdates.add(update)
continue

View File

@@ -0,0 +1,45 @@
package dev.inmo.tgbotapi.extensions.api.passport
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.SetPassportDataErrors
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.message.PassportMessage
import dev.inmo.tgbotapi.types.passport.PassportData
import dev.inmo.tgbotapi.types.passport.PassportElementError
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement
import dev.inmo.tgbotapi.utils.passport.Decryptor
suspend fun TelegramBot.setPassportDataErrors(
userId: UserId,
errors: List<PassportElementError>
) = execute(SetPassportDataErrors(userId, errors))
suspend fun TelegramBot.setPassportDataErrors(
user: User,
errors: List<PassportElementError>
) = setPassportDataErrors(user.id, errors)
suspend fun TelegramBot.setPassportDataErrors(
userId: UserId,
passportData: PassportData,
decryptor: Decryptor,
mapper: suspend Decryptor.(EncryptedPassportElement) -> PassportElementError
): Boolean = setPassportDataErrors(
userId,
passportData.data.map { decryptor.mapper(it) }.also {
if (it.isEmpty()) {
return@setPassportDataErrors false
}
}
)
suspend fun TelegramBot.setPassportDataErrors(
user: User,
passportData: PassportData,
decryptor: Decryptor,
mapper: suspend Decryptor.(EncryptedPassportElement) -> PassportElementError
) = setPassportDataErrors(user.id, passportData, decryptor, mapper)
suspend fun TelegramBot.setPassportDataErrors(
passportMessage: PassportMessage,
decryptor: Decryptor,
mapper: suspend Decryptor.(EncryptedPassportElement) -> PassportElementError
) = setPassportDataErrors(passportMessage.user, passportMessage.passportData, decryptor, mapper)

View File

@@ -7,6 +7,8 @@ import dev.inmo.tgbotapi.types.InputMedia.*
import dev.inmo.tgbotapi.types.MessageIdentifier
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.abstracts.VisualMediaGroupContent
import dev.inmo.tgbotapi.utils.RiskFeature
/**
@@ -20,7 +22,7 @@ suspend fun TelegramBot.sendMediaGroup(
replyToMessageId: MessageIdentifier? = null,
allowSendingWithoutReply: Boolean? = null
) = execute(
SendMediaGroup(
SendMediaGroup<MediaGroupContent>(
chatId, media, disableNotification, replyToMessageId, allowSendingWithoutReply
)
)

View File

@@ -31,7 +31,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
launch {
for (update in updatesChannel) {
when (val data = update.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 -> output(update)
}
}

View File

@@ -29,7 +29,9 @@ repositories {
}
kotlin {
jvm()
jvm {
compilations.main.kotlinOptions.useIR = true
}
js(BOTH) {
browser()
nodejs()

View File

@@ -1 +1 @@
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Steps Extensions","description":"This extensions project contains tools for simple interaction with chats","url":"https://insanusmokrassar.github.io/TelegramBotAPI/tgbotapi.extensions.steps","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}
{"bintrayConfig":{"repo":"TelegramBotAPI","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI","autoPublish":true,"overridePublish":true},"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 Steps Extensions","description":"This extensions project contains tools for simple interaction with chats","url":"https://insanusmokrassar.github.io/TelegramBotAPI/tgbotapi.extensions.steps","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","includeGpgSigning":true,"publishToMavenCentral":true,"developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]}}

View File

@@ -1,24 +1,9 @@
apply plugin: 'maven-publish'
apply plugin: 'signing'
task javadocsJar(type: Jar) {
classifier = 'javadoc'
}
task sourceJar (type : Jar) {
classifier = 'sources'
}
afterEvaluate {
project.publishing.publications.all {
// rename artifacts
groupId "${project.group}"
if (it.name.contains('kotlinMultiplatform')) {
artifactId = "${project.name}"
artifact sourceJar
} else {
artifactId = "${project.name}-$name"
}
}
}
publishing {
publications.all {
@@ -63,7 +48,22 @@ publishing {
password = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
}
}
maven {
name = "sonatype"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
}
}
}
}
}
}
signing {
useGpgCmd()
sign publishing.publications
}

View File

@@ -1,9 +1,11 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import dev.inmo.tgbotapi.updateshandlers.UpdatesFilter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.filter
typealias BehaviourContextReceiver<T> = suspend BehaviourContext.() -> T
typealias BehaviourContextAndTypeReceiver<T, I> = suspend BehaviourContext.(I) -> T
@@ -19,4 +21,48 @@ data class BehaviourContext(
val bot: TelegramBot,
val scope: CoroutineScope,
val flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter()
) : UpdatesFilter by flowsUpdatesFilter, TelegramBot by bot, CoroutineScope by scope
) : FlowsUpdatesFilter by flowsUpdatesFilter, TelegramBot by bot, CoroutineScope by scope
/**
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [newFlowsUpdatesFilterSetUp] is provided and
* [CoroutineScope] as new [BehaviourContext.scope]
*
* @param newFlowsUpdatesFilterSetUp As a parameter receives [FlowsUpdatesFilter] from old [this] [BehaviourContext.flowsUpdatesFilter]
*/
suspend fun <T> BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup(
newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver<Unit, FlowsUpdatesFilter>?,
behaviourContextReceiver: BehaviourContextReceiver<T>
) = copy(
flowsUpdatesFilter = FlowsUpdatesFilter(),
scope = CoroutineScope(scope.coroutineContext + SupervisorJob())
).run {
newFlowsUpdatesFilterSetUp ?.let {
it.apply { invoke(this@run, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
}
behaviourContextReceiver().also { stop() }
}
/**
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and
* [CoroutineScope] as new [BehaviourContext.scope]
*/
suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?,
behaviourContextReceiver: BehaviourContextReceiver<T>
) = doInSubContextWithFlowsUpdatesFilterSetup(
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
{ oldOne ->
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(scope, asUpdateReceiver)
}
},
behaviourContextReceiver
)
suspend fun <T> BehaviourContext.doInSubContext(
behaviourContextReceiver: BehaviourContextReceiver<T>
) = doInSubContextWithFlowsUpdatesFilterSetup(newFlowsUpdatesFilterSetUp = null, behaviourContextReceiver)
/**
* This method will cancel ALL subsequent contexts, expectations and waiters
*/
fun BehaviourContext.stop() = scope.cancel()

View File

@@ -0,0 +1,31 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.DeferredAction
import dev.inmo.micro_utils.coroutines.invokeFirstOf
import kotlinx.coroutines.*
suspend fun <T> BehaviourContext.parallel(
action: BehaviourContextReceiver<T>
) = async {
action()
}
inline infix fun <T, O> Deferred<T>.withAction(noinline callback: suspend (T) -> O) = DeferredAction(this, callback)
inline fun <T> Deferred<T>.asAction() = DeferredAction(this) { it }
suspend fun <O> BehaviourContext.oneOfActions(
deferredActions: Iterable<DeferredAction<*, O>>
) = deferredActions.invokeFirstOf(scope)
suspend fun <O> BehaviourContext.oneOfActions(
vararg deferredActions: DeferredAction<*, O>
) = this@oneOfActions.oneOfActions(deferredActions.toList())
suspend fun <O> BehaviourContext.oneOf(
deferredActions: Iterable<Deferred<O>>
) = oneOfActions(deferredActions.map { it.asAction() })
suspend fun <O> BehaviourContext.oneOf(
vararg deferredActions: Deferred<O>
) = oneOf(deferredActions.toList())

View File

@@ -24,7 +24,7 @@ private suspend fun <O> BehaviourContext.waitCallbackQueries(
}.toList().toList()
private suspend inline fun <reified T : CallbackQuery> BehaviourContext.waitEvents(
private suspend inline fun <reified T : CallbackQuery> BehaviourContext.waitCallbacks(
count: Int = 1,
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null },
@@ -51,52 +51,52 @@ suspend fun BehaviourContext.waitDataCallbackQuery(
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<DataCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitGameShortNameCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<GameShortNameCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitInlineMessageIdCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitInlineMessageIdDataCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdDataCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitInlineMessageIdGameShortNameCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<InlineMessageIdGameShortNameCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitMessageCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<MessageCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitMessageDataCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<MessageDataCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitMessageGameShortNameCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<MessageGameShortNameCallbackQuery>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)
suspend fun BehaviourContext.waitUnknownCallbackQuery(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: CallbackQueryMapper<UnknownCallbackQueryType>? = null
) = waitEvents(count, initRequest, errorFactory, filter)
) = waitCallbacks(count, initRequest, errorFactory, filter)

Some files were not shown because too many files have changed in this diff Show More