1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-04 13:26:57 +00:00

Compare commits

..

62 Commits

Author SHA1 Message Date
3c3607d817 fill changelog 2022-05-03 14:32:29 +06:00
d0add888c4 now the handlers from the upstream lists are not modified in the whole fsm behaviour context 2022-05-03 14:28:03 +06:00
3e3adab46b complete fix of issue with subcontexts 2022-05-03 13:58:13 +06:00
89d13de307 deprecation of BehaviourContextWithFSMBuilder 2022-05-03 10:38:48 +06:00
32451f4e1c start 0.38.19 2022-05-03 09:44:39 +06:00
ad50f41d1e Merge pull request #578 from InsanusMokrassar/0.38.18
0.38.18
2022-05-02 13:49:24 +06:00
8c9cd9df67 fix of #577 2022-05-02 13:21:43 +06:00
8e7f7a03c8 note about xzima help 2022-05-02 12:44:20 +06:00
484e09374d small refactor 2022-05-02 12:29:30 +06:00
Alex
619c82bb32 fix: new BehaviourContext ignored 2022-05-02 12:29:21 +06:00
33fb75a5eb start 0.38.18 2022-05-02 12:28:39 +06:00
a52f31f4c9 Merge pull request #575 from InsanusMokrassar/0.38.17
0.38.17
2022-05-01 17:23:33 +06:00
682f696866 fill changelog 2022-05-01 17:23:12 +06:00
87fff2e5d0 add BotCommandScope helper extensions 2022-05-01 17:18:44 +06:00
92b4ba2ff0 fix of #574 2022-05-01 16:50:03 +06:00
9014cdbc99 start 0.38.17 2022-05-01 16:41:47 +06:00
37317a1055 Merge pull request #573 from InsanusMokrassar/0.38.16
0.38.16
2022-04-29 23:28:12 +06:00
60c21002e1 fixes 2022-04-29 19:50:02 +06:00
78a7a3546a remove checking of webapp data from webapp module due to its existing in core 2022-04-29 19:18:22 +06:00
4799617ced "TelegramAPIUrlsKeeper" now have two new things: properties "webAppDataSecretKey" and fun "checkWebAppLink" 2022-04-29 19:11:38 +06:00
c70484076d start 0.38.16 2022-04-29 18:56:38 +06:00
0c92e6eeb4 Merge pull request #570 from InsanusMokrassar/0.38.15
0.38.15
2022-04-26 13:45:09 +06:00
efd1c8f83a add additional onDataCallbackQuery variants with regex filters 2022-04-26 13:22:00 +06:00
7e57a0e4e0 revert update of microutils version 2022-04-26 13:07:16 +06:00
6b977e67d0 Revert "add telegram update handler"
This reverts commit ab5937449c.
2022-04-26 13:05:56 +06:00
ab5937449c add telegram update handler 2022-04-25 22:28:35 +06:00
61d3131bf2 update micro_utils 2022-04-25 21:58:25 +06:00
048f244449 update microutils 2022-04-25 21:19:49 +06:00
6ebf4ff652 fixes in MessageContent#serializationModule 2022-04-25 21:19:08 +06:00
b4c41d7dd8 start 0.38.15 2022-04-24 18:39:07 +06:00
930507ab80 Merge pull request #568 from InsanusMokrassar/0.38.14
0.38.14
2022-04-23 12:43:02 +06:00
56d3628dd3 add support of menu buttons 2022-04-23 12:39:14 +06:00
56d7853f68 update defaults in telegramBot factories 2022-04-23 12:34:38 +06:00
0a27d9bf28 add shortcuts for telegram and webApp in webapps 2022-04-23 12:34:26 +06:00
c74df3960c fixes in viewport changed events 2022-04-23 12:34:13 +06:00
f4b080cb45 now WebAppData is data class 2022-04-23 12:34:05 +06:00
350b77f6ff add webAppButton overload with url as string 2022-04-23 12:33:56 +06:00
955e14f67c add trigger and expectation for WebAppData 2022-04-23 12:33:45 +06:00
ae88b7d94d fix dokka 2022-04-23 12:33:34 +06:00
88c62bd51c add checking of data in web app 2022-04-23 12:33:23 +06:00
705bcbe352 webapps 2022-04-23 12:32:39 +06:00
ffcc149c40 update info about supported telegram bot api version 2022-04-23 12:31:37 +06:00
402d91f9f2 add support of SentWebAppMessage 2022-04-23 12:31:22 +06:00
c19bccc7d1 add support of WebAppInfo 2022-04-23 12:30:35 +06:00
2c71377058 voice(_c/C)hat* -> video(_c/C)hat* renames 2022-04-23 12:30:15 +06:00
12d2c05110 add support of WebhookInfo#lastSynchronizationErrorDate 2022-04-23 12:28:40 +06:00
ddf46c1afd add support of ChatAdministratorRights 2022-04-23 12:27:24 +06:00
dd0a8b99c7 add WebAppData 2022-04-23 12:26:28 +06:00
75fb72936f fix of 563 2022-04-23 12:25:39 +06:00
1a6a010446 start 0.38.14 2022-04-23 12:24:52 +06:00
4c7bfe99c0 Merge pull request #567 from InsanusMokrassar/renovate/github_release_plugin_version
Update dependency com.github.breadmoirai:github-release to v2.3.7
2022-04-20 20:13:28 +06:00
Renovate Bot
1e4b8d405e Update dependency com.github.breadmoirai:github-release to v2.3.7 2022-04-19 00:27:10 +00:00
007112e67a Merge pull request #559 from InsanusMokrassar/0.38.13
0.38.13
2022-04-17 00:26:23 +06:00
a87c21273d BehaviourContext#on*Message extensions 2022-04-17 00:22:00 +06:00
ff2d1615da replace TODO for pipelineStepsHolder with default realization 2022-04-16 23:58:24 +06:00
08ef146daa Update gradle.properties 2022-04-13 23:12:32 +06:00
a404a6c59c fixes in DownloadFIleChannelRequestCallFactory 2022-04-11 21:56:47 +06:00
1089c716f3 deprecate StorageFileInfo 2022-04-11 21:00:56 +06:00
81ad55b19f add KtorPipelineStepsHolder#onRequestReturnResult 2022-04-11 14:41:02 +06:00
863c872f35 start to add pipelines and fix mention creation 2022-04-11 12:46:33 +06:00
ece6a917ed start 0.38.13 2022-04-11 11:59:37 +06:00
12248aa702 Merge pull request #555 from InsanusMokrassar/0.38.12
0.38.12
2022-04-09 12:24:57 +06:00
94 changed files with 2251 additions and 355 deletions

View File

