mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-11-19 13:55:57 +00:00
Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1de90412b3 | |||
| 215c8793e1 | |||
| 54835f97d1 | |||
| 923e929670 | |||
| 37488e92e6 | |||
| 830ca8122d | |||
| fbb2758bdb | |||
| a5c3e06f1c | |||
| c6fb50c4a6 | |||
| 976c0b86dc | |||
| fee5d8f925 | |||
| 808746e12d | |||
| 3fb80dd475 | |||
| db8ea0da94 | |||
| fbdfb714a3 | |||
| 1facfbc2b7 | |||
| 914a0662a9 | |||
| eda3003b7d | |||
| 459942de36 | |||
| 17f64f9b48 | |||
| 3f896c2240 | |||
| 94f8c971c5 | |||
| c43109c063 | |||
| f6058e29b4 | |||
| 3a37311331 | |||
| 9fe1472e64 | |||
| f1480c40a7 | |||
| 88eebdc448 | |||
| 8c76283db5 | |||
| 7668c48081 | |||
| 35d2135f73 | |||
| 1cb0e096be | |||
| 31f7c7f31b | |||
| 82d3b3bc48 | |||
| b3734a5c2a | |||
| 55b8736d50 | |||
| 3334fd3ca6 | |||
|
|
e2416b405a | ||
| 14f012fbfa | |||
| 1ff55057f2 | |||
| 71b5e33dbc | |||
| 08d9d183f4 | |||
| 7183634fd6 | |||
| cf9f270651 | |||
| bd87938e9c | |||
| ba76eaeb90 | |||
| d8492ae168 | |||
| 0db85232d3 | |||
| bcf2325be8 | |||
| 51174a13de | |||
| dfc1fa4d7e | |||
| 10a1d1cb38 | |||
| 6c39dc4d06 | |||
| 4877b8958e | |||
| db9c460e66 | |||
| 2bd5d53b2a | |||
| 577436843d | |||
| 048aa93044 | |||
| 4e49de0dd7 | |||
| 9dc5a7624d | |||
| 21a15db031 | |||
| a6aa4b8758 | |||
| e85d5df03e | |||
| 6833640c48 | |||
| c22c1bb144 | |||
| 8293d6683c | |||
| 7e6e892c45 | |||
| 43ac09a79b | |||
| ee1f115d77 | |||
| 7d85b6fb88 | |||
| 013944c5c9 | |||
| 55ed3e165b | |||
| 9c0106d229 | |||
| 9cd2a6220c | |||
| 033ec8f2da |
1
.github/write-good.yml
vendored
Normal file
1
.github/write-good.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
spellchecker: true
|
||||||
182
CHANGELOG.md
182
CHANGELOG.md
@@ -1,5 +1,161 @@
|
|||||||
# TelegramBotAPI changelog
|
# TelegramBotAPI changelog
|
||||||
|
|
||||||
|
## 0.27.0
|
||||||
|
|
||||||
|
* `Common`:
|
||||||
|
* Versions updates:
|
||||||
|
* `Kotlin`: `1.3.71` -> `1.3.72`
|
||||||
|
* `Klock`: `1.10.3` -> `1.10.5`
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* Typealias `LongSeconds` was added for correct explanation of seconds in `Long` primitive type
|
||||||
|
* Several new fields was added:
|
||||||
|
* `explanationField`
|
||||||
|
* `explanationEntitiesField`
|
||||||
|
* `openPeriodField`
|
||||||
|
* `closeDateField`
|
||||||
|
* Extension `List<TextPart>#justTextSources` was added for mapping of `List<TextPart>` to `List<TextSource>`
|
||||||
|
* Field `SendPoll#closeInfo` was added
|
||||||
|
* Range `openPeriodPollSecondsLimit` was added and used in all `SendPoll` requests for checking income data
|
||||||
|
* `SendQuizPoll` now able to use fields `caption` and `parseMode` for `explanation` functionality
|
||||||
|
* `quizPollExplanationLimit` was added for checking `QuizPoll` explanation size
|
||||||
|
* Field `TextLinkTextSource#url` was added
|
||||||
|
* Field `TextMentionTextSource#user` was added
|
||||||
|
* Sealed class `ScheduledCloseInfo` was added
|
||||||
|
* Class `ExactScheduledCloseInfo` was added for cases with `close_date`
|
||||||
|
* Class `ApproximateScheduledCloseInfo` was added for cases with `open_period`
|
||||||
|
* Field `Poll#scheduledCloseInfo` was added
|
||||||
|
* Sealed class `MultipleAnswersPoll` was added
|
||||||
|
* Class `RegularPoll` now extends `MultipleAnswersPoll`
|
||||||
|
* `Dice` class was replaced into new package
|
||||||
|
* Sealed class `DiceAnimationType` was added
|
||||||
|
* Field `Dice#animationType` was added as `emoji` API representation
|
||||||
|
* `SendDice` now receive `animationType` as second parameter
|
||||||
|
* For `List<TextSource>` was added several extensions:
|
||||||
|
* `toMarkdownCaptions`
|
||||||
|
* `toMarkdownTexts`
|
||||||
|
* `toMarkdownV2Captions`
|
||||||
|
* `toMarkdownV2Texts`
|
||||||
|
* `toHtmlCaptions`
|
||||||
|
* `toHtmlTexts`
|
||||||
|
* `TelegramBotAPI-extensions-api`:
|
||||||
|
* All `RequestsExecutor#sendDice` extensions now accept `DiceAnimationType?` as second parameter
|
||||||
|
* All `RequestsExecutor#sendRegularPoll` extensions now accept `ScheduledCloseInfo` fourth parameter
|
||||||
|
* All `RequestsExecutor#sendQuizPoll` extensions now accept additional parameters `caption: String` and
|
||||||
|
`parseMode: ParseMode` for `explanation` functionality and `closeInfo: ScheduledCloseInfo?` for autoclose poll
|
||||||
|
functionality
|
||||||
|
* `TelegramBotAPI-extensions-utils`:
|
||||||
|
* Several shortcuts for `ScheduledCloseInfo` was added:
|
||||||
|
* `closePollExactAt`
|
||||||
|
* `closePollExactAfter`
|
||||||
|
* `closePollAfter`
|
||||||
|
|
||||||
|
## 0.26.0
|
||||||
|
|
||||||
|
* `Common`:
|
||||||
|
* Versions updates:
|
||||||
|
* `Klock`: `1.10.0` -> `1.10.3`
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* Request `SendDice` was added (calling [sendDice](https://core.telegram.org/bots/api#senddice))
|
||||||
|
* Class `Dice` was added (type [dice](https://core.telegram.org/bots/api#dice))
|
||||||
|
* Class `DiceContent` was added (for including it in [message](https://core.telegram.org/bots/api#message) object)
|
||||||
|
* `BotCommand` was added
|
||||||
|
* `GetMyCommands` request was added
|
||||||
|
* `SetMyCommands` request was added
|
||||||
|
* `GetMe` now is object instead of class
|
||||||
|
* `GetMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe`
|
||||||
|
* `CreateNewStickerSet` renamed to `CreateStaticNewStickerSet`
|
||||||
|
* `CreateNewAnimatedStickerSet` request was added (it handle work with `tgs_sticker`)
|
||||||
|
* `StickerSet#thumb` was added
|
||||||
|
* `AddStickerToSet` renamed to `AddStaticStickerToSet`
|
||||||
|
* `AddAnimatedStickerToSet` request was added
|
||||||
|
* `SetStickerSetThumb` request was added
|
||||||
|
* Most of sticker actions now implements `StandardStickerSetAction` instead of `StickerSetAction`
|
||||||
|
* `getUpdatesLimit` was added to be ensure in get updates limit
|
||||||
|
* `GetUpdates` now will check count of requesting updates and throw exception if it is not in range `1 .. 100`
|
||||||
|
* `GetUpdates#limit` now is not nullable and by default set up to 100
|
||||||
|
* `TelegramBotAPI-extensions-api`:
|
||||||
|
* Extensions `sendDice` was added
|
||||||
|
* Extension `getMyCommands` request was added
|
||||||
|
* Extension `setMyCommands` request was added
|
||||||
|
* Extension `getMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.GetMeKt.getMe`
|
||||||
|
* **All extensions `createNewStickerSet` was renamed to `createNewStaticStickerSet`**
|
||||||
|
* Extensions `createNewAnimatedStickerSet` was added
|
||||||
|
* **All extensions `addStickerToSet` was renamed to `addStaticStickerToSet`**
|
||||||
|
* Extensions `addAnimatedStickerToSet` was added
|
||||||
|
* Extensions `setStickerSetThumb` was added
|
||||||
|
* Extension `startGettingUpdates` now will drop `SentMediaGroupUpdate` in case if it is the last in updates group
|
||||||
|
and size of retrieved updates is equal to 100 (max count of retrieved updates)
|
||||||
|
* Extensions `getUpdates` now will receive only not nullable `limit` parameter
|
||||||
|
|
||||||
|
### 0.26.4
|
||||||
|
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* Now any getting of updates will return `UnknownUpdateType` when inside of deserialization will be
|
||||||
|
`SerializationException` or `NotImplemented` error
|
||||||
|
* `CallbackGame` currently is an object
|
||||||
|
* It is possible to use `CallbackGame` for now
|
||||||
|
* `CallbackGameInlineKeyboardButton` now will not accept `callbackGame` as income object
|
||||||
|
* Now it is possible to pass exception handler in webhook
|
||||||
|
|
||||||
|
### 0.26.3
|
||||||
|
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* `CallbackGameInlineKeyboardButton` was added
|
||||||
|
([Issue-79](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/79),
|
||||||
|
[PR-80](https://github.com/InsanusMokrassar/TelegramBotAPI/pull/80))
|
||||||
|
* `UnknownInlineKeyboardButton` was added. It is unavailable for creating, but you can receive it, for example, in
|
||||||
|
`InlineQueryResult`
|
||||||
|
* `Update` now will be created even if was `SerializationException` inside of creating the update instance - in this
|
||||||
|
case will be created `UnknownUpdateType`
|
||||||
|
* `UnknownUpdateType$rawJson` value now is included (`JsonElement`)
|
||||||
|
* **EXPERIMENTALLY** `BaseEditMessageUpdate#data` now is `CommonMessage<*>`
|
||||||
|
* Suspend inline function `handleSafely` was added
|
||||||
|
* `KtorRequestsExecutor` now use `handleSafely` instead of `try` with `supervisorScope`
|
||||||
|
* `UpdatesPolling` now use `handleSafely` instead of `try` with `supervisorScope`
|
||||||
|
|
||||||
|
### 0.26.2
|
||||||
|
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* Now `EditMediaGroupUpdate` also extends `BaseEditMessageUpdate`
|
||||||
|
* **EXPERIMENTALLY** Now all `TextSource` realisations will contain `source` field as a property inside of them
|
||||||
|
* `TelegramBotAPI-extensions-api`:
|
||||||
|
* `startGettingFlowsUpdates` extension which do not require filter (but return a new one) was added
|
||||||
|
* `TelegramBotAPI-extensions-utils`:
|
||||||
|
* Subproject was added
|
||||||
|
* `filterBaseMessageUpdates`, `filterSentMediaGroupUpdates` and `filterEditMediaGroupUpdates` extensions was added
|
||||||
|
* `filterCommandsWithArgs`, `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added
|
||||||
|
* `asContentMessagesFlow`, `asChatEventsFlow` and `asUnknownMessagesFlow` extensions was added
|
||||||
|
* `withContentType` extension was added
|
||||||
|
* `onlyAnimationContentMessages` extension was added
|
||||||
|
* `onlyAudioContentMessages` extension was added
|
||||||
|
* `onlyContactContentMessages` extension was added
|
||||||
|
* `onlyDiceContentMessages` extension was added
|
||||||
|
* `onlyDocumentContentMessages` extension was added
|
||||||
|
* `onlyGameContentMessages` extension was added
|
||||||
|
* `onlyInvoiceContentMessages` extension was added
|
||||||
|
* `onlyLocationContentMessages` extension was added
|
||||||
|
* `onlyPhotoContentMessages` extension was added
|
||||||
|
* `onlyPollContentMessages` extension was added
|
||||||
|
* `onlyStickerContentMessages` extension was added
|
||||||
|
* `onlyTextContentMessages` extension was added
|
||||||
|
* `onlyVenueContentMessages` extension was added
|
||||||
|
* `onlyVideoContentMessages` extension was added
|
||||||
|
* `onlyVideoNoteContentMessages` extension was added
|
||||||
|
* `onlyVoiceContentMessages` extension was added
|
||||||
|
|
||||||
|
### 0.26.1
|
||||||
|
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* `BotCommand` now will check and throw error in case when command or description lengths is/are incorrect
|
||||||
|
* `StorageFile` now is common for all platforms
|
||||||
|
* JavaScript realization was removed due to its redundancy
|
||||||
|
* JVM realization was replaced with `fun` factory
|
||||||
|
* `StorageFile` now able to accept any factory of `Input`
|
||||||
|
* `StorageFileInfo` was added to avoid strange collisions with throws in `StorageFile`
|
||||||
|
* Fixes issue with `hashTag` for markdown
|
||||||
|
* `InvalidPhotoDimensionsException` was added for cases when `PHOTO_INVALID_DIMENSION` answer received
|
||||||
|
* Other fixes
|
||||||
|
|
||||||
## 0.25.0
|
## 0.25.0
|
||||||
|
|
||||||
* Common:
|
* Common:
|
||||||
@@ -25,32 +181,6 @@
|
|||||||
* `FlowsUpdatesFilter` now have two additional flows: `pollAnswerFlow`, `unknownUpdateTypeFlow`
|
* `FlowsUpdatesFilter` now have two additional flows: `pollAnswerFlow`, `unknownUpdateTypeFlow`
|
||||||
* `ExtendedUser` (`typealias`) was added as a `PreviewFeature`
|
* `ExtendedUser` (`typealias`) was added as a `PreviewFeature`
|
||||||
|
|
||||||
### 0.25.2
|
|
||||||
|
|
||||||
* `TelegramBotAPI`:
|
|
||||||
* Request `SendDice` was added (calling [sendDice](https://core.telegram.org/bots/api#senddice))
|
|
||||||
* Class `Dice` was added (type [dice](https://core.telegram.org/bots/api#dice))
|
|
||||||
* Class `DiceContent` was added (for including it in [message](https://core.telegram.org/bots/api#message) object)
|
|
||||||
* `BotCommand` was added
|
|
||||||
* `GetMyCommands` request was added
|
|
||||||
* `SetMyCommands` request was added
|
|
||||||
* `GetMe` now is object instead of class
|
|
||||||
* `GetMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe`
|
|
||||||
* `CreateNewStickerSet` renamed to `CreateStaticNewStickerSet`
|
|
||||||
* `CreateNewAnimatedStickerSet` request was added (it handle work with `tgs_sticker`)
|
|
||||||
* `StickerSet#thumb` was added
|
|
||||||
* `AddStickerToSet` renamed to `AddStaticStickerToSet`
|
|
||||||
* `AddAnimatedStickerToSet` request was added
|
|
||||||
* `TelegramBotAPI-extensions-api`:
|
|
||||||
* Extensions `sendDice` was added
|
|
||||||
* Extension `getMyCommands` request was added
|
|
||||||
* Extension `setMyCommands` request was added
|
|
||||||
* Extension `getMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.GetMeKt.getMe`
|
|
||||||
* **All extensions `createNewStickerSet` was renamed to `createNewStaticStickerSet`**
|
|
||||||
* Extensions `createNewAnimatedStickerSet` was added
|
|
||||||
* **All extensions `addStickerToSet` was renamed to `addStaticStickerToSet`**
|
|
||||||
* Extensions `addAnimatedStickerToSet` was added
|
|
||||||
|
|
||||||
### 0.25.1
|
### 0.25.1
|
||||||
|
|
||||||
* Update kotlin: `1.3.70` -> `1.3.71`
|
* Update kotlin: `1.3.70` -> `1.3.71`
|
||||||
|
|||||||
73
README.md
73
README.md
@@ -1,27 +1,76 @@
|
|||||||
# TelegramBotAPI
|
# TelegramBotAPI
|
||||||
|
|
||||||
| Common info | [](https://github.com/KotlinBy/awesome-kotlin) [](https://t.me/InMoTelegramBotAPI) [](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) |
|
| Common info | [](https://github.com/KotlinBy/awesome-kotlin) [](https://t.me/InMoTelegramBotAPI) [](https://travis-ci.com/InsanusMokrassar/TelegramBotAPI) |
|
||||||
| -----------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| -------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| TelegramBotAPI status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) |
|
| TelegramBotAPI status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI) |
|
||||||
| TelegramBotAPI Extensions status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) |
|
| TelegramBotAPI Extensions status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api) |
|
||||||
|
| TelegramBotAPI Util Extensions status | [](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) [](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) |
|
||||||
|
|
||||||
It is a complex of libraries for working with `TelegramBotAPI` in type-safe and strict way as much as it possible. In
|
It is a complex of libraries for working with `TelegramBotAPI` in type-safe and strict way as much as it possible. In
|
||||||
the list of this complex currently next projects:
|
the list of this complex currently next projects:
|
||||||
|
|
||||||
* [TelegramBotAPI](TelegramBotAPI/README.md) - core of library. In fact it is independent library and can be used alone
|
* [TelegramBotAPI](TelegramBotAPI/README.md) - core of library. In fact it is independent library and can be used alone
|
||||||
without any additional library
|
without any additional library
|
||||||
* [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) - contains extensions (mostly for
|
* [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) - contains extensions (mostly for
|
||||||
`RequestsExecutor`), which allows to use the core library in more pleasant way
|
`RequestsExecutor`), which allows to use the core library in more pleasant way
|
||||||
|
* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions for more comfortable
|
||||||
|
work with commands, updates and other different things
|
||||||
|
|
||||||
Most part of some specific solves or unuseful
|
Most part of some specific solves or unuseful
|
||||||
moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api).
|
moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api).
|
||||||
|
|
||||||
|
## JavaScript notes
|
||||||
|
|
||||||
|
In case if you are want to use this library inside of browser, you will need additional settings (thanks for help to [Alexander Nozik](https://research.jetbrains.org/researchers/altavir)):
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Gradle build script help</summary>
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
dependencies {
|
||||||
|
/* ... */
|
||||||
|
|
||||||
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$tgbot_api_version"
|
||||||
|
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$tgbot_api_version" // optional
|
||||||
|
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$tgbot_api_version" // optional
|
||||||
|
|
||||||
|
/* Block of dependencies for correct building in browser */
|
||||||
|
implementation(npm("fs"))
|
||||||
|
implementation(npm("bufferutil"))
|
||||||
|
implementation(npm("utf-8-validate"))
|
||||||
|
implementation(npm("abort-controller"))
|
||||||
|
implementation(npm("text-encoding"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ... */
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
target {
|
||||||
|
browser {
|
||||||
|
/* Block for fix of exception in absence of some functionality, https://github.com/ktorio/ktor/issues/1339 */
|
||||||
|
dceTask {
|
||||||
|
dceOptions {
|
||||||
|
keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## Ok, where should I start?
|
## Ok, where should I start?
|
||||||
|
|
||||||
Firstly, look at the [TelegramBotAPI](TelegramBotAPI/README.md). Here you can find all information about currently
|
In most cases, the most simple way will be to implement
|
||||||
covered Telegram Bot API and other things. After this you can look at the
|
[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) and
|
||||||
[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md).
|
[TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) for the reason that they contains more
|
||||||
|
simple tools. If you want to dive deeper in the core of library or develop something for it - welcome to
|
||||||
|
[TelegramBotAPI](TelegramBotAPI/README.md).
|
||||||
|
|
||||||
Anyway, all libraries are very typical inside of them. For example, any request in TelegramBotAPI look like
|
Anyway, all libraries are very typical inside of them. Examples:
|
||||||
`requestsExecutor.execute(SomeRequest())`.
|
|
||||||
|
* In `TelegramBotAPI` common request look like `requestsExecutor.execute(SomeRequest())`
|
||||||
|
* `TelegramBotAPI-extensions-api` typical syntax look like `requestsExecutor.someRequest()` (in most cases it would be
|
||||||
|
better to use `bot` name instead of `requestsExecutor`)
|
||||||
|
* `TelegramBotAPI-extensions-utils` will look like `filter.filterBaseMessageUpdates(chatId).filterExactCommands(Regex("^.*$"))...`
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
# TelegramBotAPI extensions
|
# TelegramBotAPI extensions
|
||||||
|
|
||||||
[](https://github.com/KotlinBy/awesome-kotlin)
|
|
||||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion)
|
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-api/_latestVersion)
|
||||||
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api)
|
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-api)
|
||||||
[](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI-extensions-api_master__publishing/)
|
|
||||||
|
|
||||||
## What is it?
|
## What is it?
|
||||||
|
|
||||||
@@ -56,20 +54,64 @@ compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$telegrambota
|
|||||||
|
|
||||||
## Example of usage and comparison with `TelegramBotAPI`
|
## Example of usage and comparison with `TelegramBotAPI`
|
||||||
|
|
||||||
As said in [TelegramBotAPI](../TelegramBotAPI/README.md#Requests), it is possible to use next syntax for requests:
|
Here presented review table for comparison of api from original [TelegramBotAPI](../TelegramBotAPI/README.md#Requests)
|
||||||
|
and extensions-api library:
|
||||||
|
|
||||||
```kotlin
|
In all examples supposed that you have created bot with next approximate lines:
|
||||||
val requestsExecutor: RequestsExecutor = ...
|
|
||||||
requestsExecutor.execute(GetMe())
|
|
||||||
```
|
|
||||||
|
|
||||||
This library offer a little bit another way for this:
|
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
val bot: RequestsExecutor = ...
|
val bot: RequestsExecutor = ...
|
||||||
bot.getMe()
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The result type of [GetMe (and getMe extension)](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt)
|
| TelegramBotAPI | TelegramBotAPI-extensions-api |
|
||||||
request is
|
|----------------|-------------------------------|
|
||||||
[ExtendedBot](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt).
|
| bot.execute(GetMe) | bot.getMe() |
|
||||||
|
| bot.execute(SendTextMessage(someChatId, text)) | bot.sendTextMessage(chat, text) |
|
||||||
|
|
||||||
|
## Updates
|
||||||
|
|
||||||
|
Usually, it is more comfortable to use filter object to get separated types of updates:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val filter = FlowsUpdatesFilter(100)
|
||||||
|
```
|
||||||
|
|
||||||
|
In this case you will be able:
|
||||||
|
|
||||||
|
* Separate types of incoming updates (even media groups)
|
||||||
|
* Simplify launch of getting updates:
|
||||||
|
```kotlin
|
||||||
|
bot.startGettingOfUpdates(
|
||||||
|
filter,
|
||||||
|
scope = CoroutineScope(Dispatchers.Default)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
* Use `filter` flows to comfortable filter, map and do other operations with the whole
|
||||||
|
getting updates process:
|
||||||
|
```kotlin
|
||||||
|
filter.messageFlow.mapNotNull {
|
||||||
|
it.data as? ContentMessage<*>
|
||||||
|
}.onEach {
|
||||||
|
println(it)
|
||||||
|
}.launchIn(
|
||||||
|
CoroutineScope(Dispatchers.Default)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Alternative way
|
||||||
|
|
||||||
|
There is an alternative way to get updates. In fact it is almost the same, but could be more useful for some cases:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val filter = bot.startGettingOfUpdates(
|
||||||
|
scope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) { // Here as reveiver will be FlowsUpdatesFilter
|
||||||
|
messageFlow.mapNotNull {
|
||||||
|
it.data as? ContentMessage<*>
|
||||||
|
}.onEach {
|
||||||
|
println(it)
|
||||||
|
}.launchIn(
|
||||||
|
CoroutineScope(Dispatchers.Default)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|||||||
|
|
||||||
suspend fun RequestsExecutor.getUpdates(
|
suspend fun RequestsExecutor.getUpdates(
|
||||||
offset: UpdateIdentifier? = null,
|
offset: UpdateIdentifier? = null,
|
||||||
limit: Int? = null,
|
limit: Int = getUpdatesLimit.last,
|
||||||
timeout: Seconds? = null,
|
timeout: Seconds? = null,
|
||||||
allowed_updates: List<String>? = ALL_UPDATES_LIST
|
allowed_updates: List<String>? = ALL_UPDATES_LIST
|
||||||
) = execute(
|
) = execute(
|
||||||
@@ -18,7 +18,7 @@ suspend fun RequestsExecutor.getUpdates(
|
|||||||
|
|
||||||
suspend fun RequestsExecutor.getUpdates(
|
suspend fun RequestsExecutor.getUpdates(
|
||||||
lastUpdate: Update,
|
lastUpdate: Update,
|
||||||
limit: Int? = null,
|
limit: Int = getUpdatesLimit.last,
|
||||||
timeout: Seconds? = null,
|
timeout: Seconds? = null,
|
||||||
allowed_updates: List<String>? = ALL_UPDATES_LIST
|
allowed_updates: List<String>? = ALL_UPDATES_LIST
|
||||||
) = getUpdates(
|
) = getUpdates(
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMyCommands
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.SetMyCommands
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.SetMyCommands
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand
|
import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand
|
||||||
|
|
||||||
|
|||||||
@@ -6,19 +6,22 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.dice.DiceAnimationType
|
||||||
|
|
||||||
suspend fun RequestsExecutor.sendDice(
|
suspend fun RequestsExecutor.sendDice(
|
||||||
chatId: ChatIdentifier,
|
chatId: ChatIdentifier,
|
||||||
|
animationType: DiceAnimationType? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = execute(
|
) = execute(
|
||||||
SendDice(chatId, disableNotification, replyToMessageId, replyMarkup)
|
SendDice(chatId, animationType, disableNotification, replyToMessageId, replyMarkup)
|
||||||
)
|
)
|
||||||
|
|
||||||
suspend fun RequestsExecutor.sendDice(
|
suspend fun RequestsExecutor.sendDice(
|
||||||
chat: Chat,
|
chat: Chat,
|
||||||
|
animationType: DiceAnimationType? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = sendDice(chat.id, disableNotification, replyToMessageId, replyMarkup)
|
) = sendDice(chat.id, animationType, disableNotification, replyToMessageId, replyMarkup)
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.SendQuizPo
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.SendRegularPoll
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.SendRegularPoll
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.QuizPoll
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.RegularPoll
|
|
||||||
|
|
||||||
suspend fun RequestsExecutor.sendRegularPoll(
|
suspend fun RequestsExecutor.sendRegularPoll(
|
||||||
chatId: ChatIdentifier,
|
chatId: ChatIdentifier,
|
||||||
@@ -17,12 +17,13 @@ suspend fun RequestsExecutor.sendRegularPoll(
|
|||||||
isAnonymous: Boolean = true,
|
isAnonymous: Boolean = true,
|
||||||
isClosed: Boolean = false,
|
isClosed: Boolean = false,
|
||||||
allowMultipleAnswers: Boolean = false,
|
allowMultipleAnswers: Boolean = false,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = execute(
|
) = execute(
|
||||||
SendRegularPoll(
|
SendRegularPoll(
|
||||||
chatId, question, options, isAnonymous, isClosed, allowMultipleAnswers, disableNotification, replyToMessageId, replyMarkup
|
chatId, question, options, isAnonymous, isClosed, allowMultipleAnswers, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
suspend fun RequestsExecutor.sendRegularPoll(
|
suspend fun RequestsExecutor.sendRegularPoll(
|
||||||
@@ -33,12 +34,13 @@ suspend fun RequestsExecutor.sendRegularPoll(
|
|||||||
options: List<String> = poll.options.map { it.text },
|
options: List<String> = poll.options.map { it.text },
|
||||||
isAnonymous: Boolean = poll.isAnonymous,
|
isAnonymous: Boolean = poll.isAnonymous,
|
||||||
allowMultipleAnswers: Boolean = poll.allowMultipleAnswers,
|
allowMultipleAnswers: Boolean = poll.allowMultipleAnswers,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = execute(
|
) = execute(
|
||||||
SendRegularPoll(
|
SendRegularPoll(
|
||||||
chatId, question, options, isAnonymous, isClosed, allowMultipleAnswers, disableNotification, replyToMessageId, replyMarkup
|
chatId, question, options, isAnonymous, isClosed, allowMultipleAnswers, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,11 +51,12 @@ suspend fun RequestsExecutor.sendRegularPoll(
|
|||||||
isAnonymous: Boolean = true,
|
isAnonymous: Boolean = true,
|
||||||
isClosed: Boolean = false,
|
isClosed: Boolean = false,
|
||||||
allowMultipleAnswers: Boolean = false,
|
allowMultipleAnswers: Boolean = false,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = sendRegularPoll(
|
) = sendRegularPoll(
|
||||||
chat.id, question, options, isAnonymous, isClosed, allowMultipleAnswers, disableNotification, replyToMessageId, replyMarkup
|
chat.id, question, options, isAnonymous, isClosed, allowMultipleAnswers, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
|
|
||||||
suspend fun RequestsExecutor.sendRegularPoll(
|
suspend fun RequestsExecutor.sendRegularPoll(
|
||||||
@@ -64,11 +67,12 @@ suspend fun RequestsExecutor.sendRegularPoll(
|
|||||||
options: List<String> = poll.options.map { it.text },
|
options: List<String> = poll.options.map { it.text },
|
||||||
isAnonymous: Boolean = poll.isAnonymous,
|
isAnonymous: Boolean = poll.isAnonymous,
|
||||||
allowMultipleAnswers: Boolean = poll.allowMultipleAnswers,
|
allowMultipleAnswers: Boolean = poll.allowMultipleAnswers,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = sendRegularPoll(
|
) = sendRegularPoll(
|
||||||
chat.id, question, options, isAnonymous, isClosed, allowMultipleAnswers, disableNotification, replyToMessageId, replyMarkup
|
chat.id, question, options, isAnonymous, isClosed, allowMultipleAnswers, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -79,12 +83,15 @@ suspend fun RequestsExecutor.sendQuizPoll(
|
|||||||
correctOptionId: Int,
|
correctOptionId: Int,
|
||||||
isAnonymous: Boolean = true,
|
isAnonymous: Boolean = true,
|
||||||
isClosed: Boolean = false,
|
isClosed: Boolean = false,
|
||||||
|
caption: String? = null,
|
||||||
|
parseMode: ParseMode? = null,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = execute(
|
) = execute(
|
||||||
SendQuizPoll(
|
SendQuizPoll(
|
||||||
chatId, question, options, correctOptionId, isAnonymous, isClosed, disableNotification, replyToMessageId, replyMarkup
|
chatId, question, options, correctOptionId, isAnonymous, isClosed, caption, parseMode, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,11 +102,14 @@ suspend fun RequestsExecutor.sendQuizPoll(
|
|||||||
correctOptionId: Int,
|
correctOptionId: Int,
|
||||||
isAnonymous: Boolean = true,
|
isAnonymous: Boolean = true,
|
||||||
isClosed: Boolean = false,
|
isClosed: Boolean = false,
|
||||||
|
caption: String? = null,
|
||||||
|
parseMode: ParseMode? = null,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = sendQuizPoll(
|
) = sendQuizPoll(
|
||||||
chat.id, question, options, correctOptionId, isAnonymous, isClosed, disableNotification, replyToMessageId, replyMarkup
|
chat.id, question, options, correctOptionId, isAnonymous, isClosed, caption, parseMode, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
|
|
||||||
suspend fun RequestsExecutor.sendQuizPoll(
|
suspend fun RequestsExecutor.sendQuizPoll(
|
||||||
@@ -110,12 +120,15 @@ suspend fun RequestsExecutor.sendQuizPoll(
|
|||||||
options: List<String> = quizPoll.options.map { it.text },
|
options: List<String> = quizPoll.options.map { it.text },
|
||||||
correctOptionId: Int = quizPoll.correctOptionId ?: error("Correct option ID must be provided by income QuizPoll or by developer"),
|
correctOptionId: Int = quizPoll.correctOptionId ?: error("Correct option ID must be provided by income QuizPoll or by developer"),
|
||||||
isAnonymous: Boolean = quizPoll.isAnonymous,
|
isAnonymous: Boolean = quizPoll.isAnonymous,
|
||||||
|
caption: String? = null,
|
||||||
|
parseMode: ParseMode? = null,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = execute(
|
) = execute(
|
||||||
SendQuizPoll(
|
SendQuizPoll(
|
||||||
chatId, question, options, correctOptionId, isAnonymous, isClosed, disableNotification, replyToMessageId, replyMarkup
|
chatId, question, options, correctOptionId, isAnonymous, isClosed, caption, parseMode, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -127,9 +140,12 @@ suspend fun RequestsExecutor.sendQuizPoll(
|
|||||||
options: List<String> = quizPoll.options.map { it.text },
|
options: List<String> = quizPoll.options.map { it.text },
|
||||||
correctOptionId: Int = quizPoll.correctOptionId ?: error("Correct option ID must be provided by income QuizPoll or by developer"),
|
correctOptionId: Int = quizPoll.correctOptionId ?: error("Correct option ID must be provided by income QuizPoll or by developer"),
|
||||||
isAnonymous: Boolean = quizPoll.isAnonymous,
|
isAnonymous: Boolean = quizPoll.isAnonymous,
|
||||||
|
caption: String? = null,
|
||||||
|
parseMode: ParseMode? = null,
|
||||||
|
closeInfo: ScheduledCloseInfo? = null,
|
||||||
disableNotification: Boolean = false,
|
disableNotification: Boolean = false,
|
||||||
replyToMessageId: MessageIdentifier? = null,
|
replyToMessageId: MessageIdentifier? = null,
|
||||||
replyMarkup: KeyboardMarkup? = null
|
replyMarkup: KeyboardMarkup? = null
|
||||||
) = sendQuizPoll(
|
) = sendQuizPoll(
|
||||||
chat.id, question, options, correctOptionId, isAnonymous, isClosed, disableNotification, replyToMessageId, replyMarkup
|
chat.id, question, options, correctOptionId, isAnonymous, isClosed, caption, parseMode, closeInfo, disableNotification, replyToMessageId, replyMarkup
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.thumbs
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.SetStickerSetThumb
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.CommonUser
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UserId
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.StickerSet
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
userId: UserId,
|
||||||
|
thumbSetName: String,
|
||||||
|
thumb: FileId
|
||||||
|
) = execute(
|
||||||
|
SetStickerSetThumb(userId, thumbSetName, thumb)
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
userId: UserId,
|
||||||
|
thumbSetName: String,
|
||||||
|
thumb: MultipartFile
|
||||||
|
) = execute(
|
||||||
|
SetStickerSetThumb(userId, thumbSetName, thumb)
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
user: CommonUser,
|
||||||
|
thumbSetName: String,
|
||||||
|
thumb: FileId
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
user.id, thumbSetName, thumb
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
user: CommonUser,
|
||||||
|
thumbSetName: String,
|
||||||
|
thumb: MultipartFile
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
user.id, thumbSetName, thumb
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
userId: UserId,
|
||||||
|
thumbSet: StickerSet,
|
||||||
|
thumb: FileId
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
userId, thumbSet.name, thumb
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
userId: UserId,
|
||||||
|
thumbSet: StickerSet,
|
||||||
|
thumb: MultipartFile
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
userId, thumbSet.name, thumb
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
user: CommonUser,
|
||||||
|
thumbSet: StickerSet,
|
||||||
|
thumb: FileId
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
user.id, thumbSet.name, thumb
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setStickerSetThumb(
|
||||||
|
user: CommonUser,
|
||||||
|
thumbSet: StickerSet,
|
||||||
|
thumb: MultipartFile
|
||||||
|
) = setStickerSetThumb(
|
||||||
|
user.id, thumbSet.name, thumb
|
||||||
|
)
|
||||||
|
|
||||||
@@ -5,13 +5,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestExceptio
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
|
||||||
import io.ktor.client.features.HttpRequestTimeoutException
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
fun RequestsExecutor.startGettingOfUpdates(
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
@@ -24,33 +24,62 @@ fun RequestsExecutor.startGettingOfUpdates(
|
|||||||
var lastUpdateIdentifier: UpdateIdentifier? = null
|
var lastUpdateIdentifier: UpdateIdentifier? = null
|
||||||
|
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
try {
|
handleSafely(
|
||||||
supervisorScope {
|
{ e ->
|
||||||
val updates = getUpdates(
|
exceptionsHandler ?.invoke(e)
|
||||||
offset = lastUpdateIdentifier?.plus(1),
|
if (e is RequestException) {
|
||||||
timeout = timeoutSeconds,
|
delay(1000L)
|
||||||
allowed_updates = allowedUpdates
|
}
|
||||||
).convertWithMediaGroupUpdates()
|
}
|
||||||
|
) {
|
||||||
supervisorScope {
|
val updates = getUpdates(
|
||||||
for (update in updates) {
|
offset = lastUpdateIdentifier?.plus(1),
|
||||||
updatesReceiver(update)
|
timeout = timeoutSeconds,
|
||||||
|
allowed_updates = allowedUpdates
|
||||||
lastUpdateIdentifier = update.lastUpdateIdentifier()
|
).let { originalUpdates ->
|
||||||
}
|
val converted = originalUpdates.convertWithMediaGroupUpdates()
|
||||||
|
/**
|
||||||
|
* Dirty hack for cases when the media group was retrieved not fully:
|
||||||
|
*
|
||||||
|
* We are throw out the last media group and will reretrieve it again in the next get updates
|
||||||
|
* and it will guarantee that it is full
|
||||||
|
*/
|
||||||
|
if (originalUpdates.size == getUpdatesLimit.last && converted.last() is SentMediaGroupUpdate) {
|
||||||
|
converted - converted.last()
|
||||||
|
} else {
|
||||||
|
converted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSafely {
|
||||||
|
for (update in updates) {
|
||||||
|
updatesReceiver(update)
|
||||||
|
|
||||||
|
lastUpdateIdentifier = update.lastUpdateIdentifier()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: HttpRequestTimeoutException) {
|
|
||||||
exceptionsHandler ?.invoke(e) // it is ok due to mechanism of long polling
|
|
||||||
} catch (e: RequestException) {
|
|
||||||
exceptionsHandler ?.invoke(e) // it is not ok, but in most cases it will mean that there is some limit for requests count
|
|
||||||
delay(1000L)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
exceptionsHandler ?.invoke(e)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will create a new one [FlowsUpdatesFilter]. This method could be unsafe due to the fact that it will start
|
||||||
|
* getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call
|
||||||
|
* [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows. To avoid it, you can pass
|
||||||
|
* [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting
|
||||||
|
*/
|
||||||
|
@PreviewFeature
|
||||||
|
fun RequestsExecutor.startGettingFlowsUpdates(
|
||||||
|
timeoutSeconds: Seconds = 30,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||||
|
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
|
||||||
|
flowsUpdatesFilterUpdatesKeeperCount: Int = 64,
|
||||||
|
flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {}
|
||||||
|
): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply {
|
||||||
|
flowUpdatesPreset()
|
||||||
|
startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver)
|
||||||
|
}
|
||||||
|
|
||||||
fun RequestsExecutor.startGettingOfUpdates(
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
updatesFilter: UpdatesFilter,
|
updatesFilter: UpdatesFilter,
|
||||||
timeoutSeconds: Seconds = 30,
|
timeoutSeconds: Seconds = 30,
|
||||||
|
|||||||
112
TelegramBotAPI-extensions-utils/README.md
Normal file
112
TelegramBotAPI-extensions-utils/README.md
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
# TelegramBotAPI Util Extensions
|
||||||
|
|
||||||
|
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion)
|
||||||
|
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils)
|
||||||
|
|
||||||
|
## What is it?
|
||||||
|
|
||||||
|
It is wrapper library for [TelegramBotAPI](../TelegramBotAPI/README.md). Currently, this library contains some usefull filters for commands, updates types and different others.
|
||||||
|
|
||||||
|
## How to implement library?
|
||||||
|
|
||||||
|
Common ways to implement this library are presented here. In some cases it will require additional steps
|
||||||
|
like inserting of additional libraries (like `kotlin stdlib`). In the examples will be used variable
|
||||||
|
`telegrambotapi-extensions-utils_version`, which must be set up by developer. Available versions are presented on
|
||||||
|
[bintray](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils), next version is last published:
|
||||||
|
|
||||||
|
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion)
|
||||||
|
|
||||||
|
### Maven
|
||||||
|
|
||||||
|
Dependency config presented here:
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.insanusmokrassar</groupId>
|
||||||
|
<artifactId>TelegramBotAPI-extensions-utils</artifactId>
|
||||||
|
<version>${telegrambotapi-extensions-utils_version}</version>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Gradle
|
||||||
|
|
||||||
|
To use last versions you will need to add one line in repositories block of your `build.gradle`:
|
||||||
|
|
||||||
|
`jcenter()` or `mavenCentral()`
|
||||||
|
|
||||||
|
And add next line to your dependencies block:
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
implementation "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version"
|
||||||
|
```
|
||||||
|
|
||||||
|
or for old gradle:
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-utils:$telegrambotapi-extensions-utils_version"
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to use?
|
||||||
|
|
||||||
|
Here will be presented several examples of usage. In all cases it is expected that you have created your bot and filter:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val bot: RequestsExecutor = KtorRequestsExecutor(
|
||||||
|
TelegramAPIUrlsKeeper(BOT_TOKEN)
|
||||||
|
)
|
||||||
|
val filter = FlowsUpdatesFilter(64)
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternative way to use the things below:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val filter = bot.startGettingUpdates(
|
||||||
|
scope = CoroutineScope(Dispatchers.Default)
|
||||||
|
) {
|
||||||
|
// place code from examples here with replacing of `filter` by `this`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Getting of only text incoming messages
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
filter.asContentMessagesFlow().onlyTextContentMessages().onEach {
|
||||||
|
println(it.content)
|
||||||
|
println(it.fullEntitiesList())
|
||||||
|
}.launchIn(
|
||||||
|
CoroutineScope(Dispatchers.Default)
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
As a result, each received message which will be just text message will be printed out with full list of its internal entities
|
||||||
|
|
||||||
|
## Shortcuts
|
||||||
|
|
||||||
|
With shortcuts you are able to use simple factories for several things.
|
||||||
|
|
||||||
|
### ScheduledCloseInfo
|
||||||
|
|
||||||
|
In case if you are creating some poll, you able to use next shortcuts.
|
||||||
|
|
||||||
|
Next sample will use info with closing at the 10 seconds after now:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
closePollExactAt(DateTime.now() + TimeSpan(10000.0))
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example we will do the same, but in another way:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
closePollExactAfter(10)
|
||||||
|
```
|
||||||
|
|
||||||
|
Here we have passed `10` seconds and will get the same result object.
|
||||||
|
|
||||||
|
In opposite to previous shortcuts, the next one will create `approximate` closing schedule:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
closePollAfter(10)
|
||||||
|
```
|
||||||
|
|
||||||
|
The main difference here is that the last one will be closed after 10 seconds since the sending. With first samples
|
||||||
|
will be created **exact** time for closing of poll
|
||||||
48
TelegramBotAPI-extensions-utils/build.gradle
Normal file
48
TelegramBotAPI-extensions-utils/build.gradle
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||||
|
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
|
||||||
|
}
|
||||||
|
|
||||||
|
project.version = "$library_version"
|
||||||
|
project.group = "$library_group"
|
||||||
|
|
||||||
|
apply from: "publish.gradle"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
|
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
js()
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") {
|
||||||
|
api "${project.group}:TelegramBotAPI:$library_version"
|
||||||
|
} else {
|
||||||
|
implementation project(":TelegramBotAPI")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
57
TelegramBotAPI-extensions-utils/maven.publish.gradle
Normal file
57
TelegramBotAPI-extensions-utils/maven.publish.gradle
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
apply plugin: 'maven-publish'
|
||||||
|
|
||||||
|
task javadocsJar(type: Jar) {
|
||||||
|
classifier = 'javadoc'
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEvaluate {
|
||||||
|
project.publishing.publications.all {
|
||||||
|
// rename artifacts
|
||||||
|
groupId "${project.group}"
|
||||||
|
if (it.name.contains('kotlinMultiplatform')) {
|
||||||
|
artifactId = "${project.name}"
|
||||||
|
} else {
|
||||||
|
artifactId = "${project.name}-$name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications.all {
|
||||||
|
artifact javadocsJar
|
||||||
|
|
||||||
|
pom.withXml {
|
||||||
|
asNode().children().last() + {
|
||||||
|
resolveStrategy = Closure.DELEGATE_FIRST
|
||||||
|
|
||||||
|
description "Util extensions for more useful work with updates and other things"
|
||||||
|
name "Telegram Bot API Utility Extensions"
|
||||||
|
url "https://insanusmokrassar.github.io/TelegramBotAPI"
|
||||||
|
|
||||||
|
scm {
|
||||||
|
developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||||
|
url "https://github.com/insanusmokrassar/TelegramBotAPI.git"
|
||||||
|
}
|
||||||
|
|
||||||
|
developers {
|
||||||
|
|
||||||
|
developer {
|
||||||
|
id "InsanusMokrassar"
|
||||||
|
name "Ovsiannikov Aleksei"
|
||||||
|
email "ovsyannikov.alexey95@gmail.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
licenses {
|
||||||
|
|
||||||
|
license {
|
||||||
|
name "Apache Software License 2.0"
|
||||||
|
url "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Utility Extensions","description":"Util extensions for more useful work with updates and other things","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"}
|
||||||
55
TelegramBotAPI-extensions-utils/publish.gradle
Normal file
55
TelegramBotAPI-extensions-utils/publish.gradle
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
apply plugin: 'com.jfrog.bintray'
|
||||||
|
|
||||||
|
apply from: "maven.publish.gradle"
|
||||||
|
|
||||||
|
bintray {
|
||||||
|
user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER')
|
||||||
|
key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY')
|
||||||
|
filesSpec {
|
||||||
|
from "${buildDir}/publications/"
|
||||||
|
eachFile {
|
||||||
|
String directorySubname = it.getFile().parentFile.name
|
||||||
|
if (it.getName() == "module.json") {
|
||||||
|
if (directorySubname == "kotlinMultiplatform") {
|
||||||
|
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module")
|
||||||
|
} else {
|
||||||
|
it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") {
|
||||||
|
it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom")
|
||||||
|
} else {
|
||||||
|
it.exclude()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
into "${project.group}".replace(".", "/")
|
||||||
|
}
|
||||||
|
pkg {
|
||||||
|
repo = "StandardRepository"
|
||||||
|
name = "${project.name}"
|
||||||
|
vcsUrl = "https://github.com/InsanusMokrassar/TelegramBotAPI"
|
||||||
|
licenses = ["Apache-2.0"]
|
||||||
|
version {
|
||||||
|
name = "${project.version}"
|
||||||
|
released = new Date()
|
||||||
|
vcsTag = "${project.version}"
|
||||||
|
gpg {
|
||||||
|
sign = true
|
||||||
|
passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bintrayUpload.doFirst {
|
||||||
|
publications = publishing.publications.collect {
|
||||||
|
if (it.name.contains('kotlinMultiplatform')) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
it.name
|
||||||
|
}
|
||||||
|
} - null
|
||||||
|
}
|
||||||
|
|
||||||
|
bintrayUpload.dependsOn publishToMavenLocal
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.*
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
|
|
||||||
|
fun <T : CallbackQuery> Flow<T>.onlyMessageDataCallbackQueries() = mapNotNull {
|
||||||
|
it as? MessageDataCallbackQuery
|
||||||
|
}
|
||||||
|
fun <T : CallbackQuery> Flow<T>.onlyInlineMessageIdDataCallbackQueries() = mapNotNull {
|
||||||
|
it as? InlineMessageIdDataCallbackQuery
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
|
fun <T : MessageContent> Flow<ContentMessage<*>>.withContentType(contentType: KClass<T>) = mapNotNull {
|
||||||
|
if (contentType.isInstance(it.content)) {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
it as ContentMessage<T>
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Flow<ContentMessage<*>>.onlyAnimationContentMessages() = withContentType(AnimationContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyAudioContentMessages() = withContentType(AudioContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyContactContentMessages() = withContentType(ContactContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyDiceContentMessages() = withContentType(DiceContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyDocumentContentMessages() = withContentType(DocumentContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyGameContentMessages() = withContentType(GameContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyInvoiceContentMessages() = withContentType(InvoiceContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyLocationContentMessages() = withContentType(LocationContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyPhotoContentMessages() = withContentType(PhotoContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyPollContentMessages() = withContentType(PollContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyStickerContentMessages() = withContentType(StickerContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyTextContentMessages() = withContentType(TextContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyVenueContentMessages() = withContentType(VenueContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyVideoContentMessages() = withContentType(VideoContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyVideoNoteContentMessages() = withContentType(VideoNoteContent::class)
|
||||||
|
fun Flow<ContentMessage<*>>.onlyVoiceContentMessages() = withContentType(VoiceContent::class)
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.LongSeconds
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.ApproximateScheduledCloseInfo
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.ExactScheduledCloseInfo
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.TimeSpan
|
||||||
|
|
||||||
|
fun closePollExactAt(
|
||||||
|
dateTime: DateTime
|
||||||
|
) = ExactScheduledCloseInfo(
|
||||||
|
dateTime
|
||||||
|
)
|
||||||
|
|
||||||
|
fun closePollExactAfter(
|
||||||
|
seconds: LongSeconds
|
||||||
|
) = closePollExactAt(
|
||||||
|
DateTime.now() + TimeSpan(seconds.toDouble() * 1000L)
|
||||||
|
)
|
||||||
|
fun closePollExactAfter(
|
||||||
|
seconds: Seconds
|
||||||
|
) = closePollExactAfter(
|
||||||
|
seconds.toLong()
|
||||||
|
)
|
||||||
|
|
||||||
|
fun closePollAfter(
|
||||||
|
seconds: LongSeconds
|
||||||
|
) = ApproximateScheduledCloseInfo(
|
||||||
|
TimeSpan(seconds.toDouble() * 1000L)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun closePollAfter(
|
||||||
|
seconds: Seconds
|
||||||
|
) = closePollAfter(seconds.toLong())
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.CallbackQueryUpdate
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
|
|
||||||
|
fun Flow<CallbackQueryUpdate>.asDataCallbackQueryFlow() = mapNotNull {
|
||||||
|
it.data as? DataCallbackQuery
|
||||||
|
}
|
||||||
|
fun Flow<CallbackQueryUpdate>.asGameShortNameCallbackQueryFlow() = mapNotNull {
|
||||||
|
it.data as? GameShortNameCallbackQuery
|
||||||
|
}
|
||||||
|
fun Flow<CallbackQueryUpdate>.asUnknownCallbackQueryFlow() = mapNotNull {
|
||||||
|
it.data as? UnknownCallbackQueryType
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.RegularTextSource
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.filterExactCommands(
|
||||||
|
commandRegex: Regex
|
||||||
|
) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage ->
|
||||||
|
(contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.filterCommandsInsideTextMessages(
|
||||||
|
commandRegex: Regex
|
||||||
|
) = asContentMessagesFlow().onlyTextContentMessages().filter { contentMessage ->
|
||||||
|
contentMessage.content.fullEntitiesList().any {
|
||||||
|
(it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Result [Flow] will emit all [TextSource]s to the collector ONLY IN CASE if first [TextSource] is
|
||||||
|
* [BotCommandTextSource] and its [BotCommandTextSource.command] is [Regex.matches] to incoming [commandRegex]. Internal
|
||||||
|
* behaviour contains next rules: all incoming text sources will be passed as is, [RegularTextSource] will be divided
|
||||||
|
* by " " for several [RegularTextSource] which will contains not empty args without spaces
|
||||||
|
*/
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.filterCommandsWithArgs(
|
||||||
|
commandRegex: Regex
|
||||||
|
): Flow<List<TextSource>> = asContentMessagesFlow().onlyTextContentMessages().mapNotNull { contentMessage ->
|
||||||
|
val allEntities = contentMessage.content.fullEntitiesList()
|
||||||
|
(allEntities.firstOrNull() as? BotCommandTextSource) ?.let {
|
||||||
|
if (commandRegex.matches(it.command)) {
|
||||||
|
allEntities.flatMap {
|
||||||
|
when (it) {
|
||||||
|
is RegularTextSource -> it.source.split(" ").mapNotNull { regularTextSourcePart ->
|
||||||
|
if (regularTextSourcePart.isNotBlank()) {
|
||||||
|
RegularTextSource(regularTextSourcePart)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> listOf(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
|
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.asContentMessagesFlow() = mapNotNull {
|
||||||
|
it.data as? ContentMessage<*>
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.asChatEventsFlow() = mapNotNull {
|
||||||
|
it.data as? ChatEventMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : BaseSentMessageUpdate> Flow<T>.asUnknownMessagesFlow() = mapNotNull {
|
||||||
|
it.data as? UnknownMessageType
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
|
|
||||||
|
fun <T : BaseMessageUpdate> Flow<T>.filterBaseMessageUpdates(chatId: ChatId): Flow<T> = filter {
|
||||||
|
it.data.chat.id == chatId
|
||||||
|
}
|
||||||
|
fun <T : BaseMessageUpdate> Flow<T>.filterBaseMessageUpdates(chat: Chat): Flow<T> = filterBaseMessageUpdates(chat.id)
|
||||||
|
|
||||||
|
|
||||||
|
fun <T : SentMediaGroupUpdate> Flow<T>.filterSentMediaGroupUpdates(chatId: ChatId): Flow<T> = filter {
|
||||||
|
it.data.first().chat.id == chatId
|
||||||
|
}
|
||||||
|
fun <T : SentMediaGroupUpdate> Flow<T>.filterSentMediaGroupUpdates(chat: Chat): Flow<T> = filterSentMediaGroupUpdates(chat.id)
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
# TelegramBotAPI
|
# TelegramBotAPI
|
||||||
|
|
||||||
[](https://github.com/KotlinBy/awesome-kotlin)
|
|
||||||
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion)
|
[ ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI/_latestVersion)
|
||||||
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI)
|
[](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI)
|
||||||
[](https://jenkins.insanusmokrassar.com/job/TelegramBotAPI_master__publishing/)
|
|
||||||
|
|
||||||
## What is it?
|
## What is it?
|
||||||
|
|
||||||
@@ -12,11 +10,10 @@ moments are describing by official [Telegram Bot API](https://core.telegram.org/
|
|||||||
|
|
||||||
## Compatibility
|
## Compatibility
|
||||||
|
|
||||||
This version compatible with [23th of January 2020 update of TelegramBotAPI (version 4.6)](https://core.telegram.org/bots/api#january-23-2020).
|
This version compatible with [24th of April 2020 update of TelegramBotAPI (version 4.8)](https://core.telegram.org/bots/api#april-24-2020).
|
||||||
There is Telegram Passport API exception of implemented functionality, which was presented in
|
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
|
[August 2018 update of TelegramBotAPI](https://core.telegram.org/bots/api-changelog#august-27-2018) update. It will be implemented
|
||||||
as soon as possible. All APIs that are not included are presented
|
as soon as possible.
|
||||||
[wiki](https://github.com/InsanusMokrassar/TelegramBotAPI/wiki/Not-included-API).
|
|
||||||
|
|
||||||
## How to implement library?
|
## How to implement library?
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ interface TextSource {
|
|||||||
val asMarkdownSource: String
|
val asMarkdownSource: String
|
||||||
val asMarkdownV2Source: String
|
val asMarkdownV2Source: String
|
||||||
val asHtmlSource: String
|
val asHtmlSource: String
|
||||||
|
val source: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -15,3 +16,5 @@ data class TextPart(
|
|||||||
val range: IntRange,
|
val range: IntRange,
|
||||||
val source: TextSource
|
val source: TextSource
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun List<TextPart>.justTextSources() = map { it.source }
|
||||||
|
|||||||
@@ -9,15 +9,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.limiters.RequestL
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.receive
|
import io.ktor.client.call.receive
|
||||||
import io.ktor.client.features.*
|
import io.ktor.client.features.*
|
||||||
import io.ktor.client.statement.HttpStatement
|
import io.ktor.client.statement.HttpStatement
|
||||||
import io.ktor.client.statement.readText
|
import io.ktor.client.statement.readText
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.supervisorScope
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
class KtorRequestsExecutor(
|
class KtorRequestsExecutor(
|
||||||
@@ -43,54 +41,56 @@ class KtorRequestsExecutor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
return try {
|
return handleSafely(
|
||||||
supervisorScope {
|
{ e ->
|
||||||
requestsLimiter.limit {
|
throw if (e is ClientRequestException) {
|
||||||
var statement: HttpStatement? = null
|
val content = e.response.readText()
|
||||||
for (factory in callsFactories) {
|
|
||||||
statement = factory.prepareCall(
|
|
||||||
client,
|
|
||||||
telegramAPIUrlsKeeper.commonAPIUrl,
|
|
||||||
request
|
|
||||||
)
|
|
||||||
if (statement != null) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val response = statement?.execute() ?: throw IllegalArgumentException("Can't execute request: $request")
|
|
||||||
val content = response.receive<String>()
|
|
||||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||||
|
newRequestException(
|
||||||
(responseObject.result?.let {
|
responseObject,
|
||||||
jsonFormatter.fromJson(request.resultDeserializer, it)
|
content,
|
||||||
} ?: responseObject.parameters?.let {
|
"Can't get result object from $content"
|
||||||
val error = it.error
|
)
|
||||||
if (error is RetryAfterError) {
|
} else {
|
||||||
delay(error.leftToRetry)
|
e
|
||||||
execute(request)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
} ?: response.let {
|
|
||||||
throw newRequestException(
|
|
||||||
responseObject,
|
|
||||||
content,
|
|
||||||
"Can't get result object from $content"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: ClientRequestException) {
|
) {
|
||||||
val content = e.response.readText()
|
requestsLimiter.limit {
|
||||||
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
var statement: HttpStatement? = null
|
||||||
throw newRequestException(
|
for (factory in callsFactories) {
|
||||||
responseObject,
|
statement = factory.prepareCall(
|
||||||
content,
|
client,
|
||||||
"Can't get result object from $content"
|
telegramAPIUrlsKeeper.commonAPIUrl,
|
||||||
)
|
request
|
||||||
} catch (e: Exception) {
|
)
|
||||||
throw e
|
if (statement != null) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val response = statement?.execute() ?: throw IllegalArgumentException("Can't execute request: $request")
|
||||||
|
val content = response.receive<String>()
|
||||||
|
val responseObject = jsonFormatter.parse(Response.serializer(), content)
|
||||||
|
|
||||||
|
(responseObject.result?.let {
|
||||||
|
jsonFormatter.fromJson(request.resultDeserializer, it)
|
||||||
|
} ?: responseObject.parameters?.let {
|
||||||
|
val error = it.error
|
||||||
|
if (error is RetryAfterError) {
|
||||||
|
delay(error.leftToRetry)
|
||||||
|
execute(request)
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} ?: response.let {
|
||||||
|
throw newRequestException(
|
||||||
|
responseObject,
|
||||||
|
content,
|
||||||
|
"Can't get result object from $content"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ fun newRequestException(
|
|||||||
description == "Bad Request: message to edit not found" -> MessageToEditNotFoundException(response, plainAnswer, message, cause)
|
description == "Bad Request: message to edit not found" -> MessageToEditNotFoundException(response, plainAnswer, message, cause)
|
||||||
description.contains("Bad Request: message is not modified") -> MessageIsNotModifiedException(response, plainAnswer, message, cause)
|
description.contains("Bad Request: message is not modified") -> MessageIsNotModifiedException(response, plainAnswer, message, cause)
|
||||||
description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause)
|
description == "Unauthorized" -> UnauthorizedException(response, plainAnswer, message, cause)
|
||||||
|
description.contains("PHOTO_INVALID_DIMENSIONS") -> InvalidPhotoDimensionsException(response, plainAnswer, message, cause)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
} ?: CommonRequestException(response, plainAnswer, message, cause)
|
} ?: CommonRequestException(response, plainAnswer, message, cause)
|
||||||
@@ -41,3 +42,6 @@ class MessageIsNotModifiedException(response: Response, plainAnswer: String, mes
|
|||||||
|
|
||||||
class MessageToEditNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
class MessageToEditNotFoundException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
||||||
RequestException(response, plainAnswer, message, cause)
|
RequestException(response, plainAnswer, message, cause)
|
||||||
|
|
||||||
|
class InvalidPhotoDimensionsException(response: Response, plainAnswer: String, message: String?, cause: Throwable?) :
|
||||||
|
RequestException(response, plainAnswer, message, cause)
|
||||||
|
|||||||
@@ -3,18 +3,18 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutSerialization
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.builtins.ListSerializer
|
import kotlinx.serialization.builtins.ListSerializer
|
||||||
|
|
||||||
private val updatesListSerializer = ListSerializer(
|
private val updatesListSerializer = ListSerializer(
|
||||||
UpdateSerializerWithoutDeserialization
|
UpdateSerializerWithoutSerialization
|
||||||
)
|
)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class GetUpdates(
|
data class GetUpdates(
|
||||||
val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates
|
val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates
|
||||||
val limit: Int? = null,
|
val limit: Int = getUpdatesLimit.last,
|
||||||
val timeout: Seconds? = null,
|
val timeout: Seconds? = null,
|
||||||
val allowed_updates: List<String>? = ALL_UPDATES_LIST
|
val allowed_updates: List<String>? = ALL_UPDATES_LIST
|
||||||
): SimpleRequest<List<Update>> {
|
): SimpleRequest<List<Update>> {
|
||||||
@@ -25,4 +25,10 @@ data class GetUpdates(
|
|||||||
|
|
||||||
override val requestSerializer: SerializationStrategy<*>
|
override val requestSerializer: SerializationStrategy<*>
|
||||||
get() = serializer()
|
get() = serializer()
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (limit !in getUpdatesLimit) {
|
||||||
|
error("GetUpdates request can be called only with limit in range $getUpdatesLimit (actual value is $limit)")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ internal object InputFileSerializer : KSerializer<InputFile> {
|
|||||||
@Serializable(InputFileSerializer::class)
|
@Serializable(InputFileSerializer::class)
|
||||||
data class MultipartFile (
|
data class MultipartFile (
|
||||||
val file: StorageFile,
|
val file: StorageFile,
|
||||||
val mimeType: String = file.contentType,
|
val mimeType: String = file.storageFileInfo.contentType,
|
||||||
val filename: String = file.fileName
|
val filename: String = file.storageFileInfo.fileName
|
||||||
) : InputFile() {
|
) : InputFile() {
|
||||||
override val fileId: String = file.generateCustomName()
|
override val fileId: String = file.storageFileInfo.generateCustomName()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.bot
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.builtins.ListSerializer
|
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ReplyMes
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.dice.DiceAnimationType
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.DiceContent
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.DiceContent
|
||||||
@@ -17,6 +18,8 @@ internal val DiceContentMessageResultDeserializer: DeserializationStrategy<Conte
|
|||||||
data class SendDice(
|
data class SendDice(
|
||||||
@SerialName(chatIdField)
|
@SerialName(chatIdField)
|
||||||
override val chatId: ChatIdentifier,
|
override val chatId: ChatIdentifier,
|
||||||
|
@SerialName(emojiField)
|
||||||
|
val animationType: DiceAnimationType? = null,
|
||||||
@SerialName(disableNotificationField)
|
@SerialName(disableNotificationField)
|
||||||
override val disableNotification: Boolean = false,
|
override val disableNotification: Boolean = false,
|
||||||
@SerialName(replyToMessageIdField)
|
@SerialName(replyToMessageIdField)
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls
|
package com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedOutput
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.justTextSources
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMessageRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMessageRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.PollContent
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.PollContent
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.fullListOfSubSource
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownV2Captions
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
private val commonResultDeserializer: DeserializationStrategy<ContentMessage<PollContent>> = TelegramBotAPIMessageDeserializationStrategyClass()
|
private val commonResultDeserializer: DeserializationStrategy<ContentMessage<PollContent>> = TelegramBotAPIMessageDeserializationStrategyClass()
|
||||||
@@ -66,6 +73,7 @@ fun Poll.createRequest(
|
|||||||
isAnonymous,
|
isAnonymous,
|
||||||
isClosed,
|
isClosed,
|
||||||
allowMultipleAnswers,
|
allowMultipleAnswers,
|
||||||
|
scheduledCloseInfo,
|
||||||
disableNotification,
|
disableNotification,
|
||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
replyMarkup
|
replyMarkup
|
||||||
@@ -78,6 +86,9 @@ fun Poll.createRequest(
|
|||||||
correctOptionId,
|
correctOptionId,
|
||||||
isAnonymous,
|
isAnonymous,
|
||||||
isClosed,
|
isClosed,
|
||||||
|
caption ?.fullListOfSubSource(captionEntities) ?.justTextSources() ?.toMarkdownV2Captions() ?.firstOrNull(),
|
||||||
|
MarkdownV2,
|
||||||
|
scheduledCloseInfo,
|
||||||
disableNotification,
|
disableNotification,
|
||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
replyMarkup
|
replyMarkup
|
||||||
@@ -89,6 +100,7 @@ fun Poll.createRequest(
|
|||||||
isAnonymous,
|
isAnonymous,
|
||||||
isClosed,
|
isClosed,
|
||||||
false,
|
false,
|
||||||
|
scheduledCloseInfo,
|
||||||
disableNotification,
|
disableNotification,
|
||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
replyMarkup
|
replyMarkup
|
||||||
@@ -100,20 +112,35 @@ fun Poll.createRequest(
|
|||||||
isAnonymous,
|
isAnonymous,
|
||||||
isClosed,
|
isClosed,
|
||||||
false,
|
false,
|
||||||
|
scheduledCloseInfo,
|
||||||
disableNotification,
|
disableNotification,
|
||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
replyMarkup
|
replyMarkup
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ScheduledCloseInfo.checkSendData() {
|
||||||
|
val span = when (this) {
|
||||||
|
is ExactScheduledCloseInfo -> (closeDateTime - DateTime.now()).seconds
|
||||||
|
is ApproximateScheduledCloseInfo -> openDuration.seconds
|
||||||
|
}.toInt()
|
||||||
|
if (span !in openPeriodPollSecondsLimit) {
|
||||||
|
error("Duration of autoclose for polls must be in range $openPeriodPollSecondsLimit, but was $span")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sealed class SendPoll : SendMessageRequest<ContentMessage<PollContent>>,
|
sealed class SendPoll : SendMessageRequest<ContentMessage<PollContent>>,
|
||||||
ReplyingMarkupSendMessageRequest<ContentMessage<PollContent>> {
|
ReplyingMarkupSendMessageRequest<ContentMessage<PollContent>> {
|
||||||
abstract val question: String
|
abstract val question: String
|
||||||
abstract val options: List<String>
|
abstract val options: List<String>
|
||||||
abstract val isAnonymous: Boolean
|
abstract val isAnonymous: Boolean
|
||||||
abstract val isClosed: Boolean
|
abstract val isClosed: Boolean
|
||||||
|
abstract val closeInfo: ScheduledCloseInfo?
|
||||||
abstract val type: String
|
abstract val type: String
|
||||||
|
|
||||||
|
internal abstract val openPeriod: LongSeconds?
|
||||||
|
internal abstract val closeDate: LongSeconds?
|
||||||
|
|
||||||
override fun method(): String = "sendPoll"
|
override fun method(): String = "sendPoll"
|
||||||
override val resultDeserializer: DeserializationStrategy<ContentMessage<PollContent>>
|
override val resultDeserializer: DeserializationStrategy<ContentMessage<PollContent>>
|
||||||
get() = commonResultDeserializer
|
get() = commonResultDeserializer
|
||||||
@@ -133,6 +160,8 @@ data class SendRegularPoll(
|
|||||||
override val isClosed: Boolean = false,
|
override val isClosed: Boolean = false,
|
||||||
@SerialName(allowsMultipleAnswersField)
|
@SerialName(allowsMultipleAnswersField)
|
||||||
val allowMultipleAnswers: Boolean = false,
|
val allowMultipleAnswers: Boolean = false,
|
||||||
|
@Transient
|
||||||
|
override val closeInfo: ScheduledCloseInfo? = null,
|
||||||
@SerialName(disableNotificationField)
|
@SerialName(disableNotificationField)
|
||||||
override val disableNotification: Boolean = false,
|
override val disableNotification: Boolean = false,
|
||||||
@SerialName(replyToMessageIdField)
|
@SerialName(replyToMessageIdField)
|
||||||
@@ -144,8 +173,17 @@ data class SendRegularPoll(
|
|||||||
override val requestSerializer: SerializationStrategy<*>
|
override val requestSerializer: SerializationStrategy<*>
|
||||||
get() = serializer()
|
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 {
|
init {
|
||||||
checkPollInfo(question, options)
|
checkPollInfo(question, options)
|
||||||
|
closeInfo ?.checkSendData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,23 +201,42 @@ data class SendQuizPoll(
|
|||||||
override val isAnonymous: Boolean = true,
|
override val isAnonymous: Boolean = true,
|
||||||
@SerialName(isClosedField)
|
@SerialName(isClosedField)
|
||||||
override val isClosed: Boolean = false,
|
override val isClosed: Boolean = false,
|
||||||
|
@SerialName(explanationField)
|
||||||
|
override val caption: String? = null,
|
||||||
|
@SerialName(explanationParseModeField)
|
||||||
|
override val parseMode: ParseMode? = null,
|
||||||
|
@Transient
|
||||||
|
override val closeInfo: ScheduledCloseInfo? = null,
|
||||||
@SerialName(disableNotificationField)
|
@SerialName(disableNotificationField)
|
||||||
override val disableNotification: Boolean = false,
|
override val disableNotification: Boolean = false,
|
||||||
@SerialName(replyToMessageIdField)
|
@SerialName(replyToMessageIdField)
|
||||||
override val replyToMessageId: MessageIdentifier? = null,
|
override val replyToMessageId: MessageIdentifier? = null,
|
||||||
@SerialName(replyMarkupField)
|
@SerialName(replyMarkupField)
|
||||||
override val replyMarkup: KeyboardMarkup? = null
|
override val replyMarkup: KeyboardMarkup? = null
|
||||||
) : SendPoll() {
|
) : SendPoll(), CaptionedOutput {
|
||||||
override val type: String = quizPollType
|
override val type: String = quizPollType
|
||||||
override val requestSerializer: SerializationStrategy<*>
|
override val requestSerializer: SerializationStrategy<*>
|
||||||
get() = serializer()
|
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 {
|
init {
|
||||||
checkPollInfo(question, options)
|
checkPollInfo(question, options)
|
||||||
|
closeInfo ?.checkSendData()
|
||||||
val correctOptionIdRange = 0 .. options.size
|
val correctOptionIdRange = 0 .. options.size
|
||||||
if (correctOptionId !in correctOptionIdRange) {
|
if (correctOptionId !in correctOptionIdRange) {
|
||||||
throw IllegalArgumentException("Correct option id must be in range of $correctOptionIdRange, but actual " +
|
throw IllegalArgumentException("Correct option id must be in range of $correctOptionIdRange, but actual " +
|
||||||
"value is $correctOptionId")
|
"value is $correctOptionId")
|
||||||
}
|
}
|
||||||
|
if (caption != null && caption.length !in quizPollExplanationLimit) {
|
||||||
|
error("Quiz poll explanation size must be in range $quizPollExplanationLimit," +
|
||||||
|
"but actual explanation contains ${caption.length} symbols")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StickerSetAction
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StandardStickerSetAction
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
fun AddAnimatedStickerToSet(
|
fun AddAnimatedStickerToSet(
|
||||||
userId: UserId,
|
userId: UserId,
|
||||||
stickerSetName: String,
|
stickerSetName: String,
|
||||||
@@ -35,7 +36,7 @@ data class AddAnimatedStickerToSet internal constructor(
|
|||||||
val sticker: FileId? = null,
|
val sticker: FileId? = null,
|
||||||
@SerialName(maskPositionField)
|
@SerialName(maskPositionField)
|
||||||
override val maskPosition: MaskPosition? = null
|
override val maskPosition: MaskPosition? = null
|
||||||
) : StickerSetAction {
|
) : StandardStickerSetAction {
|
||||||
init {
|
init {
|
||||||
if(emojis.isEmpty()) {
|
if(emojis.isEmpty()) {
|
||||||
throw IllegalArgumentException("Emojis must not be empty")
|
throw IllegalArgumentException("Emojis must not be empty")
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StickerSetAction
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StandardStickerSetAction
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
fun AddStaticStickerToSet(
|
fun AddStaticStickerToSet(
|
||||||
userId: UserId,
|
userId: UserId,
|
||||||
stickerSetName: String,
|
stickerSetName: String,
|
||||||
@@ -53,7 +54,7 @@ data class AddStaticStickerToSet internal constructor(
|
|||||||
val sticker: FileId? = null,
|
val sticker: FileId? = null,
|
||||||
@SerialName(maskPositionField)
|
@SerialName(maskPositionField)
|
||||||
override val maskPosition: MaskPosition? = null
|
override val maskPosition: MaskPosition? = null
|
||||||
) : StickerSetAction {
|
) : StandardStickerSetAction {
|
||||||
init {
|
init {
|
||||||
if(emojis.isEmpty()) {
|
if(emojis.isEmpty()) {
|
||||||
throw IllegalArgumentException("Emojis must not be empty")
|
throw IllegalArgumentException("Emojis must not be empty")
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StickerSetAction
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StandardStickerSetAction
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
@@ -39,7 +39,7 @@ data class CreateNewAnimatedStickerSet internal constructor(
|
|||||||
val containsMasks: Boolean? = null,
|
val containsMasks: Boolean? = null,
|
||||||
@SerialName(maskPositionField)
|
@SerialName(maskPositionField)
|
||||||
override val maskPosition: MaskPosition? = null
|
override val maskPosition: MaskPosition? = null
|
||||||
) : StickerSetAction {
|
) : StandardStickerSetAction {
|
||||||
init {
|
init {
|
||||||
if(emojis.isEmpty()) {
|
if(emojis.isEmpty()) {
|
||||||
throw IllegalArgumentException("Emojis must not be empty")
|
throw IllegalArgumentException("Emojis must not be empty")
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StickerSetAction
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StandardStickerSetAction
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
@@ -54,7 +54,7 @@ data class CreateNewStaticStickerSet internal constructor(
|
|||||||
val containsMasks: Boolean? = null,
|
val containsMasks: Boolean? = null,
|
||||||
@SerialName(maskPositionField)
|
@SerialName(maskPositionField)
|
||||||
override val maskPosition: MaskPosition? = null
|
override val maskPosition: MaskPosition? = null
|
||||||
) : StickerSetAction {
|
) : StandardStickerSetAction {
|
||||||
init {
|
init {
|
||||||
if(emojis.isEmpty()) {
|
if(emojis.isEmpty()) {
|
||||||
throw IllegalArgumentException("Emojis must not be empty")
|
throw IllegalArgumentException("Emojis must not be empty")
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.common.CommonMultipartFileRequest
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts.StickerSetAction
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
fun SetStickerSetThumb(
|
||||||
|
userId: UserId,
|
||||||
|
stickerSetName: String,
|
||||||
|
thumb: MultipartFile
|
||||||
|
): Request<Boolean> {
|
||||||
|
return CommonMultipartFileRequest(
|
||||||
|
SetStickerSetThumb(userId, stickerSetName),
|
||||||
|
mapOf(thumbField to thumb)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SetStickerSetThumb (
|
||||||
|
@SerialName(userIdField)
|
||||||
|
override val userId: UserId,
|
||||||
|
@SerialName(nameField)
|
||||||
|
override val name: StickerSetName,
|
||||||
|
@SerialName(thumbField)
|
||||||
|
val thumb: FileId? = null
|
||||||
|
) : StickerSetAction {
|
||||||
|
override val requestSerializer: SerializationStrategy<*>
|
||||||
|
get() = serializer()
|
||||||
|
|
||||||
|
override fun method(): String = "setStickerSetThumb"
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
||||||
|
|
||||||
|
interface StandardStickerSetAction : StickerSetAction {
|
||||||
|
val emojis: String // must be more than one
|
||||||
|
val maskPosition: MaskPosition?
|
||||||
|
}
|
||||||
@@ -2,15 +2,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.stickers.abstracts
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UserId
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UserId
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.stickers.MaskPosition
|
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
|
||||||
interface StickerSetAction : SimpleRequest<Boolean> {
|
interface StickerSetAction : SimpleRequest<Boolean> {
|
||||||
val userId: UserId
|
val userId: UserId
|
||||||
val name: String
|
val name: String
|
||||||
val emojis: String // must be more than one
|
|
||||||
val maskPosition: MaskPosition?
|
|
||||||
|
|
||||||
override val resultDeserializer: KSerializer<Boolean>
|
override val resultDeserializer: KSerializer<Boolean>
|
||||||
get() = Boolean.serializer()
|
get() = Boolean.serializer()
|
||||||
|
|||||||
@@ -9,4 +9,13 @@ data class BotCommand(
|
|||||||
val command: String,
|
val command: String,
|
||||||
@SerialName(descriptionField)
|
@SerialName(descriptionField)
|
||||||
val description: String
|
val description: String
|
||||||
)
|
) {
|
||||||
|
init {
|
||||||
|
if (command.length !in botCommandLengthLimit) {
|
||||||
|
error("Command size must be in range $botCommandLengthLimit, but actually have length ${command.length}")
|
||||||
|
}
|
||||||
|
if (description.length !in botCommandDescriptionLimit) {
|
||||||
|
error("Command description size must be in range $botCommandDescriptionLimit, but actually have length ${description.length}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,7 +22,9 @@ typealias FileUniqueId = String
|
|||||||
typealias DiceResult = Int
|
typealias DiceResult = Int
|
||||||
|
|
||||||
typealias Seconds = Int
|
typealias Seconds = Int
|
||||||
|
typealias LongSeconds = Long
|
||||||
|
|
||||||
|
val getUpdatesLimit = 1 .. 100
|
||||||
val callbackQueryAnswerLength = 0 until 200
|
val callbackQueryAnswerLength = 0 until 200
|
||||||
val captionLength = 0 until 1024
|
val captionLength = 0 until 1024
|
||||||
val textLength = 0 until 4096
|
val textLength = 0 until 4096
|
||||||
@@ -48,10 +50,15 @@ val customTitleLength = 0 .. 16
|
|||||||
|
|
||||||
val diceResultLimit = 1 .. 6
|
val diceResultLimit = 1 .. 6
|
||||||
|
|
||||||
val botCommandLimit = 1 .. 32
|
val botCommandLengthLimit = 1 .. 32
|
||||||
|
val botCommandLimit = botCommandLengthLimit
|
||||||
val botCommandDescriptionLimit = 3 .. 256
|
val botCommandDescriptionLimit = 3 .. 256
|
||||||
val botCommandsLimit = 0 .. 100
|
val botCommandsLimit = 0 .. 100
|
||||||
|
|
||||||
|
val quizPollExplanationLimit = 0 .. 200
|
||||||
|
|
||||||
|
val openPeriodPollSecondsLimit = 5 .. 600
|
||||||
|
|
||||||
const val chatIdField = "chat_id"
|
const val chatIdField = "chat_id"
|
||||||
const val messageIdField = "message_id"
|
const val messageIdField = "message_id"
|
||||||
const val updateIdField = "update_id"
|
const val updateIdField = "update_id"
|
||||||
@@ -80,6 +87,7 @@ const val containsMasksField = "contains_masks"
|
|||||||
const val resultIdField = "result_id"
|
const val resultIdField = "result_id"
|
||||||
const val inlineMessageIdField = "inline_message_id"
|
const val inlineMessageIdField = "inline_message_id"
|
||||||
const val callbackDataField = "callback_data"
|
const val callbackDataField = "callback_data"
|
||||||
|
const val callbackGameField = "callback_game"
|
||||||
const val callbackQueryIdField = "callback_query_id"
|
const val callbackQueryIdField = "callback_query_id"
|
||||||
const val inlineQueryIdField = "inline_query_id"
|
const val inlineQueryIdField = "inline_query_id"
|
||||||
const val inlineKeyboardField = "inline_keyboard"
|
const val inlineKeyboardField = "inline_keyboard"
|
||||||
@@ -196,6 +204,7 @@ const val tgsStickerField = "tgs_sticker"
|
|||||||
|
|
||||||
const val okField = "ok"
|
const val okField = "ok"
|
||||||
const val captionField = "caption"
|
const val captionField = "caption"
|
||||||
|
const val explanationField = "explanation"
|
||||||
const val idField = "id"
|
const val idField = "id"
|
||||||
const val pollIdField = "poll_id"
|
const val pollIdField = "poll_id"
|
||||||
const val textField = "text"
|
const val textField = "text"
|
||||||
@@ -247,6 +256,10 @@ const val xShiftField = "x_shift"
|
|||||||
const val yShiftField = "y_shift"
|
const val yShiftField = "y_shift"
|
||||||
const val scaleField = "scale"
|
const val scaleField = "scale"
|
||||||
|
|
||||||
|
const val explanationEntitiesField = "explanation_entities"
|
||||||
|
const val explanationParseModeField = "explanation_parse_mode"
|
||||||
|
const val openPeriodField = "open_period"
|
||||||
|
const val closeDateField = "close_date"
|
||||||
|
|
||||||
const val smallFileIdField = "small_file_id"
|
const val smallFileIdField = "small_file_id"
|
||||||
const val bigFileIdField = "big_file_id"
|
const val bigFileIdField = "big_file_id"
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types
|
package com.github.insanusmokrassar.TelegramBotAPI.types
|
||||||
|
|
||||||
import kotlinx.serialization.SerialName
|
import com.github.insanusmokrassar.TelegramBotAPI.types.dice.Dice
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
@Deprecated(
|
||||||
data class Dice(
|
"Replaced",
|
||||||
@SerialName(valueField)
|
ReplaceWith("Dice", "com.github.insanusmokrassar.TelegramBotAPI.types.dice.Dice")
|
||||||
val value: DiceResult
|
|
||||||
)
|
)
|
||||||
|
typealias Dice = Dice
|
||||||
|
|||||||
@@ -83,6 +83,28 @@ internal fun createTextPart(from: String, entities: RawMessageEntities): List<Te
|
|||||||
return resultList
|
return resultList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun List<TextPart>.asRawMessageEntities() = mapNotNull {
|
||||||
|
val source = it.source
|
||||||
|
when (source) {
|
||||||
|
is MentionTextSource -> RawMessageEntity("mention", it.range.first, it.range.last - it.range.first)
|
||||||
|
is HashTagTextSource -> RawMessageEntity("hashtag", it.range.first, it.range.last - it.range.first)
|
||||||
|
is CashTagTextSource -> RawMessageEntity("cashtag", it.range.first, it.range.last - it.range.first)
|
||||||
|
is BotCommandTextSource -> RawMessageEntity("bot_command", it.range.first, it.range.last - it.range.first)
|
||||||
|
is URLTextSource -> RawMessageEntity("url", it.range.first, it.range.last - it.range.first)
|
||||||
|
is EMailTextSource -> RawMessageEntity("email", it.range.first, it.range.last - it.range.first)
|
||||||
|
is PhoneNumberTextSource -> RawMessageEntity("phone_number", it.range.first, it.range.last - it.range.first)
|
||||||
|
is BoldTextSource -> RawMessageEntity("bold", it.range.first, it.range.last - it.range.first)
|
||||||
|
is ItalicTextSource -> RawMessageEntity("italic", it.range.first, it.range.last - it.range.first)
|
||||||
|
is CodeTextSource -> RawMessageEntity("code", it.range.first, it.range.last - it.range.first)
|
||||||
|
is PreTextSource -> RawMessageEntity("pre", it.range.first, it.range.last - it.range.first, language = source.language)
|
||||||
|
is TextLinkTextSource -> RawMessageEntity("text_link", it.range.first, it.range.last - it.range.first, source.url)
|
||||||
|
is TextMentionTextSource -> RawMessageEntity("text_mention", it.range.first, it.range.last - it.range.first, user = source.user)
|
||||||
|
is UnderlineTextSource -> RawMessageEntity("underline", it.range.first, it.range.last - it.range.first)
|
||||||
|
is StrikethroughTextSource -> RawMessageEntity("strikethrough", it.range.first, it.range.last - it.range.first)
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal fun RawMessageEntities.asTextParts(sourceString: String): List<TextPart> = createTextPart(sourceString, this)
|
internal fun RawMessageEntities.asTextParts(sourceString: String): List<TextPart> = createTextPart(sourceString, this)
|
||||||
|
|
||||||
internal typealias RawMessageEntities = List<RawMessageEntity>
|
internal typealias RawMessageEntities = List<RawMessageEntity>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class BoldTextSource(
|
class BoldTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
|||||||
private val commandRegex = Regex("[/!][^@\\s]*")
|
private val commandRegex = Regex("[/!][^@\\s]*")
|
||||||
|
|
||||||
class BotCommandTextSource(
|
class BotCommandTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
val command: String by lazy {
|
val command: String by lazy {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class CashTagTextSource(
|
class CashTagTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class CodeTextSource(
|
class CodeTextSource(
|
||||||
source: String
|
override val source: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String by lazy { source.codeMarkdown() }
|
override val asMarkdownSource: String by lazy { source.codeMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { source.codeMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { source.codeMarkdownV2() }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class EMailTextSource(
|
class EMailTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ private val String.withoutSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
class HashTagTextSource(
|
class HashTagTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy {
|
override val textParts: List<TextPart> by lazy {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class ItalicTextSource(
|
class ItalicTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ private val String.withoutCommercialAt
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MentionTextSource(
|
class MentionTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy {
|
override val textParts: List<TextPart> by lazy {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class PhoneNumberTextSource(
|
class PhoneNumberTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class PreTextSource(
|
class PreTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
val language: String? = null
|
val language: String? = null
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String by lazy { source.preMarkdown(language) }
|
override val asMarkdownSource: String by lazy { source.preMarkdown(language) }
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class RegularTextSource(
|
class RegularTextSource(
|
||||||
source: String
|
override val source: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String by lazy { source.regularMarkdown() }
|
override val asMarkdownSource: String by lazy { source.regularMarkdown() }
|
||||||
override val asMarkdownV2Source: String by lazy { source.regularMarkdownV2() }
|
override val asMarkdownV2Source: String by lazy { source.regularMarkdownV2() }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class StrikethroughTextSource(
|
class StrikethroughTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class TextLinkTextSource(
|
class TextLinkTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
url: String
|
val url: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String by lazy { source.linkMarkdown(url) }
|
override val asMarkdownSource: String by lazy { source.linkMarkdown(url) }
|
||||||
override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(url) }
|
override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(url) }
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsourc
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.MultilevelTextSource
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat
|
import com.github.insanusmokrassar.TelegramBotAPI.types.User
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class TextMentionTextSource(
|
class TextMentionTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
privateChat: PrivateChat,
|
val user: User,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
override val asMarkdownSource: String by lazy { source.textMentionMarkdown(privateChat.id) }
|
override val asMarkdownSource: String by lazy { source.textMentionMarkdown(user.id) }
|
||||||
override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(privateChat.id) }
|
override val asMarkdownV2Source: String by lazy { textMentionMarkdownV2(user.id) }
|
||||||
override val asHtmlSource: String by lazy { textMentionHTML(privateChat.id) }
|
override val asHtmlSource: String by lazy { textMentionHTML(user.id) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextSource
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class URLTextSource(
|
class URLTextSource(
|
||||||
source: String
|
override val source: String
|
||||||
) : TextSource {
|
) : TextSource {
|
||||||
override val asMarkdownSource: String by lazy { source.linkMarkdown(source) }
|
override val asMarkdownSource: String by lazy { source.linkMarkdown(source) }
|
||||||
override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(source) }
|
override val asMarkdownV2Source: String by lazy { source.linkMarkdownV2(source) }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
|
|
||||||
class UnderlineTextSource(
|
class UnderlineTextSource(
|
||||||
source: String,
|
override val source: String,
|
||||||
textParts: List<TextPart>
|
textParts: List<TextPart>
|
||||||
) : MultilevelTextSource {
|
) : MultilevelTextSource {
|
||||||
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
override val textParts: List<TextPart> by lazy { source.fullListOfSubSource(textParts) }
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
|
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.games.CallbackGame
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
|
||||||
@Serializable(InlineKeyboardButtonSerializer::class)
|
@Serializable(InlineKeyboardButtonSerializer::class)
|
||||||
sealed class InlineKeyboardButton {
|
sealed class InlineKeyboardButton {
|
||||||
abstract val text: String
|
abstract val text: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class UnknownInlineKeyboardButton internal constructor(
|
||||||
|
override val text: String,
|
||||||
|
val rawData: JsonElement
|
||||||
|
) : InlineKeyboardButton()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class PayInlineKeyboardButton(
|
data class PayInlineKeyboardButton(
|
||||||
override val text: String,
|
override val text: String,
|
||||||
@@ -24,6 +32,15 @@ data class CallbackDataInlineKeyboardButton(
|
|||||||
val callbackData: String
|
val callbackData: String
|
||||||
) : InlineKeyboardButton()
|
) : InlineKeyboardButton()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class CallbackGameInlineKeyboardButton(
|
||||||
|
@SerialName(textField)
|
||||||
|
override val text: String
|
||||||
|
) : InlineKeyboardButton() {
|
||||||
|
@SerialName(callbackGameField)
|
||||||
|
private val callbackGame = CallbackGame
|
||||||
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class LoginURLInlineKeyboardButton(
|
data class LoginURLInlineKeyboardButton(
|
||||||
override val text: String,
|
override val text: String,
|
||||||
|
|||||||
@@ -12,22 +12,25 @@ internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButto
|
|||||||
PolymorphicKind.SEALED
|
PolymorphicKind.SEALED
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton> {
|
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton>? {
|
||||||
return when {
|
return when {
|
||||||
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
|
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
|
||||||
|
json[callbackGameField] != null -> CallbackGameInlineKeyboardButton.serializer()
|
||||||
json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
|
json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
|
||||||
json[payField] != null -> PayInlineKeyboardButton.serializer()
|
json[payField] != null -> PayInlineKeyboardButton.serializer()
|
||||||
json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
|
json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
|
||||||
json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer()
|
json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer()
|
||||||
json[urlField] != null -> URLInlineKeyboardButton.serializer()
|
json[urlField] != null -> URLInlineKeyboardButton.serializer()
|
||||||
else -> throw IllegalArgumentException("Can't find correct serializer for inline button serialized as $json")
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
|
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
|
||||||
val json = JsonElementSerializer.deserialize(decoder)
|
val json = JsonElementSerializer.deserialize(decoder)
|
||||||
|
|
||||||
return nonstrictJsonFormat.fromJson(resolveSerializer(json.jsonObject), json)
|
return (json as? JsonObject) ?.let { resolveSerializer(it) } ?.let {
|
||||||
|
nonstrictJsonFormat.fromJson(it, json)
|
||||||
|
} ?: UnknownInlineKeyboardButton("", json)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
|
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
|
||||||
@@ -38,6 +41,8 @@ internal object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButto
|
|||||||
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
|
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||||
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
|
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||||
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
|
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||||
|
is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value)
|
||||||
|
is UnknownInlineKeyboardButton -> JsonElementSerializer.serialize(encoder, value.rawData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types.dice
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Dice(
|
||||||
|
@SerialName(valueField)
|
||||||
|
val value: DiceResult,
|
||||||
|
@SerialName(emojiField)
|
||||||
|
val animationType: DiceAnimationType
|
||||||
|
)
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types.dice
|
||||||
|
|
||||||
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
|
@Serializable(DiceAnimationTypeSerializer::class)
|
||||||
|
sealed class DiceAnimationType {
|
||||||
|
abstract val emoji: String
|
||||||
|
}
|
||||||
|
@Serializable(DiceAnimationTypeSerializer::class)
|
||||||
|
object CubeDiceAnimationType : DiceAnimationType() {
|
||||||
|
override val emoji: String = "\uD83C\uDFB2"
|
||||||
|
}
|
||||||
|
@Serializable(DiceAnimationTypeSerializer::class)
|
||||||
|
object DartsDiceAnimationType : DiceAnimationType() {
|
||||||
|
override val emoji: String = "\uD83C\uDFAF"
|
||||||
|
}
|
||||||
|
@Serializable(DiceAnimationTypeSerializer::class)
|
||||||
|
class UnknownDiceAnimationType(
|
||||||
|
override val emoji: String
|
||||||
|
) : DiceAnimationType()
|
||||||
|
|
||||||
|
@Serializer(DiceAnimationType::class)
|
||||||
|
internal object DiceAnimationTypeSerializer : KSerializer<DiceAnimationType> {
|
||||||
|
override val descriptor: SerialDescriptor = PrimitiveDescriptor("DiceAnimationType", PrimitiveKind.STRING)
|
||||||
|
override fun deserialize(decoder: Decoder): DiceAnimationType {
|
||||||
|
return when (val type = decoder.decodeString()) {
|
||||||
|
CubeDiceAnimationType.emoji -> CubeDiceAnimationType
|
||||||
|
DartsDiceAnimationType.emoji -> DartsDiceAnimationType
|
||||||
|
else -> UnknownDiceAnimationType(type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, value: DiceAnimationType) {
|
||||||
|
encoder.encodeString(value.emoji)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,4 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.games
|
|||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
class CallbackGame {
|
object CallbackGame
|
||||||
init {
|
|
||||||
TODO()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessage
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.asTextParts
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.asTextParts
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.dice.Dice
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame
|
import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
|
||||||
@@ -19,7 +20,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.Success
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.Invoice
|
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.Invoice
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.SuccessfulPayment
|
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.SuccessfulPayment
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
// TODO:: add PassportData type
|
// TODO:: add PassportData type
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content
|
|||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendDice
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendDice
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.dice.Dice
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
||||||
|
|
||||||
@@ -15,5 +17,11 @@ data class DiceContent(
|
|||||||
disableNotification: Boolean,
|
disableNotification: Boolean,
|
||||||
replyToMessageId: MessageIdentifier?,
|
replyToMessageId: MessageIdentifier?,
|
||||||
replyMarkup: KeyboardMarkup?
|
replyMarkup: KeyboardMarkup?
|
||||||
): Request<ContentMessage<DiceContent>> = SendDice(chatId, disableNotification, replyToMessageId, replyMarkup)
|
): Request<ContentMessage<DiceContent>> = SendDice(
|
||||||
|
chatId,
|
||||||
|
dice.animationType,
|
||||||
|
disableNotification,
|
||||||
|
replyToMessageId,
|
||||||
|
replyMarkup
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,40 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.polls
|
package com.github.insanusmokrassar.TelegramBotAPI.types.polls
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.CaptionedInput
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.TextPart
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.TimeSpan
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.builtins.ListSerializer
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.JsonObjectSerializer
|
||||||
|
|
||||||
|
sealed class ScheduledCloseInfo {
|
||||||
|
abstract val closeDateTime: DateTime
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ExactScheduledCloseInfo(
|
||||||
|
override val closeDateTime: DateTime
|
||||||
|
) : ScheduledCloseInfo()
|
||||||
|
|
||||||
|
data class ApproximateScheduledCloseInfo(
|
||||||
|
val openDuration: TimeSpan,
|
||||||
|
@Suppress("MemberVisibilityCanBePrivate")
|
||||||
|
val startPoint: DateTime = DateTime.now()
|
||||||
|
) : ScheduledCloseInfo() {
|
||||||
|
override val closeDateTime: DateTime = startPoint + openDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
val LongSeconds.asApproximateScheduledCloseInfo
|
||||||
|
get() = ApproximateScheduledCloseInfo(
|
||||||
|
TimeSpan(this * 1000.0)
|
||||||
|
)
|
||||||
|
val LongSeconds.asExactScheduledCloseInfo
|
||||||
|
get() = ExactScheduledCloseInfo(
|
||||||
|
DateTime(unixMillis = this * 1000.0)
|
||||||
|
)
|
||||||
|
|
||||||
@Serializable(PollSerializer::class)
|
@Serializable(PollSerializer::class)
|
||||||
sealed class Poll {
|
sealed class Poll {
|
||||||
@@ -14,6 +44,44 @@ sealed class Poll {
|
|||||||
abstract val votesCount: Int
|
abstract val votesCount: Int
|
||||||
abstract val isClosed: Boolean
|
abstract val isClosed: Boolean
|
||||||
abstract val isAnonymous: Boolean
|
abstract val isAnonymous: Boolean
|
||||||
|
abstract val scheduledCloseInfo: ScheduledCloseInfo?
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable(PollSerializer::class)
|
||||||
|
sealed class MultipleAnswersPoll : Poll()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
private class RawPoll(
|
||||||
|
@SerialName(idField)
|
||||||
|
val id: PollIdentifier,
|
||||||
|
@SerialName(questionField)
|
||||||
|
val question: String,
|
||||||
|
@SerialName(optionsField)
|
||||||
|
val options: List<PollOption>,
|
||||||
|
@SerialName(totalVoterCountField)
|
||||||
|
val votesCount: Int,
|
||||||
|
@SerialName(isClosedField)
|
||||||
|
val isClosed: Boolean = false,
|
||||||
|
@SerialName(isAnonymousField)
|
||||||
|
val isAnonymous: Boolean = false,
|
||||||
|
@SerialName(typeField)
|
||||||
|
val type: String,
|
||||||
|
@SerialName(allowsMultipleAnswersField)
|
||||||
|
val allowMultipleAnswers: Boolean = false,
|
||||||
|
@SerialName(correctOptionIdField)
|
||||||
|
val correctOptionId: Int? = null,
|
||||||
|
@SerialName(explanationField)
|
||||||
|
val caption: String? = null,
|
||||||
|
@SerialName(explanationEntitiesField)
|
||||||
|
val captionEntities: List<RawMessageEntity> = emptyList(),
|
||||||
|
@SerialName(openPeriodField)
|
||||||
|
val openPeriod: LongSeconds? = null,
|
||||||
|
@SerialName(closeDateField)
|
||||||
|
val closeDate: LongSeconds? = null
|
||||||
|
) {
|
||||||
|
@Transient
|
||||||
|
val scheduledCloseInfo: ScheduledCloseInfo?
|
||||||
|
= closeDate ?.asExactScheduledCloseInfo ?: openPeriod ?.asApproximateScheduledCloseInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@@ -30,91 +98,124 @@ data class UnknownPollType internal constructor(
|
|||||||
override val isClosed: Boolean = false,
|
override val isClosed: Boolean = false,
|
||||||
@SerialName(isAnonymousField)
|
@SerialName(isAnonymousField)
|
||||||
override val isAnonymous: Boolean = false,
|
override val isAnonymous: Boolean = false,
|
||||||
val raw: String
|
@Serializable
|
||||||
) : Poll()
|
val raw: JsonObject
|
||||||
|
) : Poll() {
|
||||||
|
@Transient
|
||||||
|
override val scheduledCloseInfo: ScheduledCloseInfo? = raw.getPrimitiveOrNull(
|
||||||
|
closeDateField
|
||||||
|
) ?.longOrNull ?.asExactScheduledCloseInfo ?: raw.getPrimitiveOrNull(
|
||||||
|
openPeriodField
|
||||||
|
) ?.longOrNull ?.asApproximateScheduledCloseInfo
|
||||||
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable(PollSerializer::class)
|
||||||
data class RegularPoll(
|
data class RegularPoll(
|
||||||
@SerialName(idField)
|
|
||||||
override val id: PollIdentifier,
|
override val id: PollIdentifier,
|
||||||
@SerialName(questionField)
|
|
||||||
override val question: String,
|
override val question: String,
|
||||||
@SerialName(optionsField)
|
|
||||||
override val options: List<PollOption>,
|
override val options: List<PollOption>,
|
||||||
@SerialName(totalVoterCountField)
|
|
||||||
override val votesCount: Int,
|
override val votesCount: Int,
|
||||||
@SerialName(isClosedField)
|
|
||||||
override val isClosed: Boolean = false,
|
override val isClosed: Boolean = false,
|
||||||
@SerialName(isAnonymousField)
|
|
||||||
override val isAnonymous: Boolean = false,
|
override val isAnonymous: Boolean = false,
|
||||||
@SerialName(allowsMultipleAnswersField)
|
val allowMultipleAnswers: Boolean = false,
|
||||||
val allowMultipleAnswers: Boolean = false
|
override val scheduledCloseInfo: ScheduledCloseInfo? = null
|
||||||
) : Poll()
|
) : MultipleAnswersPoll()
|
||||||
|
|
||||||
@Serializable
|
@Serializable(PollSerializer::class)
|
||||||
data class QuizPoll(
|
data class QuizPoll(
|
||||||
@SerialName(idField)
|
|
||||||
override val id: PollIdentifier,
|
override val id: PollIdentifier,
|
||||||
@SerialName(questionField)
|
|
||||||
override val question: String,
|
override val question: String,
|
||||||
@SerialName(optionsField)
|
|
||||||
override val options: List<PollOption>,
|
override val options: List<PollOption>,
|
||||||
@SerialName(totalVoterCountField)
|
|
||||||
override val votesCount: Int,
|
override val votesCount: Int,
|
||||||
/**
|
/**
|
||||||
* Nullable due to documentation (https://core.telegram.org/bots/api#poll)
|
* Nullable due to documentation (https://core.telegram.org/bots/api#poll)
|
||||||
*/
|
*/
|
||||||
@SerialName(correctOptionIdField)
|
|
||||||
val correctOptionId: Int? = null,
|
val correctOptionId: Int? = null,
|
||||||
@SerialName(isClosedField)
|
override val caption: String? = null,
|
||||||
|
override val captionEntities: List<TextPart> = emptyList(),
|
||||||
override val isClosed: Boolean = false,
|
override val isClosed: Boolean = false,
|
||||||
@SerialName(isAnonymousField)
|
override val isAnonymous: Boolean = false,
|
||||||
override val isAnonymous: Boolean = false
|
override val scheduledCloseInfo: ScheduledCloseInfo? = null
|
||||||
) : Poll()
|
) : Poll(), CaptionedInput
|
||||||
|
|
||||||
@Serializer(Poll::class)
|
@Serializer(Poll::class)
|
||||||
internal object PollSerializer : KSerializer<Poll> {
|
internal object PollSerializer : KSerializer<Poll> {
|
||||||
private val pollOptionsSerializer = ListSerializer(PollOption.serializer())
|
override val descriptor: SerialDescriptor
|
||||||
|
get() = RawPoll.serializer().descriptor
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): Poll {
|
override fun deserialize(decoder: Decoder): Poll {
|
||||||
val asJson = JsonObjectSerializer.deserialize(decoder)
|
val asJson = JsonObjectSerializer.deserialize(decoder)
|
||||||
|
val rawPoll = nonstrictJsonFormat.fromJson(RawPoll.serializer(), asJson)
|
||||||
|
|
||||||
return when (asJson.getPrimitive(typeField).content) {
|
return when (rawPoll.type) {
|
||||||
regularPollType -> nonstrictJsonFormat.fromJson(
|
quizPollType -> QuizPoll(
|
||||||
RegularPoll.serializer(),
|
rawPoll.id,
|
||||||
asJson
|
rawPoll.question,
|
||||||
|
rawPoll.options,
|
||||||
|
rawPoll.votesCount,
|
||||||
|
rawPoll.correctOptionId,
|
||||||
|
rawPoll.caption,
|
||||||
|
rawPoll.caption?.let { rawPoll.captionEntities.asTextParts(it) } ?: emptyList(),
|
||||||
|
rawPoll.isClosed,
|
||||||
|
rawPoll.isAnonymous,
|
||||||
|
rawPoll.scheduledCloseInfo
|
||||||
)
|
)
|
||||||
quizPollType -> nonstrictJsonFormat.fromJson(
|
regularPollType -> RegularPoll(
|
||||||
QuizPoll.serializer(),
|
rawPoll.id,
|
||||||
asJson
|
rawPoll.question,
|
||||||
|
rawPoll.options,
|
||||||
|
rawPoll.votesCount,
|
||||||
|
rawPoll.isClosed,
|
||||||
|
rawPoll.isAnonymous,
|
||||||
|
rawPoll.allowMultipleAnswers,
|
||||||
|
rawPoll.scheduledCloseInfo
|
||||||
)
|
)
|
||||||
else -> UnknownPollType(
|
else -> UnknownPollType(
|
||||||
asJson.getPrimitive(idField).content,
|
rawPoll.id,
|
||||||
asJson.getPrimitive(questionField).content,
|
rawPoll.question,
|
||||||
nonstrictJsonFormat.fromJson(
|
rawPoll.options,
|
||||||
pollOptionsSerializer,
|
rawPoll.votesCount,
|
||||||
asJson.getArray(optionsField)
|
rawPoll.isClosed,
|
||||||
),
|
rawPoll.isAnonymous,
|
||||||
asJson.getPrimitive(totalVoterCountField).int,
|
asJson
|
||||||
asJson.getPrimitiveOrNull(isClosedField) ?.booleanOrNull ?: false,
|
|
||||||
asJson.getPrimitiveOrNull(isAnonymousField) ?.booleanOrNull ?: true,
|
|
||||||
asJson.toString()
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: Poll) {
|
override fun serialize(encoder: Encoder, value: Poll) {
|
||||||
val asJson = when (value) {
|
val closeInfo = value.scheduledCloseInfo
|
||||||
is RegularPoll -> nonstrictJsonFormat.toJson(RegularPoll.serializer(), value)
|
val rawPoll = when (value) {
|
||||||
is QuizPoll -> nonstrictJsonFormat.toJson(QuizPoll.serializer(), value)
|
is RegularPoll -> RawPoll(
|
||||||
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $value")
|
value.id,
|
||||||
|
value.question,
|
||||||
|
value.options,
|
||||||
|
value.votesCount,
|
||||||
|
value.isClosed,
|
||||||
|
value.isAnonymous,
|
||||||
|
regularPollType,
|
||||||
|
value.allowMultipleAnswers,
|
||||||
|
openPeriod = (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.seconds ?.toLong(),
|
||||||
|
closeDate = (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000L)
|
||||||
|
)
|
||||||
|
is QuizPoll -> RawPoll(
|
||||||
|
value.id,
|
||||||
|
value.question,
|
||||||
|
value.options,
|
||||||
|
value.votesCount,
|
||||||
|
value.isClosed,
|
||||||
|
value.isAnonymous,
|
||||||
|
regularPollType,
|
||||||
|
correctOptionId = value.correctOptionId,
|
||||||
|
caption = value.caption,
|
||||||
|
captionEntities = value.captionEntities.asRawMessageEntities(),
|
||||||
|
openPeriod = (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.seconds ?.toLong(),
|
||||||
|
closeDate = (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000L)
|
||||||
|
)
|
||||||
|
is UnknownPollType -> {
|
||||||
|
JsonObjectSerializer.serialize(encoder, value.raw)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val resultJson = JsonObject(
|
RawPoll.serializer().serialize(encoder, rawPoll)
|
||||||
asJson.jsonObject + (typeField to when (value) {
|
|
||||||
is RegularPoll -> JsonPrimitive(regularPollType)
|
|
||||||
is QuizPoll -> JsonPrimitive(quizPollType)
|
|
||||||
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $value")
|
|
||||||
})
|
|
||||||
)
|
|
||||||
JsonObjectSerializer.serialize(encoder, resultJson)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
||||||
|
|
||||||
data class EditChannelPostUpdate(
|
data class EditChannelPostUpdate(
|
||||||
override val updateId: UpdateIdentifier,
|
override val updateId: UpdateIdentifier,
|
||||||
override val data: Message
|
override val data: CommonMessage<*>
|
||||||
) : BaseEditMessageUpdate
|
) : BaseEditMessageUpdate
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
package com.github.insanusmokrassar.TelegramBotAPI.types.update
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate
|
||||||
|
|
||||||
data class EditMessageUpdate(
|
data class EditMessageUpdate(
|
||||||
override val updateId: UpdateIdentifier,
|
override val updateId: UpdateIdentifier,
|
||||||
override val data: Message
|
override val data: CommonMessage<*>
|
||||||
) : BaseEditMessageUpdate
|
) : BaseEditMessageUpdate
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates
|
package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|
||||||
|
|
||||||
interface MediaGroupUpdate : Update
|
interface MediaGroupUpdate : Update
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ interface SentMediaGroupUpdate: MediaGroupUpdate {
|
|||||||
val origins: List<BaseMessageUpdate>
|
val origins: List<BaseMessageUpdate>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditMediaGroupUpdate : MediaGroupUpdate {
|
interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate {
|
||||||
override val data: MediaGroupMessage
|
override val data: MediaGroupMessage
|
||||||
val origin: BaseMessageUpdate
|
val origin: BaseMessageUpdate
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.RawCallbac
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult
|
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.RawInlineQuery
|
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.RawInlineQuery
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
|
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
|
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
||||||
@@ -13,8 +12,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@@ -22,11 +20,11 @@ internal data class RawUpdate constructor(
|
|||||||
@SerialName(updateIdField)
|
@SerialName(updateIdField)
|
||||||
val updateId: UpdateIdentifier,
|
val updateId: UpdateIdentifier,
|
||||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||||
private val edited_message: Message? = null,
|
private val edited_message: CommonMessage<*>? = null,
|
||||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||||
private val message: Message? = null,
|
private val message: Message? = null,
|
||||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||||
private val edited_channel_post: Message? = null,
|
private val edited_channel_post: CommonMessage<*>? = null,
|
||||||
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
|
||||||
private val channel_post: Message? = null,
|
private val channel_post: Message? = null,
|
||||||
private val inline_query: RawInlineQuery? = null,
|
private val inline_query: RawInlineQuery? = null,
|
||||||
@@ -42,26 +40,39 @@ internal data class RawUpdate constructor(
|
|||||||
* @return One of children of [Update] interface or null in case of unknown type of update
|
* @return One of children of [Update] interface or null in case of unknown type of update
|
||||||
*/
|
*/
|
||||||
fun asUpdate(raw: JsonElement): Update {
|
fun asUpdate(raw: JsonElement): Update {
|
||||||
return initedUpdate ?: when {
|
return initedUpdate ?: try {
|
||||||
edited_message != null -> EditMessageUpdate(updateId, edited_message)
|
when {
|
||||||
message != null -> MessageUpdate(updateId, message)
|
edited_message != null -> EditMessageUpdate(updateId, edited_message)
|
||||||
edited_channel_post != null -> EditChannelPostUpdate(updateId, edited_channel_post)
|
message != null -> MessageUpdate(updateId, message)
|
||||||
channel_post != null -> ChannelPostUpdate(updateId, channel_post)
|
edited_channel_post != null -> EditChannelPostUpdate(updateId, edited_channel_post)
|
||||||
|
channel_post != null -> ChannelPostUpdate(updateId, channel_post)
|
||||||
|
|
||||||
chosen_inline_result != null -> ChosenInlineResultUpdate(updateId, chosen_inline_result.asChosenInlineResult)
|
chosen_inline_result != null -> ChosenInlineResultUpdate(updateId, chosen_inline_result.asChosenInlineResult)
|
||||||
inline_query != null -> InlineQueryUpdate(updateId, inline_query.asInlineQuery)
|
inline_query != null -> InlineQueryUpdate(updateId, inline_query.asInlineQuery)
|
||||||
callback_query != null -> CallbackQueryUpdate(
|
callback_query != null -> CallbackQueryUpdate(
|
||||||
updateId,
|
updateId,
|
||||||
callback_query.asCallbackQuery(raw.jsonObject["callback_query"].toString())
|
callback_query.asCallbackQuery(raw.jsonObject["callback_query"].toString())
|
||||||
)
|
)
|
||||||
shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query)
|
shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query)
|
||||||
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
|
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
|
||||||
poll != null -> PollUpdate(updateId, poll)
|
poll != null -> PollUpdate(updateId, poll)
|
||||||
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
||||||
else -> UnknownUpdateType(
|
else -> UnknownUpdateType(
|
||||||
updateId,
|
updateId,
|
||||||
raw.toString()
|
raw.toString(),
|
||||||
)
|
raw
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (e: Error) {
|
||||||
|
when (e) {
|
||||||
|
is SerializationException,
|
||||||
|
is NotImplementedError -> UnknownUpdateType(
|
||||||
|
updateId,
|
||||||
|
raw.toString(),
|
||||||
|
raw
|
||||||
|
)
|
||||||
|
else -> throw e
|
||||||
|
}
|
||||||
}.also {
|
}.also {
|
||||||
initedUpdate = it
|
initedUpdate = it
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
||||||
|
|
||||||
interface BaseEditMessageUpdate : BaseMessageUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||||
|
|
||||||
|
interface BaseEditMessageUpdate : BaseMessageUpdate {
|
||||||
|
override val data: CommonMessage<*>
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.json.JsonElement
|
||||||
import kotlinx.serialization.json.JsonElementSerializer
|
import kotlinx.serialization.json.JsonElementSerializer
|
||||||
|
|
||||||
interface Update {
|
interface Update {
|
||||||
@@ -13,10 +14,11 @@ interface Update {
|
|||||||
|
|
||||||
data class UnknownUpdateType(
|
data class UnknownUpdateType(
|
||||||
override val updateId: UpdateIdentifier,
|
override val updateId: UpdateIdentifier,
|
||||||
override val data: String
|
override val data: String,
|
||||||
|
val rawJson: JsonElement
|
||||||
) : Update
|
) : Update
|
||||||
|
|
||||||
internal object UpdateSerializerWithoutDeserialization : KSerializer<Update> {
|
internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
|
||||||
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
|
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder)
|
override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder)
|
||||||
|
|||||||
@@ -53,15 +53,17 @@ fun createMarkdownText(
|
|||||||
partLength: Int = 4096
|
partLength: Int = 4096
|
||||||
): List<String> = createFormattedText(entities, partLength, MarkdownParseMode)
|
): List<String> = createFormattedText(entities, partLength, MarkdownParseMode)
|
||||||
|
|
||||||
fun CaptionedInput.toMarkdownCaptions(): List<String> = createMarkdownText(
|
fun List<TextSource>.toMarkdownCaptions(): List<String> = createMarkdownText(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
captionLength.last + 1
|
captionLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun CaptionedInput.toMarkdownCaptions(): List<String> = fullEntitiesList().toMarkdownCaptions()
|
||||||
|
|
||||||
fun TextContent.toMarkdownTexts(): List<String> = createMarkdownText(
|
fun List<TextSource>.toMarkdownTexts(): List<String> = createMarkdownText(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
textLength.last + 1
|
textLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun TextContent.toMarkdownTexts(): List<String> = fullEntitiesList().toMarkdownTexts()
|
||||||
|
|
||||||
|
|
||||||
fun createMarkdownV2Text(
|
fun createMarkdownV2Text(
|
||||||
@@ -69,15 +71,17 @@ fun createMarkdownV2Text(
|
|||||||
partLength: Int = 4096
|
partLength: Int = 4096
|
||||||
): List<String> = createFormattedText(entities, partLength, MarkdownV2ParseMode)
|
): List<String> = createFormattedText(entities, partLength, MarkdownV2ParseMode)
|
||||||
|
|
||||||
fun CaptionedInput.toMarkdownV2Captions(): List<String> = createMarkdownV2Text(
|
fun List<TextSource>.toMarkdownV2Captions(): List<String> = createMarkdownV2Text(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
captionLength.last + 1
|
captionLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun CaptionedInput.toMarkdownV2Captions(): List<String> = fullEntitiesList().toMarkdownV2Captions()
|
||||||
|
|
||||||
fun TextContent.toMarkdownV2Texts(): List<String> = createMarkdownV2Text(
|
fun List<TextSource>.toMarkdownV2Texts(): List<String> = createMarkdownV2Text(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
textLength.last + 1
|
textLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun TextContent.toMarkdownV2Texts(): List<String> = fullEntitiesList().toMarkdownV2Texts()
|
||||||
|
|
||||||
|
|
||||||
fun createHtmlText(
|
fun createHtmlText(
|
||||||
@@ -85,14 +89,16 @@ fun createHtmlText(
|
|||||||
partLength: Int = 4096
|
partLength: Int = 4096
|
||||||
): List<String> = createFormattedText(entities, partLength, HTMLParseMode)
|
): List<String> = createFormattedText(entities, partLength, HTMLParseMode)
|
||||||
|
|
||||||
fun CaptionedInput.toHtmlCaptions(): List<String> = createHtmlText(
|
fun List<TextSource>.toHtmlCaptions(): List<String> = createHtmlText(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
captionLength.last + 1
|
captionLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun CaptionedInput.toHtmlCaptions(): List<String> = fullEntitiesList().toHtmlCaptions()
|
||||||
|
|
||||||
fun TextContent.toHtmlTexts(): List<String> = createHtmlText(
|
fun List<TextSource>.toHtmlTexts(): List<String> = createHtmlText(
|
||||||
fullEntitiesList(),
|
this,
|
||||||
textLength.last + 1
|
textLength.last + 1
|
||||||
)
|
)
|
||||||
|
fun TextContent.toHtmlTexts(): List<String> = fullEntitiesList().toHtmlTexts()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.supervisorScope
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
|
||||||
|
*
|
||||||
|
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
|
||||||
|
* exception will be available for catching
|
||||||
|
*/
|
||||||
|
suspend inline fun <T> handleSafely(
|
||||||
|
noinline onException: suspend (Exception) -> T = { throw it },
|
||||||
|
noinline block: suspend CoroutineScope.() -> T
|
||||||
|
): T {
|
||||||
|
return try {
|
||||||
|
supervisorScope(block)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
onException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
|
import com.benasher44.uuid.uuid4
|
||||||
import io.ktor.utils.io.core.Input
|
import io.ktor.utils.io.core.Input
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
expect class StorageFile {
|
data class StorageFileInfo(
|
||||||
val contentType: String
|
val contentType: String,
|
||||||
val fileName: String
|
val fileName: String
|
||||||
fun generateCustomName(): String
|
) {
|
||||||
fun asInput(): Input
|
fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}"
|
||||||
|
}
|
||||||
|
|
||||||
|
data class StorageFile(
|
||||||
|
val storageFileInfo: StorageFileInfo,
|
||||||
|
private val inputSource: () -> Input
|
||||||
|
) {
|
||||||
|
fun asInput() = inputSource()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
|
private val extensionRegex = Regex("[^.]*$")
|
||||||
|
|
||||||
|
val String.fileExtension
|
||||||
|
get() = extensionRegex.find(this) ?.value ?: ""
|
||||||
@@ -120,7 +120,7 @@ fun String.mentionHTML(): String = mention(String::toHtml)
|
|||||||
|
|
||||||
|
|
||||||
fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown)
|
fun String.hashTagMarkdown(): String = hashTag(String::toMarkdown)
|
||||||
fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common)
|
fun String.hashTagMarkdownV2(): String = hashTag(String::escapeMarkdownV2Common).escapeMarkdownV2Common()
|
||||||
fun String.hashTagHTML(): String = hashTag(String::toHtml)
|
fun String.hashTagHTML(): String = hashTag(String::toHtml)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ fun String.toMarkdown(): String {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val markdownV2LinkEscapes = mutableSetOf(')', '\\')
|
private val markdownV2LinkEscapes = setOf(')', '\\')
|
||||||
private val markdownV2PreAndCodeEscapes = mutableSetOf('`', '\\')
|
private val markdownV2PreAndCodeEscapes = setOf('`', '\\')
|
||||||
private val markdownV2CommonEscapes = mutableSetOf(
|
private val markdownV2CommonEscapes = setOf(
|
||||||
'_',
|
'_',
|
||||||
'*',
|
'*',
|
||||||
'[', ']',
|
'[', ']',
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
|
||||||
|
|
||||||
import com.benasher44.uuid.uuid4
|
|
||||||
import io.ktor.utils.io.core.Input
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.Transient
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
actual data class StorageFile(
|
|
||||||
actual val contentType: String,
|
|
||||||
actual val fileName: String,
|
|
||||||
@Transient
|
|
||||||
val inputGetter: () -> Input = throw IllegalStateException("Can't create object without input"),
|
|
||||||
@Transient
|
|
||||||
val extension: String = throw IllegalStateException("Can't create object without extension")
|
|
||||||
) {
|
|
||||||
actual fun asInput(): Input = inputGetter()
|
|
||||||
actual fun generateCustomName(): String = "${uuid4()}.$extension"
|
|
||||||
}
|
|
||||||
@@ -1,20 +1,16 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
import com.benasher44.uuid.uuid4
|
|
||||||
import io.ktor.utils.io.core.Input
|
|
||||||
import io.ktor.utils.io.streams.asInput
|
import io.ktor.utils.io.streams.asInput
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.Transient
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
||||||
@Serializable
|
fun StorageFile(
|
||||||
actual class StorageFile(
|
file: File
|
||||||
@Transient
|
) = StorageFile(
|
||||||
private val file: File = throw IllegalStateException("Can't create object without file")
|
StorageFileInfo(
|
||||||
|
Files.probeContentType(file.toPath()),
|
||||||
|
file.name
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
actual val contentType: String = Files.probeContentType(file.toPath())
|
file.inputStream().asInput()
|
||||||
actual val fileName: String = file.name
|
|
||||||
actual fun generateCustomName(): String = "${uuid4()}.${file.extension}"
|
|
||||||
actual fun asInput(): Input = Files.newInputStream(file.toPath()).asInput()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,11 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
|
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
import io.ktor.request.receiveText
|
import io.ktor.request.receiveText
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
@@ -45,6 +41,7 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||||
allowedUpdates: List<String>? = null,
|
allowedUpdates: List<String>? = null,
|
||||||
maxAllowedConnections: Int? = null,
|
maxAllowedConnections: Int? = null,
|
||||||
|
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
|
||||||
block: UpdateReceiver<Update>
|
block: UpdateReceiver<Update>
|
||||||
): Job {
|
): Job {
|
||||||
val executeDeferred = certificate ?.let {
|
val executeDeferred = certificate ?.let {
|
||||||
@@ -74,12 +71,18 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
module {
|
module {
|
||||||
routing {
|
routing {
|
||||||
post(listenRoute) {
|
post(listenRoute) {
|
||||||
val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
|
handleSafely(
|
||||||
val update = nonstrictJsonFormat.fromJson(
|
{
|
||||||
RawUpdate.serializer(),
|
exceptionsHandler ?.invoke(it)
|
||||||
asJson
|
}
|
||||||
)
|
) {
|
||||||
updatesChannel.send(update.asUpdate(asJson))
|
val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
|
||||||
|
val update = nonstrictJsonFormat.fromJson(
|
||||||
|
UpdateDeserializationStrategy,
|
||||||
|
asJson
|
||||||
|
)
|
||||||
|
updatesChannel.send(update)
|
||||||
|
}
|
||||||
call.respond("Ok")
|
call.respond("Ok")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,11 +116,6 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
launch {
|
launch {
|
||||||
for (update in updatesChannel) {
|
for (update in updatesChannel) {
|
||||||
val data = update.data
|
val data = update.data
|
||||||
if (data is MediaGroupUpdate) {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
when (data) {
|
when (data) {
|
||||||
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
|
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
|
||||||
else -> block(update)
|
else -> block(update)
|
||||||
@@ -139,6 +137,33 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun RequestsExecutor.setWebhook(
|
||||||
|
url: String,
|
||||||
|
port: Int,
|
||||||
|
engineFactory: ApplicationEngineFactory<*, *>,
|
||||||
|
listenHost: String = "0.0.0.0",
|
||||||
|
listenRoute: String = "/",
|
||||||
|
certificate: InputFile? = null,
|
||||||
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
||||||
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||||
|
allowedUpdates: List<String>? = null,
|
||||||
|
maxAllowedConnections: Int? = null,
|
||||||
|
block: UpdateReceiver<Update>
|
||||||
|
) = setWebhook(
|
||||||
|
url,
|
||||||
|
port,
|
||||||
|
engineFactory,
|
||||||
|
listenHost,
|
||||||
|
listenRoute,
|
||||||
|
certificate,
|
||||||
|
privateKeyConfig,
|
||||||
|
scope,
|
||||||
|
allowedUpdates,
|
||||||
|
maxAllowedConnections,
|
||||||
|
null,
|
||||||
|
block
|
||||||
|
)
|
||||||
|
|
||||||
suspend fun RequestsExecutor.setWebhook(
|
suspend fun RequestsExecutor.setWebhook(
|
||||||
url: String,
|
url: String,
|
||||||
port: Int,
|
port: Int,
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
kotlin_version=1.3.71
|
kotlin_version=1.3.72
|
||||||
kotlin_coroutines_version=1.3.5
|
kotlin_coroutines_version=1.3.5
|
||||||
kotlin_serialisation_runtime_version=0.20.0
|
kotlin_serialisation_runtime_version=0.20.0
|
||||||
klock_version=1.10.0
|
klock_version=1.10.5
|
||||||
uuid_version=0.1.0
|
uuid_version=0.1.0
|
||||||
ktor_version=1.3.2
|
ktor_version=1.3.2
|
||||||
|
|
||||||
library_group=com.github.insanusmokrassar
|
library_group=com.github.insanusmokrassar
|
||||||
library_version=0.25.2
|
library_version=0.27.0
|
||||||
|
|
||||||
gradle_bintray_plugin_version=1.8.4
|
gradle_bintray_plugin_version=1.8.4
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
include ":TelegramBotAPI"
|
include ":TelegramBotAPI"
|
||||||
include ":TelegramBotAPI-extensions-api"
|
include ":TelegramBotAPI-extensions-api"
|
||||||
|
include ":TelegramBotAPI-extensions-utils"
|
||||||
|
|||||||
Reference in New Issue
Block a user