@@ -1,5 +1,58 @@
# TelegramBotAPI changelog
## 0.38.19
* `BehaviourBuilder`:
* Hotfixes
* `BehaviourBuilder FSM`:
* `BehaviourContextWithFSMBuilder` deprecated in favor to `BehaviourContextWithFSM`
* Now it is possible to define additional handlers in subcontexts of `BehaviourBuilderWithFSM`
## 0.38.18
* `Core`:
* Add support of test servers (fix of [#577](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/577))
* `BehaviourBuilder`:
* Fixes in extension `BehaviourContext#doInSubContextWithUpdatesFilter` (thanks to [xzima](https://github.com/xzima))
## 0.38.17
* `Core`:
* Add `BotCommandScopeChat` as new `BotCommandScope` (fix of [#574](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/574))
* `BotCommandScope` companion got several properties and functions for more useful scope creation
## 0.38.16
* `Core`:
* `TelegramAPIUrlsKeeper` now have two new things: properties `webAppDataSecretKey` and fun `checkWebAppLink`
## 0.38.15
* `Common`:
* `Version`:
* `MicroUtils`: `0.9.20` -> `0.9.24`
* `Core`:
* Fixes in `MessageContent#serializationModule`
* `BehaviourBuilder`:
* Add triggers for `DataCallbackQuery` and subtypes with regex checking of data
## 0.38.14
__This update contains including of [Telegram Bot API 6.0](https://core.telegram.org/bots/api-changelog#april-16-2022)__
* `Core`:
* Constructor of `UnknownInlineKeyboardButton` is not internal and can be created with any `json`
* `WebApps`:
* Created 🎉
## 0.38.13
* `Core`:
* Fixes in `mention` creation
* Deprecate `StorageFileInfo`
* `BehaviourBuilder`:
* In the expectations a lot of `on*Message` extensions have been added (like `onContentMessage`). These extensions could be useful when with the `Content` its message info is important
## 0.38.12
* `Common`:

View File

@@ -1,4 +1,4 @@
# TelegramBotAPI [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) [![Supported version](https://img.shields.io/badge/Telegram%20Bot%20API-5.7-blue)](https://core.telegram.org/bots/api-changelog#january-31-2022)
# TelegramBotAPI [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/tgbotapi) [![Supported version](https://img.shields.io/badge/Telegram%20Bot%20API-6.0-blue)](https://core.telegram.org/bots/api-changelog#april-16-2022)
| [![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin) [![Build Status](https://github.com/InsanusMokrassar/TelegramBotAPI/workflows/Build/badge.svg)](https://github.com/InsanusMokrassar/TelegramBotAPI/actions) [![Small survey](https://img.shields.io/static/v1?label=Google&message=Survey&color=blue&logo=google-sheets)](https://docs.google.com/forms/d/e/1FAIpQLSctdJHT_aEniyYT0-IUAEfo1hsIlezX2owlkEAYX4KPl2V2_A/viewform?usp=sf_link) [![Chat in Telegram](https://img.shields.io/static/v1?label=Telegram&message=Chat&color=blue&logo=telegram)](https://t.me/InMoTelegramBotAPI) |
|:---:|

View File

@@ -40,11 +40,17 @@ kotlin {
dependencies {
implementation kotlin('stdlib')
rootProject.subprojects.forEach {
if (it != project) {
api it
}
}
api project(":tgbotapi.core")
api project(":tgbotapi.api")
api project(":tgbotapi.utils")
api project(":tgbotapi.behaviour_builder")
api project(":tgbotapi.behaviour_builder.fsm")
api project(":tgbotapi")
}
}
jsMain {
dependencies {
api project(":tgbotapi.webapps")
}
}
}

View File

@@ -12,7 +12,7 @@ klock_version=2.7.0
uuid_version=0.4.0
ktor_version=1.6.8
micro_utils_version=0.9.19
micro_utils_version=0.9.24
javax_activation_version=1.1.1
@@ -20,6 +20,6 @@ javax_activation_version=1.1.1
dokka_version=1.6.10
library_group=dev.inmo
library_version=0.38.12
library_version=0.38.19
github_release_plugin_version=2.2.12
github_release_plugin_version=2.3.7

View File

@@ -21,4 +21,5 @@ include ":tgbotapi.extensions.utils"
include ":tgbotapi.extensions.behaviour_builder"
include ":tgbotapi.extensions.behaviour_builder.fsm"
include ":tgbotapi"
include ":tgbotapi.webapps"
include ":docs"

View File

@@ -40,8 +40,9 @@ data class BotBuilder internal constructor(
fun buildBot(
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
testServer: Boolean = false,
block: BotBuilder.() -> Unit
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
TelegramAPIUrlsKeeper(token, testServer, apiUrl),
BotBuilder().apply(block).createHttpClient()
)

View File

@@ -13,7 +13,7 @@ import io.ktor.client.engine.*
*/
fun telegramBot(
urlsKeeper: TelegramAPIUrlsKeeper,
client: HttpClient
client: HttpClient = HttpClient()
): TelegramBot = telegramBot(urlsKeeper) {
this.client = client
}
@@ -66,17 +66,19 @@ inline fun telegramBot(
inline fun telegramBot(
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
client: HttpClient
): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), client)
testServer: Boolean = false,
client: HttpClient = HttpClient()
): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, testServer, apiUrl), client)
@Suppress("NOTHING_TO_INLINE")
inline fun <T: HttpClientEngineConfig> telegramBot(
token: String,
clientFactory: HttpClientEngineFactory<T>,
apiUrl: String = telegramBotAPIDefaultUrl,
testServer: Boolean = false,
noinline clientConfig: HttpClientConfig<T>.() -> Unit = {}
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
TelegramAPIUrlsKeeper(token, testServer, apiUrl),
clientFactory,
clientConfig
)
@@ -90,9 +92,10 @@ inline fun telegramBot(
token: String,
clientEngine: HttpClientEngine,
apiUrl: String = telegramBotAPIDefaultUrl,
testServer: Boolean = false,
noinline clientConfig: HttpClientConfig<*>.() -> Unit = {}
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
TelegramAPIUrlsKeeper(token, testServer, apiUrl),
clientEngine,
clientConfig
)
@@ -105,8 +108,9 @@ inline fun telegramBot(
inline fun telegramBot(
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
testServer: Boolean = false,
noinline clientConfig: HttpClientConfig<*>.() -> Unit
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
TelegramAPIUrlsKeeper(token, testServer, apiUrl),
clientConfig
)

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.extensions.api.answers
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.answers.AnswerWebAppQuery
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult
import dev.inmo.tgbotapi.types.WebAppQueryId
suspend fun TelegramBot.answerWebAppQuery(
webAppQueryId: WebAppQueryId,
result: InlineQueryResult
) = execute(AnswerWebAppQuery(webAppQueryId, result))
suspend fun TelegramBot.answer(
webAppQueryId: WebAppQueryId,
result: InlineQueryResult
) = execute(AnswerWebAppQuery(webAppQueryId, result))

View File

@@ -0,0 +1,13 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.ClearMyDefaultAdministratorRights
import dev.inmo.tgbotapi.types.ChatAdministratorRightsImpl
suspend fun TelegramBot.clearMyDefaultAdministratorRights(
forChannels: Boolean? = null
) = execute(ClearMyDefaultAdministratorRights(forChannels))
suspend fun TelegramBot.clearMyDefaultAdministratorRightsForChannels() = clearMyDefaultAdministratorRights(forChannels = true)
suspend fun TelegramBot.clearMyDefaultAdministratorRightsForGroupsAndSupergroups() = clearMyDefaultAdministratorRights(forChannels = false)

View File

@@ -0,0 +1,12 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.GetMyDefaultAdministratorRights
suspend fun TelegramBot.getMyDefaultAdministratorRights(
forChannels: Boolean? = null
) = execute(GetMyDefaultAdministratorRights(forChannels))
suspend fun TelegramBot.getMyDefaultAdministratorRightsForChannels() = getMyDefaultAdministratorRights(forChannels = true)
suspend fun TelegramBot.getMyDefaultAdministratorRightsForGroupsAndSupergroups() = getMyDefaultAdministratorRights(forChannels = false)

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.extensions.api.bot
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.bot.SetMyDefaultAdministratorRights
import dev.inmo.tgbotapi.types.ChatAdministratorRightsImpl
suspend fun TelegramBot.setMyDefaultAdministratorRights(
rights: ChatAdministratorRightsImpl,
forChannels: Boolean? = null
) = execute(SetMyDefaultAdministratorRights(rights, forChannels))
suspend fun TelegramBot.setMyDefaultAdministratorRightsForChannels(
rights: ChatAdministratorRightsImpl
) = setMyDefaultAdministratorRights(rights, forChannels = true)
suspend fun TelegramBot.setMyDefaultAdministratorRightsForGroupsAndSupergroups(
rights: ChatAdministratorRightsImpl
) = setMyDefaultAdministratorRights(rights, forChannels = false)

View File

@@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.extensions.api.chat.get
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.get.GetChatMenuButton
import dev.inmo.tgbotapi.requests.chat.modify.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
suspend fun TelegramBot.getChatMenuButton(
chatId: ChatId
) = execute(GetChatMenuButton(chatId))
suspend fun TelegramBot.getChatMenuButton(
chat: PrivateChat
) = getChatMenuButton(chat.id)

View File

@@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.extensions.api.chat.get
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.get.GetDefaultChatMenuButton
import dev.inmo.tgbotapi.requests.chat.modify.SetDefaultChatMenuButton
import dev.inmo.tgbotapi.types.MenuButton
suspend fun TelegramBot.getDefaultChatMenuButton() = execute(GetDefaultChatMenuButton)

View File

@@ -18,7 +18,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers: Boolean? = null,
canPinMessages: Boolean? = null,
canPromoteMembers: Boolean? = null,
canManageVoiceChats: Boolean? = null,
canManageVideoChats: Boolean? = null,
canManageChat: Boolean?
) = execute(
PromoteChatMember(
@@ -34,7 +34,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers,
canPinMessages,
canPromoteMembers,
canManageVoiceChats,
canManageVideoChats,
canManageChat
)
)
@@ -52,7 +52,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers: Boolean? = null,
canPinMessages: Boolean? = null,
canPromoteMembers: Boolean? = null,
canManageVoiceChats: Boolean? = null,
canManageVideoChats: Boolean? = null,
canManageChat: Boolean? = null
) = promoteChatMember(
chat.id,
@@ -67,7 +67,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers,
canPinMessages,
canPromoteMembers,
canManageVoiceChats,
canManageVideoChats,
canManageChat
)
@@ -84,7 +84,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers: Boolean? = null,
canPinMessages: Boolean? = null,
canPromoteMembers: Boolean? = null,
canManageVoiceChats: Boolean? = null,
canManageVideoChats: Boolean? = null,
canManageChat: Boolean? = null
) = promoteChatMember(
chatId,
@@ -99,7 +99,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers,
canPinMessages,
canPromoteMembers,
canManageVoiceChats,
canManageVideoChats,
canManageChat
)
@@ -116,7 +116,7 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers: Boolean? = null,
canPinMessages: Boolean? = null,
canPromoteMembers: Boolean? = null,
canManageVoiceChats: Boolean? = null,
canManageVideoChats: Boolean? = null,
canManageChat: Boolean? = null
) = promoteChatMember(
chat.id,
@@ -131,6 +131,6 @@ suspend fun TelegramBot.promoteChatMember(
canRestrictMembers,
canPinMessages,
canPromoteMembers,
canManageVoiceChats,
canManageVideoChats,
canManageChat
)

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.extensions.api.chat.modify
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.modify.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
suspend fun TelegramBot.setChatMenuButton(
chatId: ChatId,
menuButton: MenuButton
) = execute(SetChatMenuButton(chatId, menuButton))
suspend fun TelegramBot.setChatMenuButton(
chat: PrivateChat,
menuButton: MenuButton
) = setChatMenuButton(chat.id, menuButton)

View File

@@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.extensions.api.chat.modify
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.modify.SetDefaultChatMenuButton
import dev.inmo.tgbotapi.types.MenuButton
suspend fun TelegramBot.setDefaultChatMenuButton(
menuButton: MenuButton
) = execute(SetDefaultChatMenuButton(menuButton))

View File

@@ -9,6 +9,8 @@ import dev.inmo.micro_utils.coroutines.accumulatorFlow
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import kotlin.jvm.JvmName
import kotlin.reflect.KClass
/**
* Interface which combine [BehaviourContext] and [StatesMachine]. Subcontext of triggers and states contexts must have
@@ -30,6 +32,25 @@ interface BehaviourContextWithFSM<T : State> : BehaviourContext, StatesMachine<T
}
}
/**
* Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that
* for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement
*
* @see BehaviourWithFSMStateHandlerHolder
* @see onStateOrSubstate
*/
fun <I : T> add(kClass: KClass<I>, strict: Boolean = false, handler: BehaviourWithFSMStateHandler<I, T>)
/**
* Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that
* for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass
* requirements
*
* @see BehaviourWithFSMStateHandlerHolder
* @see strictlyOn
*/
fun <I : T> addStrict(kClass: KClass<I>, handler: BehaviourWithFSMStateHandler<I, T>) = add(kClass, strict = true, handler)
override fun copy(
bot: TelegramBot,
scope: CoroutineScope,
@@ -48,6 +69,28 @@ interface BehaviourContextWithFSM<T : State> : BehaviourContext, StatesMachine<T
}
}
/**
* Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that
* for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSM.add
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : O, O: State> BehaviourContextWithFSM<O>.onStateOrSubstate(handler: BehaviourWithFSMStateHandler<I, O>) = add(I::class, strict = false, handler)
/**
* Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that
* for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass
* requirements
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSM.addStrict
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : O, O: State> BehaviourContextWithFSM<O>.strictlyOn(handler: BehaviourWithFSMStateHandler<I, O>) = addStrict(I::class, handler)
/**
* Default realization of [BehaviourContextWithFSM]. It uses [behaviourContext] as a base for this object as
* [BehaviourContext], but managing substates contexts updates for avoiding of updates lost between states
@@ -58,6 +101,9 @@ class DefaultBehaviourContextWithFSM<T : State>(
private val handlers: List<BehaviourWithFSMStateHandlerHolder<*, T>>
) : BehaviourContext by behaviourContext, BehaviourContextWithFSM<T> {
private val updatesFlows = mutableMapOf<Any, Flow<Update>>()
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
private var actualHandlersList = additionalHandlers + handlers
private fun getContextUpdatesFlow(context: Any) = updatesFlows.getOrPut(context) {
allUpdatesFlow.accumulatorFlow(scope)
}
@@ -65,12 +111,17 @@ class DefaultBehaviourContextWithFSM<T : State>(
override suspend fun StatesMachine<in T>.handleState(state: T): T? = launchStateHandling(
state,
allUpdatesFlow,
handlers
actualHandlersList
)
override fun <I : T> add(kClass: KClass<I>, strict: Boolean, handler: BehaviourWithFSMStateHandler<I, T>) {
additionalHandlers.add(BehaviourWithFSMStateHandlerHolder(kClass, strict, handler))
actualHandlersList = additionalHandlers + handlers
}
override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions {
val statePerformer: suspend (T) -> Unit = { state: T ->
val newState = launchStateHandling(state, getContextUpdatesFlow(state.context), handlers)
val newState = launchStateHandling(state, getContextUpdatesFlow(state.context), actualHandlersList)
if (newState != null) {
statesManager.update(state, newState)
} else {
@@ -94,6 +145,26 @@ class DefaultBehaviourContextWithFSM<T : State>(
launch { statePerformer(it) }
}
}
/**
* Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that
* for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSM.add
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : T> onStateOrSubstate(handler: BehaviourWithFSMStateHandler<I, T>) = add(I::class, strict = false, handler)
/**
* Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that
* for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass
* requirements
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSM.addStrict
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : T> strictlyOn(handler: BehaviourWithFSMStateHandler<I, T>) = addStrict(I::class, handler)
override suspend fun startChain(state: T) {
statesManager.startChain(state)
@@ -106,7 +177,7 @@ class DefaultBehaviourContextWithFSM<T : State>(
onBufferOverflow: BufferOverflow,
upstreamUpdatesFlow: Flow<Update>?,
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?
): BehaviourContextWithFSM<T> = BehaviourContextWithFSM(
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
behaviourContext.copy(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter),
handlers,
statesManager

View File

@@ -14,70 +14,8 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlin.reflect.KClass
class BehaviourContextWithFSMBuilder<T : State> internal constructor(
private val resultBehaviourContext: BehaviourContextWithFSM<T>,
private val handlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>>
) : BehaviourContextWithFSM<T> by resultBehaviourContext {
internal constructor(
baseBehaviourContext: BehaviourContext,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
handlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf()
) : this(DefaultBehaviourContextWithFSM(baseBehaviourContext, statesManager, handlers), handlers)
/**
* Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that
* for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement
*
* @see BehaviourWithFSMStateHandlerHolder
* @see onStateOrSubstate
*/
fun <I : T> add(kClass: KClass<I>, handler: BehaviourWithFSMStateHandler<I, T>) {
handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, false, handler))
}
/**
* Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that
* for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass
* requirements
*
* @see BehaviourWithFSMStateHandlerHolder
* @see strictlyOn
*/
fun <I : T> addStrict(kClass: KClass<I>, handler: BehaviourWithFSMStateHandler<I, T>) {
handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, true, handler))
}
/**
* Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that
* for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSMBuilder.add
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : T> onStateOrSubstate(handler: BehaviourWithFSMStateHandler<I, T>) {
add(I::class, handler)
}
/**
* Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that
* for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass
* requirements
*
* @see BehaviourWithFSMStateHandlerHolder
* @see BehaviourContextWithFSMBuilder.addStrict
*/
@Suppress("MemberVisibilityCanBePrivate")
inline fun <reified I : T> strictlyOn(handler: BehaviourWithFSMStateHandler<I, T>) {
addStrict(I::class, handler)
}
/**
* Returns completed [resultBehaviourContext], [handlers] and [statesManager]
*/
internal fun build() = resultBehaviourContext
}
@Deprecated("Will be removed soon")
typealias BehaviourContextWithFSMBuilder<T> = BehaviourContextWithFSM<T>
/**
* Creates [BehaviourContextWithFSM] via creating of [DefaultBehaviourContext] with [this] as [TelegramBot],
@@ -94,17 +32,17 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
): BehaviourContextWithFSM<T> = BehaviourContextWithFSMBuilder(
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
DefaultBehaviourContext(
this,
defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = upstreamUpdatesFlow
),
statesManager,
presetHandlers
).apply { block() }.build()
presetHandlers,
statesManager
).apply { block() }
/**
* Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates
@@ -116,9 +54,9 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
): Pair<BehaviourContextWithFSM<T>, Job> = buildBehaviourWithFSM(
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): Pair<DefaultBehaviourContextWithFSM<T>, Job> = buildBehaviourWithFSM(
upstreamUpdatesFlow,
scope,
defaultExceptionsHandler,
@@ -147,8 +85,8 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
* @see BehaviourContext
* @see BehaviourContextWithFSM
* @see longPolling
* @see BehaviourContextWithFSMBuilder.strictlyOn
* @see BehaviourContextWithFSMBuilder.onStateOrSubstate
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
@PreviewFeature
suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
@@ -156,17 +94,17 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
): BehaviourContextWithFSM<T> = BehaviourContextWithFSMBuilder(
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
DefaultBehaviourContext(
this,
defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = flowUpdatesFilter.allUpdatesFlow
),
statesManager,
presetHandlers
).apply { block() }.build()
presetHandlers,
statesManager
).apply { block() }
/**
* Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates
@@ -176,16 +114,16 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
* @see buildBehaviourWithFSMAndStartLongPolling
* @see BehaviourContext
* @see longPolling
* @see BehaviourContextWithFSMBuilder.strictlyOn
* @see BehaviourContextWithFSMBuilder.onStateOrSubstate
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
@PreviewFeature
suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
) = FlowsUpdatesFilter().let {
buildBehaviourWithFSM(
it,

View File

@@ -37,11 +37,13 @@ suspend fun <T : State> telegramBotWithBehaviourAndFSM(
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
testServer: Boolean = false,
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): TelegramBot = telegramBot(
token,
apiUrl,
testServer,
builder
).apply {
buildBehaviourWithFSMAndStartLongPolling(
@@ -72,12 +74,14 @@ suspend fun <T : State> telegramBotWithBehaviourAndFSMAndStartLongPolling(
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
statesManager: StatesManager<T> = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()),
presetHandlers: MutableList<BehaviourWithFSMStateHandlerHolder<*, T>> = mutableListOf(),
block: CustomBehaviourContextReceiver<BehaviourContextWithFSMBuilder<T>, Unit>
presetHandlers: List<BehaviourWithFSMStateHandlerHolder<*, T>> = listOf(),
testServer: Boolean = false,
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): Pair<TelegramBot, Job> {
return telegramBot(
token,
apiUrl,
testServer,
builder
).let {
it to it.buildBehaviourWithFSMAndStartLongPolling (

View File

@@ -26,7 +26,7 @@ internal inline fun <BC, T, I1, I2> CustomBehaviourContextAndTwoTypesReceiver<BC
): CustomBehaviourContextAndTypeReceiver<BC, T, I2> = { invoke(this, i1, it) }
/**
* This class contains all necessary tools for work with bots and especially for [buildBehaviour]
* This class contains all necessary tools for work with bots and especially [buildBehaviour]
*
* @see DefaultBehaviourContext
*/
@@ -90,7 +90,7 @@ class DefaultBehaviourContext(
onBufferOverflow: BufferOverflow,
upstreamUpdatesFlow: Flow<Update>?,
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?
): BehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter)
): DefaultBehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter)
}
fun BehaviourContext(
@@ -116,19 +116,20 @@ suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
scope: CoroutineScope = LinkedSupervisorScope(),
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
): T = copy(
scope = scope,
updatesFilter = updatesFilter ?.let { _ ->
{
(this as? BC) ?.run {
updatesFilter(it)
} ?: true
}
},
upstreamUpdatesFlow = updatesUpstreamFlow
).run {
withContext(coroutineContext) {
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
): T {
val newContext = copy(
scope = scope,
updatesFilter = updatesFilter ?.let { _ ->
{
(this as? BC) ?.run {
updatesFilter(it)
} ?: true
}
},
upstreamUpdatesFlow = updatesUpstreamFlow
) as BC
return withContext(currentCoroutineContext()) {
newContext.behaviourContextReceiver().also { if (stopOnCompletion) newContext.stop() }
}
}

View File

@@ -30,10 +30,12 @@ suspend fun telegramBotWithBehaviour(
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
testServer: Boolean = false,
block: BehaviourContextReceiver<Unit>
): TelegramBot = telegramBot(
token,
apiUrl,
testServer,
builder
).apply {
buildBehaviour(
@@ -63,11 +65,13 @@ suspend fun telegramBotWithBehaviourAndLongPolling(
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
testServer: Boolean = false,
block: BehaviourContextReceiver<Unit>
): Pair<TelegramBot, Job> {
return telegramBot(
token,
apiUrl,
testServer,
builder
).let {
it to it.buildBehaviourWithLongPolling(

View File

@@ -23,7 +23,7 @@ import kotlinx.coroutines.flow.toList
typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T?
private suspend fun <O> BehaviourContext.waitCommonMessage(
private suspend fun <O> BehaviourContext.waitCommonContent(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
@@ -77,7 +77,7 @@ private suspend inline fun <reified T : MessageContent> BehaviourContext.waitCon
noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToContentMapper<T>? = null
) : List<T> = waitCommonMessage<T>(
) : List<T> = waitCommonContent<T>(
count,
initRequest,
includeMediaGroups,
@@ -90,7 +90,8 @@ private suspend inline fun <reified T : MessageContent> BehaviourContext.waitCon
contentConverter(mapper)
).toList()
suspend fun BehaviourContext.waitContentMessage(
suspend fun BehaviourContext.waitContent(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,

View File

@@ -0,0 +1,281 @@
@file:Suppress("unused")
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.message.content.abstracts.*
import dev.inmo.tgbotapi.types.message.content.media.*
import dev.inmo.tgbotapi.types.message.content.media.AudioMediaGroupContent
import dev.inmo.tgbotapi.types.message.content.media.DocumentMediaGroupContent
import dev.inmo.tgbotapi.types.message.content.media.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.media.VisualMediaGroupContent
import dev.inmo.tgbotapi.types.message.payments.InvoiceContent
import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
typealias CommonMessageToCommonMessageMapper<T> = suspend CommonMessage<T>.() -> CommonMessage<T>?
internal suspend fun <O : MessageContent> BehaviourContext.waitCommonMessage(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
errorFactory: NullableRequestBuilder<*> = { null },
filter: SimpleFilter<CommonMessage<MessageContent>>? = null,
mapper: suspend CommonMessage<MessageContent>.() -> CommonMessage<O>?
): Flow<CommonMessage<O>> = expectFlow(
initRequest,
count,
errorFactory
) {
val messages = when (it) {
is SentMediaGroupUpdate -> {
if (includeMediaGroups) {
it.data.map { it as CommonMessage<MessageContent> }
} else {
emptyList()
}
}
is BaseSentMessageUpdate -> listOf(it.data)
else -> return@expectFlow emptyList()
}
messages.mapNotNull { message ->
val asCommonMessage = message as CommonMessage<MessageContent>
if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper()
} else {
null
}
}
}
internal inline fun <reified T : MessageContent> contentMessageConverter(
noinline mapper: CommonMessageToCommonMessageMapper<T>? = null
): suspend CommonMessage<MessageContent>.() -> CommonMessage<T>? = mapper ?.let {
{
if (content is T) {
@Suppress("UNCHECKED_CAST")
val message = (this as CommonMessage<T>)
safelyWithoutExceptions { mapper(message) }
} else {
null
}
}
} ?: {
@Suppress("UNCHECKED_CAST")
if (content is T) this as CommonMessage<T> else null
}
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContentMessage(
count: Int = 1,
initRequest: Request<*>? = null,
includeMediaGroups: Boolean = true,
noinline errorFactory: NullableRequestBuilder<*> = { null },
noinline filter: SimpleFilter<CommonMessage<T>>? = null,
noinline mapper: CommonMessageToCommonMessageMapper<T>? = null
) : List<CommonMessage<T>> = waitCommonMessage<T>(
count,
initRequest,
includeMediaGroups,
errorFactory,
filter ?.let {
{
it.withContent<T>() ?.let { filter(it) } == true
}
},
contentMessageConverter(mapper)
).toList()
suspend fun BehaviourContext.waitContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<MessageContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<MessageContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitContactMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<ContactContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<ContactContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDiceMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<DiceContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<DiceContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitGameMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<GameContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<GameContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLocationMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<LocationContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<LocationContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitLiveLocationMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<LiveLocationContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<LiveLocationContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitStaticLocationMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<StaticLocationContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<StaticLocationContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPollMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<PollContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<PollContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitTextMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<TextContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<TextContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVenueMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<VenueContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<VenueContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudioMediaGroupContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<AudioMediaGroupContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<AudioMediaGroupContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocumentMediaGroupContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<DocumentMediaGroupContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<DocumentMediaGroupContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMediaMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = false,
filter: SimpleFilter<CommonMessage<MediaContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<MediaContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnyMediaGroupContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<MediaGroupContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<MediaGroupContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVisualMediaGroupContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<VisualMediaGroupContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<VisualMediaGroupContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitTextedMediaContentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = true,
filter: SimpleFilter<CommonMessage<TextedMediaContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<TextedMediaContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAnimationMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<AnimationContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<AnimationContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitAudioMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = false,
filter: SimpleFilter<CommonMessage<AudioContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<AudioContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitDocumentMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = false,
filter: SimpleFilter<CommonMessage<DocumentContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<DocumentContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitPhotoMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = false,
filter: SimpleFilter<CommonMessage<PhotoContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<PhotoContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitStickerMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<StickerContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<StickerContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
includeMediaGroups: Boolean = false,
filter: SimpleFilter<CommonMessage<VideoContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<VideoContent>? = null
) = waitContentMessage(count, initRequest, includeMediaGroups, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoNoteMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<VideoNoteContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<VideoNoteContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVoiceMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<VoiceContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<VoiceContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitInvoiceMessage(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<CommonMessage<InvoiceContent>>? = null,
mapper: CommonMessageToCommonMessageMapper<InvoiceContent>? = null
) = waitContentMessage(count, initRequest, false, errorFactory, filter, mapper)

View File

@@ -10,6 +10,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.ChatEvents.*
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
import dev.inmo.tgbotapi.types.message.ChatEvents.voice.*
import dev.inmo.tgbotapi.types.message.PrivateEventMessage
import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent
import kotlinx.coroutines.flow.toList
@@ -89,33 +90,66 @@ suspend fun BehaviourContext.waitChatEvents(
mapper: EventMessageToEventMapper<ChatEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice")
suspend fun BehaviourContext.waitVoiceChatEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VoiceChatEvent>>? = null,
mapper: EventMessageToEventMapper<VoiceChatEvent>? = null
filter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null,
mapper: EventMessageToEventMapper<VideoChatEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice")
suspend fun BehaviourContext.waitVoiceChatStartedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VoiceChatStarted>>? = null,
mapper: EventMessageToEventMapper<VoiceChatStarted>? = null
filter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null,
mapper: EventMessageToEventMapper<VideoChatStarted>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice")
suspend fun BehaviourContext.waitVoiceChatEndedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VoiceChatEnded>>? = null,
mapper: EventMessageToEventMapper<VoiceChatEnded>? = null
filter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null,
mapper: EventMessageToEventMapper<VideoChatEnded>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
@Deprecated("Renamed as Video instead of Voice")
suspend fun BehaviourContext.waitVoiceChatParticipantsInvitedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VoiceChatParticipantsInvited>>? = null,
mapper: EventMessageToEventMapper<VoiceChatParticipantsInvited>? = null
filter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null,
mapper: EventMessageToEventMapper<VideoChatParticipantsInvited>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null,
mapper: EventMessageToEventMapper<VideoChatEvent>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatStartedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null,
mapper: EventMessageToEventMapper<VideoChatStarted>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatEndedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null,
mapper: EventMessageToEventMapper<VideoChatEnded>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitVideoChatParticipantsInvitedEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null,
mapper: EventMessageToEventMapper<VideoChatParticipantsInvited>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitMessageAutoDeleteTimerChangedEvents(
@@ -241,3 +275,10 @@ suspend fun BehaviourContext.waitUserLoggedInEvents(
filter: SimpleFilter<ChatEventMessage<UserLoggedIn>>? = null,
mapper: EventMessageToEventMapper<UserLoggedIn>? = null
) = waitEvents(count, initRequest, errorFactory, filter, mapper)
suspend fun BehaviourContext.waitWebAppDataEvents(
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null },
count: Int = 1,
filter: SimpleFilter<PrivateEventMessage<WebAppData>>? = null,
mapper: EventMessageToEventMapper<WebAppData>? = null
) = waitEvents(count, initRequest, errorFactory, filter ?.let { { it is PrivateEventMessage && filter(it) } }, mapper)

View File

@@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CallbackQueryFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserCallbackQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus
import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate
import dev.inmo.tgbotapi.types.CallbackQuery.*
import dev.inmo.tgbotapi.types.update.abstracts.Update
@@ -44,6 +45,62 @@ suspend fun <BC : BehaviourContext> BC.onDataCallbackQuery(
scenarioReceiver
)
/**
* @param dataRegex Will be used with [initialFilter] as [initialFilter] for upstream [onDataCallbackQuery] to filter
* [DataCallbackQuery] with [DataCallbackQuery.data] [String.matches] to [dataRegex]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onDataCallbackQuery(
dataRegex: Regex,
initialFilter: SimpleFilter<DataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, DataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in DataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, DataCallbackQuery>
) = onDataCallbackQuery(
initialFilter = initialFilter + {
it.data.matches(dataRegex)
},
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param data Will be converted to [Regex] via its constructor and pass it to upstream [onDataCallbackQuery]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onDataCallbackQuery(
data: String,
initialFilter: SimpleFilter<DataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, DataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in DataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, DataCallbackQuery>
) = onDataCallbackQuery(
Regex(data),
initialFilter,
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
@@ -116,6 +173,62 @@ suspend fun <BC : BehaviourContext> BC.onInlineMessageIdDataCallbackQuery(
scenarioReceiver
)
/**
* @param dataRegex Will be used with [initialFilter] as [initialFilter] for upstream [onInlineMessageIdDataCallbackQuery]
* to filter [InlineMessageIdDataCallbackQuery] with [InlineMessageIdDataCallbackQuery.data] [String.matches] to [dataRegex]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onInlineMessageIdDataCallbackQuery(
dataRegex: Regex,
initialFilter: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, InlineMessageIdDataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in InlineMessageIdDataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, InlineMessageIdDataCallbackQuery>
) = onInlineMessageIdDataCallbackQuery(
initialFilter = initialFilter + {
it.data.matches(dataRegex)
},
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param data Will be converted to [Regex] via its constructor and pass it to upstream [onInlineMessageIdDataCallbackQuery]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onInlineMessageIdDataCallbackQuery(
data: String,
initialFilter: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, InlineMessageIdDataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in InlineMessageIdDataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, InlineMessageIdDataCallbackQuery>
) = onInlineMessageIdDataCallbackQuery(
Regex(data),
initialFilter,
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
@@ -188,6 +301,62 @@ suspend fun <BC : BehaviourContext> BC.onMessageDataCallbackQuery(
scenarioReceiver
)
/**
* @param dataRegex Will be used with [initialFilter] as [initialFilter] for upstream [onMessageDataCallbackQuery] to filter
* [MessageDataCallbackQuery] with [MessageDataCallbackQuery.data] [String.matches] to [dataRegex]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onMessageDataCallbackQuery(
dataRegex: Regex,
initialFilter: SimpleFilter<MessageDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, MessageDataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in MessageDataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, MessageDataCallbackQuery>
) = onMessageDataCallbackQuery(
initialFilter = initialFilter + {
it.data.matches(dataRegex)
},
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param data Will be converted to [Regex] via its constructor and pass it to upstream [onMessageDataCallbackQuery]
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onMessageDataCallbackQuery(
data: String,
initialFilter: SimpleFilter<MessageDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, MessageDataCallbackQuery, Update>? = CallbackQueryFilterByUser,
markerFactory: MarkerFactory<in MessageDataCallbackQuery, Any> = ByUserCallbackQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, MessageDataCallbackQuery>
) = onMessageDataCallbackQuery(
Regex(data),
initialFilter,
subcontextUpdatesFilter,
markerFactory,
scenarioReceiver
)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,

View File

@@ -24,7 +24,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
typealias CommonMessageFilter<T> = SimpleFilter<CommonMessage<T>>
inline fun <T : MessageContent> CommonMessageFilter(noinline block: CommonMessageFilter<T>) = block
internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent> BC.onContent(
internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent> BC.onContentMessageWithType(
noinline initialFilter: CommonMessageFilter<T>? = null,
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<T>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<T>, Any> = ByChatMessageMarkerFactory,
@@ -57,7 +57,7 @@ suspend fun <BC : BehaviourContext> BC.onContentMessage(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<MessageContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<MessageContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<MessageContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -81,7 +81,7 @@ suspend fun <BC : BehaviourContext> BC.onContact(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<ContactContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<ContactContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<ContactContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -105,7 +105,7 @@ suspend fun <BC : BehaviourContext> BC.onDice(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<DiceContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<DiceContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<DiceContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -129,7 +129,7 @@ suspend fun <BC : BehaviourContext> BC.onGame(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<GameContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<GameContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<GameContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -153,7 +153,7 @@ suspend fun <BC : BehaviourContext> BC.onLocation(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<LocationContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<LocationContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<LocationContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -177,7 +177,7 @@ suspend fun <BC : BehaviourContext> BC.onLiveLocation(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<LiveLocationContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<LiveLocationContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<LiveLocationContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -201,7 +201,7 @@ suspend fun <BC : BehaviourContext> BC.onStaticLocation(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<StaticLocationContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<StaticLocationContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<StaticLocationContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -225,7 +225,7 @@ suspend fun <BC : BehaviourContext> BC.onPoll(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<PollContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<PollContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<PollContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -249,7 +249,7 @@ suspend fun <BC : BehaviourContext> BC.onText(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<TextContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<TextContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<TextContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -273,7 +273,7 @@ suspend fun <BC : BehaviourContext> BC.onVenue(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<VenueContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<VenueContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<VenueContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -297,7 +297,7 @@ suspend fun <BC : BehaviourContext> BC.onAudioMediaGroup(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<AudioMediaGroupContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<AudioMediaGroupContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<AudioMediaGroupContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -321,7 +321,7 @@ suspend fun <BC : BehaviourContext> BC.onDocumentMediaGroupContent(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<DocumentMediaGroupContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<DocumentMediaGroupContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<DocumentMediaGroupContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -345,7 +345,7 @@ suspend fun <BC : BehaviourContext> BC.onTextedMediaContent(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<TextedMediaContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<TextedMediaContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<TextedMediaContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -369,7 +369,7 @@ suspend fun <BC : BehaviourContext> BC.onMediaCollection(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<MediaCollectionContent<TelegramMediaFile>>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<MediaCollectionContent<TelegramMediaFile>>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<MediaCollectionContent<TelegramMediaFile>>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -393,7 +393,7 @@ suspend fun <BC : BehaviourContext> BC.onMedia(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<MediaContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<MediaContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<MediaContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -417,7 +417,7 @@ suspend fun <BC : BehaviourContext> BC.onAnimation(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<AnimationContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<AnimationContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<AnimationContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -441,7 +441,7 @@ suspend fun <BC : BehaviourContext> BC.onAudio(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<AudioContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<AudioContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<AudioContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -465,7 +465,7 @@ suspend fun <BC : BehaviourContext> BC.onDocument(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<DocumentContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<DocumentContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<DocumentContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -489,7 +489,7 @@ suspend fun <BC : BehaviourContext> BC.onPhoto(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<PhotoContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<PhotoContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<PhotoContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -513,7 +513,7 @@ suspend fun <BC : BehaviourContext> BC.onSticker(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<StickerContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<StickerContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<StickerContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -537,7 +537,7 @@ suspend fun <BC : BehaviourContext> BC.onVideo(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<VideoContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<VideoContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<VideoContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -561,7 +561,7 @@ suspend fun <BC : BehaviourContext> BC.onVideoNote(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<VideoNoteContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<VideoNoteContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<VideoNoteContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -585,7 +585,7 @@ suspend fun <BC : BehaviourContext> BC.onVoice(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<VoiceContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<VoiceContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<VoiceContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,
@@ -609,7 +609,7 @@ suspend fun <BC : BehaviourContext> BC.onInvoice(
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<InvoiceContent>, Update> = MessageFilterByChat,
markerFactory: MarkerFactory<in CommonMessage<InvoiceContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<InvoiceContent>>
) = onContent(
) = onContentMessageWithType(
initialFilter,
subcontextUpdatesFilter,
markerFactory,

View File

@@ -12,6 +12,7 @@ import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage
import dev.inmo.tgbotapi.types.message.ChatEvents.*
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
import dev.inmo.tgbotapi.types.message.ChatEvents.voice.*
import dev.inmo.tgbotapi.types.message.PrivateEventMessage
import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent
import dev.inmo.tgbotapi.types.update.abstracts.Update
@@ -94,11 +95,12 @@ suspend fun <BC : BehaviourContext> BC.onChatEvent(
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@Deprecated("Renamed as Video instead of Voice")
suspend fun <BC : BehaviourContext> BC.onVoiceChatEvent(
initialFilter: SimpleFilter<ChatEventMessage<VoiceChatEvent>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VoiceChatEvent>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VoiceChatEvent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VoiceChatEvent>>
initialFilter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatEvent>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatEvent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatEvent>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
@@ -113,11 +115,12 @@ suspend fun <BC : BehaviourContext> BC.onVoiceChatEvent(
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@Deprecated("Renamed as Video instead of Voice")
suspend fun <BC : BehaviourContext> BC.onVoiceChatStartedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VoiceChatStarted>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VoiceChatStarted>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VoiceChatStarted>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VoiceChatStarted>>
initialFilter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatStarted>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatStarted>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatStarted>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
@@ -132,11 +135,12 @@ suspend fun <BC : BehaviourContext> BC.onVoiceChatStartedEvent(
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@Deprecated("Renamed as Video instead of Voice")
suspend fun <BC : BehaviourContext> BC.onVoiceChatEndedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VoiceChatEnded>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VoiceChatEnded>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VoiceChatEnded>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VoiceChatEnded>>
initialFilter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatEnded>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatEnded>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatEnded>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
@@ -151,11 +155,88 @@ suspend fun <BC : BehaviourContext> BC.onVoiceChatEndedEvent(
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@Deprecated("Renamed as Video instead of Voice")
suspend fun <BC : BehaviourContext> BC.onVoiceChatParticipantsInvitedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VoiceChatParticipantsInvited>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VoiceChatParticipantsInvited>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VoiceChatParticipantsInvited>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VoiceChatParticipantsInvited>>
initialFilter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatParticipantsInvited>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatParticipantsInvited>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatParticipantsInvited>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onVideoChatEvent(
initialFilter: SimpleFilter<ChatEventMessage<VideoChatEvent>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatEvent>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatEvent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatEvent>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onVideoChatStartedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VideoChatStarted>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatStarted>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatStarted>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatStarted>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onVideoChatEndedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VideoChatEnded>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatEnded>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatEnded>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatEnded>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onVideoChatParticipantsInvitedEvent(
initialFilter: SimpleFilter<ChatEventMessage<VideoChatParticipantsInvited>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, ChatEventMessage<VideoChatParticipantsInvited>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<VideoChatParticipantsInvited>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<VideoChatParticipantsInvited>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
@@ -483,3 +564,30 @@ suspend fun <BC : BehaviourContext> BC.onUserLoggedIn(
markerFactory: MarkerFactory<in ChatEventMessage<UserLoggedIn>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<UserLoggedIn>>
) = onEvent(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
* @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example,
* this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage].
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own.
* Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times]
* to combinate several filters
* @param markerFactory Will be used to identify different "stream". [scenarioReceiver] will be called synchronously
* in one "stream". Output of [markerFactory] will be used as a key for "stream"
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
suspend fun <BC : BehaviourContext> BC.onWebAppData(
initialFilter: SimpleFilter<PrivateEventMessage<WebAppData>>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, PrivateEventMessage<WebAppData>, Update>? = MessageFilterByChat,
markerFactory: MarkerFactory<in ChatEventMessage<WebAppData>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PrivateEventMessage<WebAppData>>
) = onEvent(
initialFilter ?.let { { it is PrivateEventMessage<WebAppData> && initialFilter(it) } },
subcontextUpdatesFilter ?.let { { it: ChatEventMessage<WebAppData>, update: Update -> it is PrivateEventMessage<WebAppData> && subcontextUpdatesFilter(it, update) } },
markerFactory
) {
if (it is PrivateEventMessage<WebAppData>) {
scenarioReceiver(it)
}
}

View File

@@ -28,16 +28,20 @@ fun <T> SimpleFilter<T>.listNone() = SimpleFilter<Iterable<T>> {
/**
* Makes an AND (&&) operation between [this] and [other]
*/
operator fun <T> SimpleFilter<T>.times(other: SimpleFilter<T>): SimpleFilter<T> = {
this(it) && other(it)
}
operator fun <T> SimpleFilter<T>?.times(other: SimpleFilter<T>): SimpleFilter<T> = this ?.let {
{
this(it) && other(it)
}
} ?: other
/**
* Makes an OR (||) operation between [this] and [other]
*/
operator fun <T> SimpleFilter<T>.plus(other: SimpleFilter<T>): SimpleFilter<T> = {
this(it) || other(it)
}
operator fun <T> SimpleFilter<T>?.plus(other: SimpleFilter<T>): SimpleFilter<T> = this ?.let {
{
this(it) || other(it)
}
} ?: other
/**
* Reverse results of [this]

View File

@@ -2,6 +2,6 @@ package dev.inmo.tgbotapi.CommonAbstracts.types
import dev.inmo.tgbotapi.types.ChatIdentifier
interface ChatRequest {
val chatId: ChatIdentifier
}
interface ChatRequest : OptionalChatRequest {
override val chatId: ChatIdentifier
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.CommonAbstracts.types
import dev.inmo.tgbotapi.types.ChatIdentifier
interface OptionalChatRequest {
val chatId: ChatIdentifier?
}

View File

@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.bot.Ktor
import dev.inmo.micro_utils.common.Optional
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient

View File

@@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.bot.BaseRequestsExecutor
import dev.inmo.tgbotapi.bot.Ktor.base.*
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.bot.exceptions.newRequestException
import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder
import dev.inmo.tgbotapi.bot.settings.limiters.ExceptionsOnlyLimiter
import dev.inmo.tgbotapi.bot.settings.limiters.RequestLimiter
import dev.inmo.tgbotapi.requests.abstracts.Request
@@ -15,33 +16,6 @@ import io.ktor.client.features.*
import io.ktor.client.statement.readText
import kotlinx.serialization.json.Json
class KtorRequestsExecutorBuilder(
var telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
) {
var client: HttpClient = HttpClient()
var callsFactories: List<KtorCallFactory> = emptyList()
var excludeDefaultFactories: Boolean = false
var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter()
var jsonFormatter: Json = nonstrictJsonFormat
fun build() = KtorRequestsExecutor(telegramAPIUrlsKeeper, client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter)
}
inline fun telegramBot(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
crossinline builder: KtorRequestsExecutorBuilder.() -> Unit = {}
): TelegramBot = KtorRequestsExecutorBuilder(telegramAPIUrlsKeeper).apply(builder).build()
/**
* Shortcut for [telegramBot]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
crossinline builder: KtorRequestsExecutorBuilder.() -> Unit = {}
): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, apiUrl), builder)
@RiskFeature
fun createTelegramBotDefaultKtorCallRequestsFactories() = listOf(
SimpleRequestCallFactory(),
@@ -56,7 +30,8 @@ class KtorRequestsExecutor(
callsFactories: List<KtorCallFactory> = emptyList(),
excludeDefaultFactories: Boolean = false,
private val requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter(),
private val jsonFormatter: Json = nonstrictJsonFormat
private val jsonFormatter: Json = nonstrictJsonFormat,
private val pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
if (!excludeDefaultFactories) {
@@ -73,37 +48,51 @@ class KtorRequestsExecutor(
}
override suspend fun <T : Any> execute(request: Request<T>): T {
return safely(
{ e ->
throw if (e is ClientRequestException) {
val content = e.response.readText()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
} else {
e
}
}
) {
requestsLimiter.limit {
var result: T? = null
for (potentialFactory in callsFactories) {
result = potentialFactory.makeCall(
client,
telegramAPIUrlsKeeper,
request,
jsonFormatter
)
if (result != null) {
break
}
}
return runCatching {
safely(
{ e ->
pipelineStepsHolder.onRequestException(request, e) ?.let { return@safely it }
result ?: error("Can't execute request: $request")
throw if (e is ClientRequestException) {
val content = e.response.readText()
val responseObject = jsonFormatter.decodeFromString(Response.serializer(), content)
newRequestException(
responseObject,
content,
"Can't get result object from $content"
)
} else {
e
}
}
) {
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
requestsLimiter.limit {
var result: T? = null
lateinit var factoryHandledRequest: KtorCallFactory
for (potentialFactory in callsFactories) {
pipelineStepsHolder.onBeforeCallFactoryMakeCall(request, potentialFactory)
result = potentialFactory.makeCall(
client,
telegramAPIUrlsKeeper,
request,
jsonFormatter
)
result = pipelineStepsHolder.onAfterCallFactoryMakeCall(result, request, potentialFactory)
if (result != null) {
factoryHandledRequest = potentialFactory
break
}
}
result ?.let {
pipelineStepsHolder.onRequestResultPresented(it, request, factoryHandledRequest, callsFactories)
} ?: pipelineStepsHolder.onRequestResultAbsent(request, callsFactories) ?: error("Can't execute request: $request")
}
}
}.let {
pipelineStepsHolder.onRequestReturnResult(it, request, callsFactories)
}
}
@@ -111,3 +100,31 @@ class KtorRequestsExecutor(
client.close()
}
}
class KtorRequestsExecutorBuilder(
var telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
) {
var client: HttpClient = HttpClient()
var callsFactories: List<KtorCallFactory> = emptyList()
var excludeDefaultFactories: Boolean = false
var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter()
var jsonFormatter: Json = nonstrictJsonFormat
fun build() = KtorRequestsExecutor(telegramAPIUrlsKeeper, client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter)
}
inline fun telegramBot(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
builder: KtorRequestsExecutorBuilder.() -> Unit = {}
): TelegramBot = KtorRequestsExecutorBuilder(telegramAPIUrlsKeeper).apply(builder).build()
/**
* Shortcut for [telegramBot]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun telegramBot(
token: String,
apiUrl: String = telegramBotAPIDefaultUrl,
testServer: Boolean = false,
builder: KtorRequestsExecutorBuilder.() -> Unit = {}
): TelegramBot = telegramBot(TelegramAPIUrlsKeeper(token, testServer, apiUrl), builder)

View File

@@ -1,6 +1,6 @@
package dev.inmo.tgbotapi.bot.Ktor.base
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.DownloadFileStream
import dev.inmo.tgbotapi.requests.abstracts.Request
@@ -25,13 +25,16 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
ByteReadChannelAllocator {
val scope = CoroutineScope(coroutineContext)
val scope = CoroutineScope(currentCoroutineContext() + SupervisorJob())
val outChannel = ByteChannel()
scope.launchSafelyWithoutExceptions {
client.get<HttpStatement>(fullUrl).execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive()
channel.copyAndClose(outChannel)
scope.launch {
runCatchingSafely {
client.get<HttpStatement>(fullUrl).execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive()
channel.copyAndClose(outChannel)
}
}
scope.cancel()
}
outChannel
} as T

View File

@@ -0,0 +1,75 @@
package dev.inmo.tgbotapi.bot.ktor
import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.abstracts.Request
interface KtorPipelineStepsHolder {
/**
* Will be called when any exception will happen due to the [request] handling. If returns value - that value
* will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onRequestException(
request: Request<T>,
t: Throwable
): T? = null
/**
* Will always be called before requests executor will check all [callsFactories] for an opportunity to make call of
* [request]
*/
suspend fun onBeforeSearchCallFactory(
request: Request<*>,
callsFactories: List<KtorCallFactory>
) {}
/**
* Will always be called before [potentialFactory] will try to make [request]
*/
suspend fun onBeforeCallFactoryMakeCall(
request: Request<*>,
potentialFactory: KtorCallFactory
) {}
/**
* Will always be called after [potentialFactory] has tried to make [request] and got some [result]. If returns
* value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onAfterCallFactoryMakeCall(
result: T?,
request: Request<T>,
potentialFactory: KtorCallFactory
): T? = result
/**
* Will be called when [resultCallFactory] is the [KtorCallFactory] from [callsFactories] which has successfully
* handled [request] and returned [result]. If returns value - that value will be returned from
* [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onRequestResultPresented(
result: T,
request: Request<T>,
resultCallFactory: KtorCallFactory,
callsFactories: List<KtorCallFactory>
): T? = result
/**
* Will be called when there is no [KtorCallFactory] from [callsFactories] which may handle [request]. If returns
* value - that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onRequestResultAbsent(
request: Request<T>,
callsFactories: List<KtorCallFactory>
): T? = null
/**
* This step will be called when the [result] has been retrieved (or exception has happened). If returns value -
* that value will be returned from [dev.inmo.tgbotapi.bot.RequestsExecutor.execute] instead
*/
suspend fun <T: Any> onRequestReturnResult(
result: Result<T>,
request: Request<T>,
callsFactories: List<KtorCallFactory>
): T = result.getOrThrow()
companion object : KtorPipelineStepsHolder
}

View File

@@ -61,9 +61,9 @@ object InputFileSerializer : KSerializer<InputFile> {
@Serializable(InputFileSerializer::class)
data class MultipartFile (
val file: StorageFile,
val filename: String = file.storageFileInfo.fileName
val filename: String = file.fileName
) : InputFile() {
override val fileId: String = file.storageFileInfo.generateCustomName()
override val fileId: String = file.generateCustomName()
}
@Suppress("NOTHING_TO_INLINE", "unused")

View File

@@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.requests.answers
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult
import dev.inmo.tgbotapi.types.webapps.query.SentWebAppMessage
import kotlinx.serialization.*
@Serializable
data class AnswerWebAppQuery(
@SerialName(webAppQueryIdField)
val webAppQueryId: WebAppQueryId,
@SerialName(resultField)
val result: InlineQueryResult
) : SimpleRequest<SentWebAppMessage> {
override fun method(): String = "answerWebAppQuery"
override val resultDeserializer: DeserializationStrategy<SentWebAppMessage>
get() = SentWebAppMessage.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
class ClearMyDefaultAdministratorRights(
@SerialName(forChannelsField)
val forChannels: Boolean? = null
) : SimpleRequest<Boolean> {
override fun method(): String = "setMyDefaultAdministratorRights"
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -0,0 +1,23 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.ChatMember.AdministratorChatMemberImpl
import kotlinx.serialization.*
@Serializable
data class GetMyDefaultAdministratorRights(
@SerialName(forChannelsField)
val forChannels: Boolean? = null
) : SimpleRequest<AdministratorChatMemberImpl> {
override fun method(): String = "getMyDefaultAdministratorRights"
override val resultDeserializer: DeserializationStrategy<AdministratorChatMemberImpl>
get() = AdministratorChatMemberImpl.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
companion object {
val ForChannels = GetMyDefaultAdministratorRights(true)
val ForGroups = GetMyDefaultAdministratorRights(false)
}
}

View File

@@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.requests.bot
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
class SetMyDefaultAdministratorRights(
@SerialName(rightsField)
val rights: ChatAdministratorRightsImpl,
@SerialName(forChannelsField)
val forChannels: Boolean? = null
) : SimpleRequest<Boolean> {
override fun method(): String = "setMyDefaultAdministratorRights"
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.requests.chat.get
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class GetChatMenuButton(
@SerialName(chatIdField)
override val chatId: ChatIdentifier
) : ChatRequest, SimpleRequest<MenuButton> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = GetDefaultChatMenuButton.method()
override val resultDeserializer: DeserializationStrategy<MenuButton>
get() = MenuButtonSerializer
}

View File

@@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.requests.chat.get
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.CommonAbstracts.types.OptionalChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
object GetDefaultChatMenuButton : OptionalChatRequest, SimpleRequest<MenuButton> {
override val chatId: ChatIdentifier?
get() = null
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = "getChatMenuButton"
override val resultDeserializer: DeserializationStrategy<MenuButton>
get() = MenuButtonSerializer
}

View File

@@ -32,8 +32,8 @@ data class PromoteChatMember(
private val canPinMessages: Boolean? = null,
@SerialName(canPromoteMembersField)
private val canPromoteMembers: Boolean? = null,
@SerialName(canManageVoiceChatsField)
private val canManageVoiceChats: Boolean? = null,
@SerialName(canManageVideoChatsField)
private val canManageVideoChats: Boolean? = null,
@SerialName(canManageChatField)
private val canManageChat: Boolean? = null
) : ChatMemberRequest<Boolean>, UntilDate {

View File

@@ -0,0 +1,24 @@
package dev.inmo.tgbotapi.requests.chat.modify
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class SetChatMenuButton(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@Serializable(MenuButtonSerializer::class)
@SerialName(menuButtonField)
val menuButton: MenuButton
) : ChatRequest, SimpleRequest<Boolean> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = SetDefaultChatMenuButton.method()
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
}

View File

@@ -0,0 +1,25 @@
package dev.inmo.tgbotapi.requests.chat.modify
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class SetDefaultChatMenuButton(
@Serializable(MenuButtonSerializer::class)
@SerialName(menuButtonField)
val menuButton: MenuButton
) : SimpleRequest<Boolean> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = Companion.method()
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
companion object {
fun method() = "setChatMenuButton"
}
}

View File

@@ -0,0 +1,31 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatAdministratorRights
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ChatAdministratorRightsImpl(
@SerialName(canChangeInfoField)
override val canChangeInfo: Boolean = false,
@SerialName(canPostMessagesField)
override val canPostMessages: Boolean = false,
@SerialName(canEditMessagesField)
override val canEditMessages: Boolean = false,
@SerialName(canDeleteMessagesField)
override val canRemoveMessages: Boolean = false,
@SerialName(canInviteUsersField)
override val canInviteUsers: Boolean = false,
@SerialName(canRestrictMembersField)
override val canRestrictMembers: Boolean = false,
@SerialName(canPinMessagesField)
override val canPinMessages: Boolean = false,
@SerialName(canPromoteMembersField)
override val canPromoteMembers: Boolean = false,
@SerialName(canManageVideoChatsField)
override val canManageVideoChats: Boolean = false,
@SerialName(canManageChatField)
override val canManageChat: Boolean = false,
@SerialName(isAnonymousField)
override val isAnonymous: Boolean = false
) : ChatAdministratorRights

View File

@@ -26,8 +26,8 @@ data class AdministratorChatMemberImpl(
override val canPinMessages: Boolean = false,
@SerialName(canPromoteMembersField)
override val canPromoteMembers: Boolean = false,
@SerialName(canManageVoiceChatsField)
override val canManageVoiceChats: Boolean = false,
@SerialName(canManageVideoChatsField)
override val canManageVideoChats: Boolean = false,
@SerialName(canManageChatField)
override val canManageChat: Boolean = false,
@SerialName(isAnonymousField)

View File

@@ -31,7 +31,7 @@ data class CreatorChatMember(
@Transient
override val canPromoteMembers: Boolean = true
@Transient
override val canManageVoiceChats: Boolean = true
override val canManageVideoChats: Boolean = true
@Transient
override val canManageChat: Boolean = true
@SerialName(statusField)

View File

@@ -8,17 +8,12 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializable(AdministratorChatMemberSerializer::class)
interface AdministratorChatMember : SpecialRightsChatMember {
interface AdministratorChatMember : SpecialRightsChatMember, ChatAdministratorRights {
val canBeEdited: Boolean
val canPostMessages: Boolean
val canEditMessages: Boolean
val canRemoveMessages: Boolean
val canRestrictMembers: Boolean
val canPromoteMembers: Boolean
val canManageVoiceChats: Boolean
val canManageChat: Boolean
val isAnonymous: Boolean
val customTitle: String?
val canManageVoiceChats: Boolean
get() = canManageVideoChats
}
@RiskFeature

View File

@@ -0,0 +1,18 @@
package dev.inmo.tgbotapi.types.ChatMember.abstracts
sealed interface SpecialChatAdministratorRights {
val canChangeInfo: Boolean
val canInviteUsers: Boolean
val canPinMessages: Boolean
}
interface ChatAdministratorRights : SpecialChatAdministratorRights {
val isAnonymous: Boolean
val canManageChat: Boolean
val canRemoveMessages: Boolean
val canManageVideoChats: Boolean
val canRestrictMembers: Boolean
val canPromoteMembers: Boolean
val canPostMessages: Boolean
val canEditMessages: Boolean
}

View File

@@ -3,8 +3,4 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts
import kotlinx.serialization.Serializable
@Serializable(ChatMemberSerializer::class)
interface SpecialRightsChatMember : ChatMember {
val canChangeInfo: Boolean
val canInviteUsers: Boolean
val canPinMessages: Boolean
}
interface SpecialRightsChatMember : ChatMember, SpecialChatAdministratorRights

View File

@@ -27,6 +27,7 @@ typealias FoursquareType = String
typealias GooglePlaceId = String
typealias GooglePlaceType = String
typealias MembersLimit = Int
typealias WebAppQueryId = String
typealias Seconds = Int
typealias MilliSeconds = Long
@@ -133,6 +134,7 @@ const val inlineMessageIdField = "inline_message_id"
const val callbackDataField = "callback_data"
const val callbackGameField = "callback_game"
const val callbackQueryIdField = "callback_query_id"
const val webAppQueryIdField = "web_app_query_id"
const val inlineQueryIdField = "inline_query_id"
const val inlineKeyboardField = "inline_keyboard"
const val showAlertField = "show_alert"
@@ -154,6 +156,7 @@ const val dropPendingUpdatesField = "drop_pending_updates"
const val hasCustomCertificateField = "has_custom_certificate"
const val pendingUpdateCountField = "pending_update_count"
const val lastErrorDateField = "last_error_date"
const val lastSynchronizationErrorDateField = "last_synchronization_error_date"
const val lastErrorMessageField = "last_error_message"
const val votesCountField = "voter_count"
const val isClosedField = "is_closed"
@@ -263,6 +266,9 @@ const val canRestrictMembersField = "can_restrict_members"
const val canPinMessagesField = "can_pin_messages"
const val canPromoteMembersField = "can_promote_members"
const val canManageVoiceChatsField = "can_manage_voice_chats"
const val canManageVideoChatsField = "can_manage_video_chats"
const val rightsField = "rights"
const val forChannelsField = "for_channels"
const val canManageChatField = "can_manage_chat"
const val pngStickerField = "png_sticker"
const val tgsStickerField = "tgs_sticker"
@@ -313,6 +319,7 @@ const val pricesField = "prices"
const val payloadField = "payload"
const val vcardField = "vcard"
const val resultsField = "results"
const val resultField = "result"
const val certificateField = "certificate"
const val questionField = "question"
const val optionsField = "options"
@@ -430,3 +437,7 @@ const val bankStatementField = "bank_statement"
const val rentalAgreementField = "rental_agreement"
const val passportRegistrationField = "passport_registration"
const val temporaryRegistrationField = "temporary_registration"
const val buttonTextField = "button_text"
const val webAppField = "web_app"
const val menuButtonField = "menu_button"

View File

@@ -0,0 +1,122 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
@Serializable(MenuButtonSerializer::class)
sealed interface MenuButton {
@Required
val type: String
@Serializable
object Commands : MenuButton {
@Required
override val type: String
get() = "commands"
}
@Serializable
data class WebApp(
val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : MenuButton {
@Required
override val type: String
get() = Companion.type
companion object {
val type: String
get() = "web_app"
}
}
@Serializable
object Default : MenuButton {
@Required
override val type: String
get() = "default"
}
@Serializable
@RiskFeature
data class Unknown (
override val type: String,
val rawJson: JsonElement
) : MenuButton
companion object {
fun serializer(): KSerializer<MenuButton> = MenuButtonSerializer
}
}
@Serializable
internal data class MenuButtonSurrogate(
val type: String,
val text: String? = null,
@SerialName(webAppField)
val webApp: WebAppInfo? = null,
val srcJsonElement: JsonElement? = null
)
@Serializer(MenuButton::class)
object MenuButtonSerializer : KSerializer<MenuButton> {
override val descriptor: SerialDescriptor
get() = MenuButtonSurrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): MenuButton {
val surrogate = if (decoder is JsonDecoder) {
val json = JsonElement.serializer().deserialize(decoder)
runCatching {
decoder.json.decodeFromJsonElement(MenuButtonSurrogate.serializer(), json)
}.onFailure {
return MenuButton.Unknown(
runCatching { json.jsonObject[typeField] ?.jsonPrimitive ?.content }.getOrNull() ?: "",
json
)
}.getOrThrow().copy(
srcJsonElement = json
)
} else {
MenuButtonSurrogate.serializer().deserialize(decoder)
}
return when (surrogate.type) {
MenuButton.Commands.type -> MenuButton.Commands
MenuButton.Default.type -> MenuButton.Default
MenuButton.WebApp.type -> if (surrogate.text != null && surrogate.webApp != null) {
MenuButton.WebApp(surrogate.text, surrogate.webApp)
} else {
null
}
else -> null
} ?: MenuButton.Unknown(
surrogate.type,
surrogate.srcJsonElement ?: buildJsonObject { }
)
}
override fun serialize(encoder: Encoder, value: MenuButton) {
encoder.encodeSerializableValue(
MenuButtonSurrogate.serializer(),
when (value) {
MenuButton.Default,
MenuButton.Commands -> MenuButtonSurrogate(value.type)
is MenuButton.WebApp -> MenuButtonSurrogate(value.type, value.text, value.webApp)
is MenuButton.Unknown -> {
encoder.encodeSerializableValue(
JsonElement.serializer(),
value.rawJson
)
return
}
}
)
}
}

View File

@@ -33,7 +33,12 @@ inline fun mention(parts: TextSourcesList, id: Identifier) = mention(parts, User
@Suppress("NOTHING_TO_INLINE")
inline fun Identifier.mention(parts: TextSourcesList) = mention(parts, this)
@Suppress("NOTHING_TO_INLINE")
inline fun mention(user: User, vararg parts: TextSource) = mention(parts.toList(), user)
inline fun mention(user: User, vararg parts: TextSource) = mention(
textSourcesOrElseTextSource(parts.toList()) {
RegularTextSource("${user.lastName} ${user.firstName}")
},
user
)
@Suppress("NOTHING_TO_INLINE")
inline fun mention(text: String, user: User) = mention(user, regular(text))
@Suppress("NOTHING_TO_INLINE")

View File

@@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.types.MessageEntity.textsources
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlin.js.JsName
import kotlin.jvm.JvmName
@RiskFeature
inline fun textSourcesOrElse(
textSources: TextSourcesList,
block: () -> TextSourcesList
): TextSourcesList = textSources.takeIf { it.isNotEmpty() } ?: block()
@RiskFeature
inline fun textSourcesOrElseTextSource(
textSources: TextSourcesList,
block: () -> TextSource
): TextSourcesList = textSources.takeIf { it.isNotEmpty() } ?: listOf(block())

View File

@@ -16,6 +16,8 @@ data class WebhookInfo(
val allowedUpdates: List<String> = ALL_UPDATES_LIST,
@SerialName(lastErrorDateField)
val lastErrorDate: TelegramDate? = null,
@SerialName(lastSynchronizationErrorDateField)
val lastSynchronizationErrorDate: TelegramDate? = null,
@SerialName(lastErrorMessageField)
val lastErrorMessage: String? = null
) {

View File

@@ -2,8 +2,9 @@ package dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.games.CallbackGame
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import kotlinx.serialization.*
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.*
/**
* Some button of [dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup]. See inheritors and visit
@@ -15,10 +16,14 @@ sealed interface InlineKeyboardButton {
}
@Serializable
data class UnknownInlineKeyboardButton internal constructor(
override val text: String,
data class UnknownInlineKeyboardButton (
val rawData: JsonElement
) : InlineKeyboardButton
) : InlineKeyboardButton {
override val text: String
get() = runCatching {
rawData.jsonObject[textField] ?.jsonPrimitive ?.content
}.getOrNull() ?: ""
}
/**
* This type of button must always be the first button in the first row. Visit
@@ -116,3 +121,14 @@ data class URLInlineKeyboardButton(
@SerialName(urlField)
val url: String
) : InlineKeyboardButton
/**
* Button with [WebAppInfo]. Web App will be launched when the button is pressed. The Web App will be able to send a
* `web_app_data` service message. **Available in private chats only**.
*/
@Serializable
data class WebAppInlineKeyboardButton(
override val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : InlineKeyboardButton

View File

@@ -37,7 +37,7 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
return (json as? JsonObject) ?.let { resolveSerializer(it) } ?.let {
nonstrictJsonFormat.decodeFromJsonElement(it, json)
} ?: UnknownInlineKeyboardButton("", json)
} ?: UnknownInlineKeyboardButton(json)
}
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
@@ -48,6 +48,7 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
is WebAppInlineKeyboardButton -> WebAppInlineKeyboardButton.serializer().serialize(encoder, value)
is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value)
is UnknownInlineKeyboardButton -> JsonElement.serializer().serialize(encoder, value.rawData)
}

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.buttons
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.*
@@ -63,10 +64,22 @@ data class RequestLocationKeyboardButton(
override val text: String
) : KeyboardButton {
@SerialName(requestLocationField)
@EncodeDefault
@Required
val requestLocation: Boolean = true
}
/**
* Private chats only. Description of the Web App that will be launched when the user presses the button. The Web App
* will be able to send an arbitrary message on behalf of the user using the method `answerWebAppQuery`. Available only
* in private chats between a user and the bot.
*/
@Serializable
data class WebAppKeyboardButton(
override val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : KeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the poll with [requestPoll] options. You will be able
* to catch this poll in updates and data using [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPoll] in
@@ -97,6 +110,13 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
asJson is JsonObject && asJson[requestLocationField] != null -> RequestLocationKeyboardButton(
asJson[textField]!!.jsonPrimitive.content
)
asJson is JsonObject && asJson[webAppField] != null -> WebAppKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
WebAppInfo.serializer(),
asJson[webAppField]!!
)
)
asJson is JsonObject && asJson[requestPollField] != null -> RequestPollKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
@@ -119,6 +139,7 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
when (value) {
is RequestContactKeyboardButton -> RequestContactKeyboardButton.serializer().serialize(encoder, value)
is RequestLocationKeyboardButton -> RequestLocationKeyboardButton.serializer().serialize(encoder, value)
is WebAppKeyboardButton -> WebAppKeyboardButton.serializer().serialize(encoder, value)
is RequestPollKeyboardButton -> RequestPollKeyboardButton.serializer().serialize(encoder, value)
is SimpleKeyboardButton -> encoder.encodeString(value.text)
is UnknownKeyboardButton -> JsonElement.serializer().serialize(encoder, nonstrictJsonFormat.parseToJsonElement(value.raw))

View File

@@ -22,11 +22,14 @@ private class SurrogateBotCommandScope(
BotCommandScopeAllGroupChats.type -> BotCommandScopeAllGroupChats
BotCommandScopeAllChatAdministrators.type -> BotCommandScopeAllChatAdministrators
BotCommandScopeChatAdministrators.type -> BotCommandScopeChatAdministrators(
chatId ?: error("chat_administrators type must have $chatIdField field, but have no")
chatId ?: error("${BotCommandScopeChatAdministrators.type} type must have $chatIdField field, but have no")
)
BotCommandScopeChatMember.type -> BotCommandScopeChatMember(
chatId ?: error("chat_administrators type must have $chatIdField field, but have no"),
userId ?: error("chat_administrators type must have $userIdField field, but have no")
chatId ?: error("${BotCommandScopeChatMember.type} type must have $chatIdField field, but have no"),
userId ?: error("${BotCommandScopeChatMember.type} type must have $userIdField field, but have no")
)
BotCommandScopeChat.type -> BotCommandScopeChat(
chatId ?: error("${BotCommandScopeChat.type} type must have $chatIdField field, but have no")
)
else -> UnknownBotCommandScope(type)
}
@@ -40,6 +43,7 @@ private class SurrogateBotCommandScope(
BotCommandScopeAllChatAdministrators -> SurrogateBotCommandScope(scope.type)
is BotCommandScopeChatAdministrators -> SurrogateBotCommandScope(scope.type, scope.chatId)
is BotCommandScopeChatMember -> SurrogateBotCommandScope(scope.type, scope.chatId, scope.userId)
is BotCommandScopeChat -> SurrogateBotCommandScope(scope.type, scope.chatId)
}
}
}
@@ -47,6 +51,16 @@ private class SurrogateBotCommandScope(
@Serializable(BotCommandScopeSerializer::class)
sealed interface BotCommandScope {
val type: String
companion object {
val Default = BotCommandScopeDefault
val AllPrivateChats = BotCommandScopeAllPrivateChats
val AllGroupChats = BotCommandScopeAllGroupChats
val AllChatAdministrators = BotCommandScopeAllChatAdministrators
fun ChatAdministrators(chatId: ChatIdentifier) = BotCommandScopeChatAdministrators(chatId)
fun Chat(chatId: ChatIdentifier) = BotCommandScopeChat(chatId)
fun ChatMember(chatId: ChatIdentifier, userId: UserId) = BotCommandScopeChatMember(chatId, userId)
}
}
@Serializable
@@ -94,6 +108,17 @@ data class BotCommandScopeChatAdministrators(
}
}
@Serializable
data class BotCommandScopeChat(
override val chatId: ChatIdentifier
) : ChatBotCommandScope {
@Required
override val type: String = BotCommandScopeChat.type
companion object {
const val type = "chat"
}
}
@Serializable
data class BotCommandScopeChatMember(
override val chatId: ChatIdentifier,

View File

@@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.types.message.ChatEvents
import dev.inmo.tgbotapi.types.buttonTextField
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PrivateEvent
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class WebAppData(
@SerialName(dataField)
val data: String,
@SerialName(buttonTextField)
val buttonText: String
) : PrivateEvent

View File

@@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.abstracts
import dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatScheduled
interface VideoChatEvent : PublicChatEvent
@Deprecated("Renamed", ReplaceWith("VideoChatEvent", "dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatEvent"))
typealias VoiceChatEvent = VideoChatEvent

View File

@@ -1,3 +0,0 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.abstracts
interface VoiceChatEvent : PublicChatEvent

View File

@@ -4,15 +4,18 @@ import com.soywiz.klock.TimeSpan
import com.soywiz.klock.seconds
import dev.inmo.tgbotapi.types.Seconds
import dev.inmo.tgbotapi.types.durationField
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VoiceChatEvent
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VideoChatEvent
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class VoiceChatEnded(
data class VideoChatEnded(
@SerialName(durationField)
val duration: Seconds
) : VoiceChatEvent {
) : VideoChatEvent {
val timeSpan: TimeSpan
get() = TimeSpan(duration.seconds.milliseconds)
}
@Deprecated("Renamed", ReplaceWith("VideoChatEnded", "dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatEnded"))
typealias VoiceChatEnded = VideoChatEnded

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.voice
import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VideoChatEvent
import dev.inmo.tgbotapi.types.usersField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class VideoChatParticipantsInvited(
@SerialName(usersField)
val users: List<User> = emptyList()
) : VideoChatEvent
@Deprecated("Renamed", ReplaceWith("VideoChatParticipantsInvited", "dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatParticipantsInvited"))
typealias VoiceChatParticipantsInvited = VideoChatParticipantsInvited

View File

@@ -1,13 +1,16 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.voice
import dev.inmo.tgbotapi.types.TelegramDate
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VoiceChatEvent
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VideoChatEvent
import dev.inmo.tgbotapi.types.startDateField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class VoiceChatScheduled(
data class VideoChatScheduled(
@SerialName(startDateField)
val startDate: TelegramDate
) : VoiceChatEvent
) : VideoChatEvent
@Deprecated("Renamed", ReplaceWith("VideoChatScheduled", "dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatScheduled"))
typealias VoiceChatScheduled = VideoChatScheduled

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.voice
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VideoChatEvent
import kotlinx.serialization.Serializable
@Serializable
object VideoChatStarted : VideoChatEvent
@Deprecated("Renamed", ReplaceWith("VideoChatStarted", "dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatStarted"))
typealias VoiceChatStarted = VideoChatStarted

View File

@@ -1,13 +0,0 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.voice
import dev.inmo.tgbotapi.types.User
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VoiceChatEvent
import dev.inmo.tgbotapi.types.usersField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class VoiceChatParticipantsInvited(
@SerialName(usersField)
val users: List<User> = emptyList()
) : VoiceChatEvent

View File

@@ -1,7 +0,0 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.voice
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.VoiceChatEvent
import kotlinx.serialization.Serializable
@Serializable
object VoiceChatStarted : VoiceChatEvent

View File

@@ -84,10 +84,10 @@ internal data class RawMessage(
private val successful_payment: SuccessfulPayment? = null,
// Voice Chat Service Messages
private val voice_chat_scheduled: VoiceChatScheduled? = null,
private val voice_chat_started: VoiceChatStarted? = null,
private val voice_chat_ended: VoiceChatEnded? = null,
private val voice_chat_participants_invited: VoiceChatParticipantsInvited? = null,
private val video_chat_scheduled: VideoChatScheduled? = null,
private val video_chat_started: VideoChatStarted? = null,
private val video_chat_ended: VideoChatEnded? = null,
private val video_chat_participants_invited: VideoChatParticipantsInvited? = null,
// AutoDelete Message time changed
private val message_auto_delete_timer_changed: MessageAutoDeleteTimerChanged? = null,
@@ -95,6 +95,9 @@ internal data class RawMessage(
// login property
private val connected_website: String? = null,
// login property
private val web_app_data: WebAppData? = null,
// passport property
private val passport_data: PassportData? = null,
private val proximity_alert_triggered: ProximityAlertTriggered? = null,
@@ -183,11 +186,11 @@ internal data class RawMessage(
left_chat_member != null -> LeftChatMember(left_chat_member)
new_chat_title != null -> NewChatTitle(new_chat_title)
new_chat_photo != null -> NewChatPhoto(new_chat_photo.toList())
voice_chat_started != null -> voice_chat_started
voice_chat_scheduled != null -> voice_chat_scheduled
video_chat_started != null -> video_chat_started
video_chat_scheduled != null -> video_chat_scheduled
message_auto_delete_timer_changed != null -> message_auto_delete_timer_changed
voice_chat_ended != null -> voice_chat_ended
voice_chat_participants_invited != null -> voice_chat_participants_invited
video_chat_ended != null -> video_chat_ended
video_chat_participants_invited != null -> video_chat_participants_invited
delete_chat_photo -> DeleteChatPhoto()
group_chat_created -> GroupChatCreated(
migrate_to_chat_id
@@ -203,6 +206,7 @@ internal data class RawMessage(
proximity_alert_triggered != null -> proximity_alert_triggered
successful_payment != null -> SuccessfulPaymentEvent(successful_payment)
connected_website != null -> UserLoggedIn(connected_website)
web_app_data != null -> web_app_data
else -> null
}
}

View File

@@ -20,8 +20,7 @@ interface MessageContent: ResendableContent {
subclass(DiceContent::class)
subclass(TextContent::class)
subclass(LiveLocationContent::class)
subclass(StaticLocationContent::class)
subclass(LocationContent::class, LocationContentSerializer)
subclass(PhotoContent::class)
subclass(VideoContent::class)

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.types.webapps
import dev.inmo.tgbotapi.types.urlField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class WebAppInfo(
@SerialName(urlField)
val url: String
)

View File

@@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.types.webapps.query
import dev.inmo.tgbotapi.types.InlineMessageIdentifier
import kotlinx.serialization.Serializable
@Serializable
data class SentWebAppMessage(
val inlineMessageId: InlineMessageIdentifier? = null
)

View File

@@ -0,0 +1,19 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.crypto.SourceString
internal expect fun SourceString.hmacSha256(key: String): String
private val HEX_ARRAY = "0123456789abcdef".toCharArray()
internal fun SourceBytes.hex(): String {
val hexChars = CharArray(size * 2)
for (j in indices) {
val v: Int = this[j].toInt() and 0xFF
hexChars[j * 2] = HEX_ARRAY[v ushr 4]
hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F]
}
return hexChars.concatToString()
}
internal fun SourceString.hex(): String = encodeToByteArray().hex()

View File

@@ -1,6 +1,8 @@
package dev.inmo.tgbotapi.utils
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.common.filename
import io.ktor.utils.io.*
import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.Input
@@ -13,6 +15,7 @@ import kotlinx.serialization.Serializable
* @param fileName This filename will be used in telegram system as name of file
*/
@Serializable
@Deprecated("Will be removed soon")
data class StorageFileInfo(
val fileName: String
) {
@@ -25,18 +28,35 @@ data class StorageFileInfo(
/**
* Contains info about file, which potentially can be sent to telegram system.
*
* @param storageFileInfo Information about this file
* @param fileName Filename
* @param inputSource Lambda which able to allocate [Input] for uploading/manipulating data
*
* @see StorageFileInfo
* @see asStorageFile
*/
data class StorageFile(
val storageFileInfo: StorageFileInfo,
val fileName: String,
private val inputSource: () -> Input
) {
val input: Input
get() = inputSource()
@Deprecated("This field will be removed soon. Use fileName instead of StorageFileInfo")
val storageFileInfo: StorageFileInfo
get() = StorageFileInfo(fileName)
/**
* This methods is required for random generation of name for keeping warranties about unique file name
*/
fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}"
@Deprecated("This constructor will be removed soon. Use constructor with fileName instead of StorageFileInfo")
constructor(
storageFileInfo: StorageFileInfo,
inputSource: () -> Input
) : this(
storageFileInfo.fileName,
inputSource
)
}
@Suppress("NOTHING_TO_INLINE")
@@ -44,7 +64,7 @@ inline fun StorageFile(
fileName: String,
bytes: ByteArray
) = StorageFile(
StorageFileInfo(fileName)
fileName
) {
ByteReadPacket(bytes)
}
@@ -54,8 +74,8 @@ suspend inline fun StorageFile(
fileName: String,
byteReadChannel: ByteReadChannel
) = StorageFile(
StorageFileInfo(fileName),
byteReadChannel.asInput().let { { it } }
fileName,
inputSource = byteReadChannel.asInput().let { { it } }
)
@Suppress("NOTHING_TO_INLINE", "unused")

View File

@@ -16,16 +16,33 @@ private inline val String.withoutLastSlash: String
class TelegramAPIUrlsKeeper(
token: String,
hostUrl: String = telegramBotAPIDefaultUrl
hostUrl: String = telegramBotAPIDefaultUrl,
urlsSuffixes: String = ""
) {
val webAppDataSecretKey by lazy {
token.hmacSha256("WebAppData")
}
val commonAPIUrl: String
val fileBaseUrl: String
constructor(token: String, testServer: Boolean, hostUrl: String = telegramBotAPIDefaultUrl) : this(
token,
hostUrl,
"/test".takeIf { testServer } ?: ""
)
init {
val correctedHost = hostUrl.withoutLastSlash
commonAPIUrl = "$correctedHost/bot$token"
fileBaseUrl = "$correctedHost/file/bot$token"
commonAPIUrl = "$correctedHost/bot$token$urlsSuffixes"
fileBaseUrl = "$correctedHost/file/bot$token$urlsSuffixes"
}
fun createFileLinkUrl(filePath: String) = "${fileBaseUrl}/$filePath"
/**
* @param rawData Data from [dev.inmo.tgbotapi.webapps.WebApp.initData]
* @param hash Data from [dev.inmo.tgbotapi.webapps.WebApp.initDataUnsafe] from the field [dev.inmo.tgbotapi.webapps.WebAppInitData.hash]
*/
fun checkWebAppLink(rawData: String, hash: String) = rawData.hmacSha256(webAppDataSecretKey).hex() == hash
}

View File

@@ -0,0 +1,6 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.CryptoJS
import dev.inmo.micro_utils.crypto.SourceString
actual fun SourceString.hmacSha256(key: String) = CryptoJS.asDynamic().HmacSHA256(this, key).toString().unsafeCast<String>()

View File

@@ -0,0 +1,14 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.crypto.SourceString
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
actual fun SourceString.hmacSha256(key: String): String {
val mac = Mac.getInstance("HmacSHA256")
val secretKey = SecretKeySpec(key.toByteArray(), "HmacSHA256")
mac.init(secretKey)
return mac.doFinal(toByteArray()).hex()
}

View File

@@ -7,7 +7,7 @@ import java.nio.file.Files
fun StorageFile(
file: File
) = StorageFile(
StorageFileInfo(file.name)
file.name
) {
file.inputStream().asInput()
}

View File

@@ -2516,6 +2516,16 @@ inline fun InlineKeyboardButton.asURLInlineKeyboardButton(): URLInlineKeyboardBu
inline fun InlineKeyboardButton.requireURLInlineKeyboardButton(): URLInlineKeyboardButton =
this as URLInlineKeyboardButton
@PreviewFeature
inline fun <T> InlineKeyboardButton.whenWebAppKeyboardButton(block: (WebAppKeyboardButton) -> T) = asWebAppKeyboardButton() ?.let(block)
@PreviewFeature
inline fun InlineKeyboardButton.asWebAppKeyboardButton(): WebAppKeyboardButton? = this as? WebAppKeyboardButton
@PreviewFeature
inline fun InlineKeyboardButton.requireWebAppKeyboardButton(): WebAppKeyboardButton =
this as WebAppKeyboardButton
@PreviewFeature
inline fun <T> InlineKeyboardButton.whenUnknownInlineKeyboardButton(block: (UnknownInlineKeyboardButton) -> T) = asUnknownInlineKeyboardButton() ?.let(block)
@@ -3191,51 +3201,113 @@ inline fun ChatEvent.asSupergroupEvent(): SupergroupEvent? = this as? Supergroup
inline fun ChatEvent.requireSupergroupEvent(): SupergroupEvent = this as SupergroupEvent
@PreviewFeature
inline fun <T> ChatEvent.whenVoiceChatEvent(block: (VoiceChatEvent) -> T) = asVoiceChatEvent() ?.let(block)
@Deprecated("Renamed as Video instead of Voice")
inline fun <T> ChatEvent.whenVoiceChatEvent(block: (VideoChatEvent) -> T) = asVoiceChatEvent() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVoiceChatEvent(): VoiceChatEvent? = this as? VoiceChatEvent
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.asVoiceChatEvent(): VideoChatEvent? = this as? VideoChatEvent
@PreviewFeature
inline fun ChatEvent.requireVoiceChatEvent(): VoiceChatEvent = this as VoiceChatEvent
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.requireVoiceChatEvent(): VideoChatEvent = this as VideoChatEvent
@PreviewFeature
inline fun <T> ChatEvent.whenVoiceChatEnded(block: (VoiceChatEnded) -> T) = asVoiceChatEnded() ?.let(block)
@Deprecated("Renamed as Video instead of Voice")
inline fun <T> ChatEvent.whenVoiceChatEnded(block: (VideoChatEnded) -> T) = asVoiceChatEnded() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVoiceChatEnded(): VoiceChatEnded? = this as? VoiceChatEnded
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.asVoiceChatEnded(): VideoChatEnded? = this as? VideoChatEnded
@PreviewFeature
inline fun ChatEvent.requireVoiceChatEnded(): VoiceChatEnded = this as VoiceChatEnded
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.requireVoiceChatEnded(): VideoChatEnded = this as VideoChatEnded
@PreviewFeature
inline fun <T> ChatEvent.whenVoiceChatParticipantsInvited(block: (VoiceChatParticipantsInvited) -> T) = asVoiceChatParticipantsInvited() ?.let(block)
@Deprecated("Renamed as Video instead of Voice")
inline fun <T> ChatEvent.whenVoiceChatParticipantsInvited(block: (VideoChatParticipantsInvited) -> T) = asVoiceChatParticipantsInvited() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVoiceChatParticipantsInvited(): VoiceChatParticipantsInvited? =
this as? VoiceChatParticipantsInvited
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.asVoiceChatParticipantsInvited(): VideoChatParticipantsInvited? =
this as? VideoChatParticipantsInvited
@PreviewFeature
inline fun ChatEvent.requireVoiceChatParticipantsInvited(): VoiceChatParticipantsInvited =
this as VoiceChatParticipantsInvited
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.requireVoiceChatParticipantsInvited(): VideoChatParticipantsInvited =
this as VideoChatParticipantsInvited
@PreviewFeature
inline fun <T> ChatEvent.whenVoiceChatStarted(block: (VoiceChatStarted) -> T) = asVoiceChatStarted() ?.let(block)
@Deprecated("Renamed as Video instead of Voice")
inline fun <T> ChatEvent.whenVoiceChatStarted(block: (VideoChatStarted) -> T) = asVoiceChatStarted() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVoiceChatStarted(): VoiceChatStarted? = this as? VoiceChatStarted
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.asVoiceChatStarted(): VideoChatStarted? = this as? VideoChatStarted
@PreviewFeature
inline fun ChatEvent.requireVoiceChatStarted(): VoiceChatStarted = this as VoiceChatStarted
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.requireVoiceChatStarted(): VideoChatStarted = this as VideoChatStarted
@PreviewFeature
inline fun <T> ChatEvent.whenVoiceChatScheduled(block: (VoiceChatScheduled) -> T) = asVoiceChatScheduled() ?.let(block)
@Deprecated("Renamed as Video instead of Voice")
inline fun <T> ChatEvent.whenVoiceChatScheduled(block: (VideoChatScheduled) -> T) = asVoiceChatScheduled() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVoiceChatScheduled(): VoiceChatScheduled? = this as? VoiceChatScheduled
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.asVoiceChatScheduled(): VideoChatScheduled? = this as? VideoChatScheduled
@PreviewFeature
inline fun ChatEvent.requireVoiceChatScheduled(): VoiceChatScheduled = this as VoiceChatScheduled
@Deprecated("Renamed as Video instead of Voice")
inline fun ChatEvent.requireVoiceChatScheduled(): VideoChatScheduled = this as VideoChatScheduled
@PreviewFeature
inline fun <T> ChatEvent.whenVideoChatEvent(block: (VideoChatEvent) -> T) = asVideoChatEvent() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVideoChatEvent(): VideoChatEvent? = this as? VideoChatEvent
@PreviewFeature
inline fun ChatEvent.requireVideoChatEvent(): VideoChatEvent = this as VideoChatEvent
@PreviewFeature
inline fun <T> ChatEvent.whenVideoChatEnded(block: (VideoChatEnded) -> T) = asVideoChatEnded() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVideoChatEnded(): VideoChatEnded? = this as? VideoChatEnded
@PreviewFeature
inline fun ChatEvent.requireVideoChatEnded(): VideoChatEnded = this as VideoChatEnded
@PreviewFeature
inline fun <T> ChatEvent.whenVideoChatParticipantsInvited(block: (VideoChatParticipantsInvited) -> T) = asVideoChatParticipantsInvited() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVideoChatParticipantsInvited(): VideoChatParticipantsInvited? =
this as? VideoChatParticipantsInvited
@PreviewFeature
inline fun ChatEvent.requireVideoChatParticipantsInvited(): VideoChatParticipantsInvited =
this as VideoChatParticipantsInvited
@PreviewFeature
inline fun <T> ChatEvent.whenVideoChatStarted(block: (VideoChatStarted) -> T) = asVideoChatStarted() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVideoChatStarted(): VideoChatStarted? = this as? VideoChatStarted
@PreviewFeature
inline fun ChatEvent.requireVideoChatStarted(): VideoChatStarted = this as VideoChatStarted
@PreviewFeature
inline fun <T> ChatEvent.whenVideoChatScheduled(block: (VideoChatScheduled) -> T) = asVideoChatScheduled() ?.let(block)
@PreviewFeature
inline fun ChatEvent.asVideoChatScheduled(): VideoChatScheduled? = this as? VideoChatScheduled
@PreviewFeature
inline fun ChatEvent.requireVideoChatScheduled(): VideoChatScheduled = this as VideoChatScheduled
@PreviewFeature
inline fun <T> ChatEvent.whenUserLoggedIn(block: (UserLoggedIn) -> T) = asUserLoggedIn() ?.let(block)

View File

@@ -177,17 +177,33 @@ inline val Message.successful_payment: SuccessfulPayment?
get() = asChatEventMessage() ?.chatEvent ?.asSuccessfulPaymentEvent() ?.payment
@RiskFeature(RawFieldsUsageWarning)
inline val Message.voice_chat_scheduled: VoiceChatScheduled?
@Deprecated("Renamed as video instead of voice")
inline val Message.voice_chat_scheduled: VideoChatScheduled?
get() = asChatEventMessage() ?.chatEvent ?.asVoiceChatScheduled()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.voice_chat_started: VoiceChatStarted?
@Deprecated("Renamed as video instead of voice")
inline val Message.voice_chat_started: VideoChatStarted?
get() = asChatEventMessage() ?.chatEvent ?.asVoiceChatStarted()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.voice_chat_ended: VoiceChatEnded?
@Deprecated("Renamed as video instead of voice")
inline val Message.voice_chat_ended: VideoChatEnded?
get() = asChatEventMessage() ?.chatEvent ?.asVoiceChatEnded()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.voice_chat_participants_invited: VoiceChatParticipantsInvited?
@Deprecated("Renamed as video instead of voice")
inline val Message.voice_chat_participants_invited: VideoChatParticipantsInvited?
get() = asChatEventMessage() ?.chatEvent ?.asVoiceChatParticipantsInvited()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.video_chat_scheduled: VideoChatScheduled?
get() = asChatEventMessage() ?.chatEvent ?.asVideoChatScheduled()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.video_chat_started: VideoChatStarted?
get() = asChatEventMessage() ?.chatEvent ?.asVideoChatStarted()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.video_chat_ended: VideoChatEnded?
get() = asChatEventMessage() ?.chatEvent ?.asVideoChatEnded()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.video_chat_participants_invited: VideoChatParticipantsInvited?
get() = asChatEventMessage() ?.chatEvent ?.asVideoChatParticipantsInvited()
@RiskFeature(RawFieldsUsageWarning)
inline val Message.message_auto_delete_timer_changed: MessageAutoDeleteTimerChanged?

View File

@@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.LoginURL
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.MatrixBuilder
import dev.inmo.tgbotapi.utils.RowBuilder
@@ -128,3 +129,25 @@ inline fun InlineKeyboardRowBuilder.urlButton(
text: String,
url: String
) = add(URLInlineKeyboardButton(text, url))
/**
* Creates and put [WebAppInlineKeyboardButton]. Please, remember that this button is available in private chats only
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.webAppButton(
text: String,
webApp: WebAppInfo
) = add(WebAppInlineKeyboardButton(text, webApp))
/**
* Creates and put [WebAppInlineKeyboardButton]. Please, remember that this button is available in private chats only
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.webAppButton(
text: String,
url: String
) = webAppButton(text, WebAppInfo(url))

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.buttons.*
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.MatrixBuilder
import dev.inmo.tgbotapi.utils.RowBuilder
@@ -98,3 +99,14 @@ inline fun ReplyKeyboardRowBuilder.requestPollButton(
text: String,
pollType: KeyboardButtonPollType
) = add(RequestPollKeyboardButton(text, pollType))
/**
* Creates and put [WebAppKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.webAppButton(
text: String,
webApp: WebAppInfo
) = add(WebAppKeyboardButton(text, webApp))

View File

@@ -0,0 +1,56 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
project.version = "$library_version"
project.group = "$library_group"
project.description = "Web App bindings for the Telegram Web Apps API"
apply from: "../publish.gradle"
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
jvm {
compilations.main {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
js(IR) {
browser()
nodejs()
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib')
api project(":tgbotapi.core")
}
}
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

View File

@@ -0,0 +1,6 @@
package dev.inmo.tgbotapi.webapps
enum class ColorScheme {
LIGHT,
DARK
}

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.webapps
import dev.inmo.micro_utils.crypto.CryptoJs
fun CryptoJs.HmacSHA256(text: String, key: String) = this.asDynamic().HmacSHA256(text, key).unsafeCast<String>()
fun CryptoJs.hex(text: String) = this.asDynamic().format.Hex(text).unsafeCast<String>()

View File

@@ -0,0 +1,4 @@
package dev.inmo.tgbotapi.webapps
typealias EventHandler = WebApp.() -> Unit
typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.webapps
sealed class EventType(val typeName: String) {
object ThemeChanged : EventType("themeChanged")
object ViewportChanged : EventType("viewportChanged")
object MainButtonClicked : EventType("mainButtonClicked")
}

View File

@@ -0,0 +1,55 @@
package dev.inmo.tgbotapi.webapps
import kotlin.js.Json
import kotlin.js.json
external class MainButton {
val text: String
fun setText(text: String): MainButton
var color: String
var textColor: String
val isVisible: Boolean
fun show(): MainButton
fun hide(): MainButton
val isActive: Boolean
fun enable(): MainButton
fun disable(): MainButton
val isProgressVisible: Boolean
fun showProgress(leaveActive: Boolean = definedExternally): MainButton
fun hideProgress(): MainButton
internal fun onClick(eventHandler: () -> Unit): MainButton
internal fun setParams(params: Json): MainButton
}
data class MainButtonParams(
val text: String? = null,
val color: String? = null,
val textColor: String? = null,
val isActive: Boolean? = null,
val isVisible: Boolean? = null
)
fun MainButton.onClick(eventHandler: EventHandler) = onClick {
val that = js("this").unsafeCast<WebApp>()
that.eventHandler()
}
fun MainButton.setParams(params: MainButtonParams) = setParams(
json(
*listOfNotNull(
params.text ?.let { "text" to params.text },
params.color ?.let { "color" to params.color },
params.textColor ?.let { "text_color" to params.textColor },
params.isActive ?.let { "is_active" to params.isActive },
params.isVisible ?.let { "is_visible" to params.isVisible },
).toTypedArray()
)
)

View File

@@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.webapps
import kotlinx.browser.window
import org.w3c.dom.Window
external interface Telegram {
val WebApp: WebApp
}
val Window.Telegram
get() = asDynamic().Telegram.unsafeCast<Telegram>()
val telegram: Telegram
get() = window.Telegram
val webApp: WebApp
get() = telegram.WebApp

View File

@@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.webapps
external interface ThemeParams {
@JsName("bg_color")
val backgroundColor: String?
@JsName("text_color")
val textColor: String?
@JsName("hint_color")
val hintColor: String?
@JsName("link_color")
val linkColor: String?
@JsName("button_color")
val buttonColor: String?
@JsName("button_text_color")
val buttonTextColor: String?
}

View File

@@ -0,0 +1,5 @@
package dev.inmo.tgbotapi.webapps
external interface ViewportChangedData {
val isStateStable: Boolean
}

View File

@@ -0,0 +1,83 @@
package dev.inmo.tgbotapi.webapps
import dev.inmo.micro_utils.crypto.CryptoJS
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
external class WebApp {
val initData: String
val initDataUnsafe: WebAppInitData
@JsName("colorScheme")
val colorSchemeRaw: String
val themeParams: ThemeParams
val isExpanded: Boolean
val viewportHeight: Float
val viewportStableHeight: Float
@JsName("MainButton")
val mainButton: MainButton
internal fun onEvent(type: String, callback: () -> Unit)
@JsName("onEvent")
internal fun onEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit)
fun offEvent(type: String, callback: () -> Unit)
@JsName("offEvent")
fun offEventWithBoolean(type: String, callback: (ViewportChangedData) -> Unit)
fun sendData(data: String)
fun ready()
fun expand()
fun close()
}
val WebApp.colorScheme: ColorScheme
get() = when (colorSchemeRaw) {
"light" -> ColorScheme.LIGHT
"dark" -> ColorScheme.DARK
else -> ColorScheme.LIGHT
}
/**
* @return The callback which should be used in case you want to turn off events handling
*/
fun WebApp.onEvent(type: EventType, eventHandler: EventHandler) = {
eventHandler(js("this").unsafeCast<WebApp>())
}.also {
onEvent(
type.typeName,
callback = it
)
}
/**
* @return The callback which should be used in case you want to turn off events handling
*/
fun WebApp.onEvent(type: EventType.ViewportChanged, eventHandler: ViewportChangedEventHandler) = { it: ViewportChangedData ->
eventHandler(js("this").unsafeCast<WebApp>(), it)
}.also {
onEventWithBoolean(
type.typeName,
callback = it
)
}
/**
* @return The callback which should be used in case you want to turn off events handling
*/
fun WebApp.onThemeChanged(eventHandler: EventHandler) = onEvent(EventType.ThemeChanged, eventHandler)
/**
* @return The callback which should be used in case you want to turn off events handling
*/
fun WebApp.onMainButtonClicked(eventHandler: EventHandler) = onEvent(EventType.MainButtonClicked, eventHandler)
/**
* @return The callback which should be used in case you want to turn off events handling
*/
fun WebApp.onViewportChanged(eventHandler: ViewportChangedEventHandler) = onEvent(EventType.ViewportChanged, eventHandler)
fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppLink(
initData,
initDataUnsafe.hash
)

View File

@@ -0,0 +1,19 @@
package dev.inmo.tgbotapi.webapps
import dev.inmo.tgbotapi.types.MilliSeconds
import dev.inmo.tgbotapi.types.WebAppQueryId
external interface WebAppInitData {
val queryId: WebAppQueryId?
val user: WebAppUser?
val receiver: WebAppUser?
@JsName("start_param")
val startParam: String?
@JsName("auth_date")
val authDate: MilliSeconds
val hash: String
}

View File

@@ -0,0 +1,37 @@
package dev.inmo.tgbotapi.webapps
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.tgbotapi.types.*
external interface WebAppUser {
val id: Identifier
@JsName(isBotField)
val isBot: Boolean?
@JsName(firstNameField)
val firstName: String
@JsName(lastNameField)
val lastName: String?
@JsName(usernameField)
val username: String?
@JsName(languageCodeField)
val languageCode: String?
@JsName(photoUrlField)
val photoUrl: String?
}
fun WebAppUser.asUser() = if (isBot == true) {
CommonBot(
UserId(id),
username ?.let(::Username) ?: error("Username is absent for bot, but must exists"),
firstName,
lastName ?: ""
)
} else {
CommonUser(
UserId(id),
firstName,
lastName ?: "",
username ?.let(::Username),
languageCode ?.let(::IetfLanguageCode)
)
}