mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-11-17 20:40:20 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d6709baf4 | |||
| b50ad30176 | |||
| 887c51f83a | |||
| c193b512d9 | |||
| ee055873e6 | |||
| 6db6209c88 | |||
| e92563e85f | |||
| 03f2f0e25b | |||
| 4197e13c54 | |||
| e09ea9a9b4 | |||
| 2a32654d57 | |||
| 0fff553ce1 | |||
| 33a1701f5b | |||
| f9a9f958ba | |||
| f686be0271 | |||
| 91307f3ebf | |||
| 8e64205f53 | |||
| 5434df1f02 | |||
| e56199ac9f | |||
| 3e199c6944 | |||
| b43d9aefb9 | |||
| fe133bbde0 | |||
| 332fe95adf | |||
| 1f416d4a28 | |||
| e626d8b5cc | |||
| b906d605f4 | |||
| d3584e793c | |||
| f71ac51461 | |||
| 5fe8cf948a | |||
| 85ea101641 |
29
CHANGELOG.md
29
CHANGELOG.md
@@ -1,5 +1,34 @@
|
|||||||
# TelegramBotAPI changelog
|
# TelegramBotAPI changelog
|
||||||
|
|
||||||
|
## 4.2.4
|
||||||
|
|
||||||
|
* `Core`:
|
||||||
|
* Fixes in webhook parts adapter
|
||||||
|
* `BehaviourBuilderWithFSM`:
|
||||||
|
* Fixes in `DefaultBehaviourContextWithFSM`
|
||||||
|
|
||||||
|
## 4.2.3
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `MicroUtils`: `0.16.2` -> `0.16.4`
|
||||||
|
* `Core`:
|
||||||
|
* Simplify default `RequestsLimiter` (`ExceptionsOnlyLimiter`) (thanks to [@y9san9](https://github.com/y9san9) for help)
|
||||||
|
|
||||||
|
## 4.2.2
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `MicroUtils`: `0.16.0` -> `0.16.2`
|
||||||
|
* `Core`:
|
||||||
|
* Fix of [#694](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/694): add opportunity to create `ChatId` and `ChatIdWithThreadId` from `IdChatIdentifier`
|
||||||
|
|
||||||
|
## 4.2.1
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `MicroUtils`: `0.15.0` -> `0.16.0`
|
||||||
|
* `Ktor`: `2.1.3` -> `2.2.1`
|
||||||
|
* `Utils`:
|
||||||
|
* Improve support of `makeLinkToMessage` extensions
|
||||||
|
|
||||||
## 4.2.0
|
## 4.2.0
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ allprojects {
|
|||||||
mavenLocal()
|
mavenLocal()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
google()
|
google()
|
||||||
|
maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" }
|
||||||
}
|
}
|
||||||
if (it != rootProject.findProject("docs")) {
|
if (it != rootProject.findProject("docs")) {
|
||||||
tasks.whenTaskAdded { task ->
|
tasks.whenTaskAdded { task ->
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ kotlin.incremental=true
|
|||||||
kotlin.incremental.js=true
|
kotlin.incremental.js=true
|
||||||
|
|
||||||
library_group=dev.inmo
|
library_group=dev.inmo
|
||||||
library_version=4.2.0
|
library_version=4.2.4
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ javax-activation = "1.1.1"
|
|||||||
|
|
||||||
korlibs = "3.4.0"
|
korlibs = "3.4.0"
|
||||||
uuid = "0.6.0"
|
uuid = "0.6.0"
|
||||||
ktor = "2.1.3"
|
ktor = "2.2.1"
|
||||||
|
|
||||||
ksp = "1.7.22-1.0.8"
|
ksp = "1.7.22-1.0.8"
|
||||||
kotlin-poet = "1.12.0"
|
kotlin-poet = "1.12.0"
|
||||||
|
|
||||||
microutils = "0.15.0"
|
microutils = "0.16.4"
|
||||||
|
|
||||||
github-release-plugin = "2.4.1"
|
github-release-plugin = "2.4.1"
|
||||||
dokka = "1.7.20"
|
dokka = "1.7.20"
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar.T
|
|||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.channels.BufferOverflow
|
import kotlinx.coroutines.channels.BufferOverflow
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,6 +135,9 @@ class DefaultBehaviourContextWithFSM<T : State>(
|
|||||||
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
|
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
|
||||||
private var actualHandlersList = additionalHandlers + handlers
|
private var actualHandlersList = additionalHandlers + handlers
|
||||||
|
|
||||||
|
protected val statesJobs = mutableMapOf<T, Job>()
|
||||||
|
protected val statesJobsMutex = Mutex()
|
||||||
|
|
||||||
override suspend fun launchStateHandling(state: T, handlers: List<CheckableHandlerHolder<in T, T>>): T? {
|
override suspend fun launchStateHandling(state: T, handlers: List<CheckableHandlerHolder<in T, T>>): T? {
|
||||||
return launchStateHandling(state, handlers, onStateHandlingErrorHandler)
|
return launchStateHandling(state, handlers, onStateHandlingErrorHandler)
|
||||||
}
|
}
|
||||||
@@ -164,21 +169,49 @@ class DefaultBehaviourContextWithFSM<T : State>(
|
|||||||
statesManager.endChain(state)
|
statesManager.endChain(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Job.enableRemoveOnCompletion(state: T) {
|
||||||
|
invokeOnCompletion {
|
||||||
|
launchSafelyWithoutExceptions {
|
||||||
|
statesJobsMutex.withLock {
|
||||||
|
if (this@enableRemoveOnCompletion === statesJobs[state]) {
|
||||||
|
statesJobs.remove(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) {
|
statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) {
|
||||||
launch { statePerformer(it) }
|
statesJobsMutex.withLock {
|
||||||
|
runCatchingSafely { statesJobs.remove(it) ?.cancel() }
|
||||||
|
|
||||||
|
statesJobs[it] = launch { statePerformer(it) }.apply { enableRemoveOnCompletion(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
statesJobsMutex.withLock {
|
||||||
|
runCatchingSafely { statesJobs.remove(it) ?.cancel() }
|
||||||
|
}
|
||||||
|
updatesFlows.remove(it.context)
|
||||||
}
|
}
|
||||||
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) { (old, new) ->
|
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) { (old, new) ->
|
||||||
|
statesJobsMutex.withLock {
|
||||||
|
runCatchingSafely { statesJobs.remove(old) ?.cancel() }
|
||||||
|
runCatchingSafely { statesJobs.remove(new) ?.cancel() }
|
||||||
|
statesJobs[new] = launch { statePerformer(new) }.apply { enableRemoveOnCompletion(new) }
|
||||||
|
}
|
||||||
if (old.context != new.context) {
|
if (old.context != new.context) {
|
||||||
updatesFlows.remove(old.context)
|
updatesFlows.remove(old.context)
|
||||||
}
|
}
|
||||||
launch { statePerformer(new) }
|
|
||||||
}
|
|
||||||
statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) {
|
|
||||||
updatesFlows.remove(it.context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
statesManager.getActiveStates().forEach {
|
statesManager.getActiveStates().forEach {
|
||||||
launch { statePerformer(it) }
|
statesJobsMutex.withLock {
|
||||||
|
runCatchingSafely { statesJobs.remove(it) ?.cancel() }
|
||||||
|
|
||||||
|
statesJobs[it] = launch { statePerformer(it) }.apply { enableRemoveOnCompletion(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class KtorRequestsExecutor(
|
|||||||
client: HttpClient = HttpClient(),
|
client: HttpClient = HttpClient(),
|
||||||
callsFactories: List<KtorCallFactory> = emptyList(),
|
callsFactories: List<KtorCallFactory> = emptyList(),
|
||||||
excludeDefaultFactories: Boolean = false,
|
excludeDefaultFactories: Boolean = false,
|
||||||
private val requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter(),
|
private val requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter,
|
||||||
private val jsonFormatter: Json = nonstrictJsonFormat,
|
private val jsonFormatter: Json = nonstrictJsonFormat,
|
||||||
private val pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder
|
private val pipelineStepsHolder: KtorPipelineStepsHolder = KtorPipelineStepsHolder
|
||||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||||
@@ -51,7 +51,7 @@ class KtorRequestsExecutor(
|
|||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
return runCatchingSafely {
|
return runCatchingSafely {
|
||||||
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
|
pipelineStepsHolder.onBeforeSearchCallFactory(request, callsFactories)
|
||||||
requestsLimiter.limit {
|
requestsLimiter.limit(request) {
|
||||||
var result: T? = null
|
var result: T? = null
|
||||||
lateinit var factoryHandledRequest: KtorCallFactory
|
lateinit var factoryHandledRequest: KtorCallFactory
|
||||||
for (potentialFactory in callsFactories) {
|
for (potentialFactory in callsFactories) {
|
||||||
@@ -111,7 +111,7 @@ class KtorRequestsExecutorBuilder(
|
|||||||
var client: HttpClient = HttpClient()
|
var client: HttpClient = HttpClient()
|
||||||
var callsFactories: List<KtorCallFactory> = emptyList()
|
var callsFactories: List<KtorCallFactory> = emptyList()
|
||||||
var excludeDefaultFactories: Boolean = false
|
var excludeDefaultFactories: Boolean = false
|
||||||
var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter()
|
var requestsLimiter: RequestLimiter = ExceptionsOnlyLimiter
|
||||||
var jsonFormatter: Json = nonstrictJsonFormat
|
var jsonFormatter: Json = nonstrictJsonFormat
|
||||||
|
|
||||||
fun build() = KtorRequestsExecutor(telegramAPIUrlsKeeper, client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter)
|
fun build() = KtorRequestsExecutor(telegramAPIUrlsKeeper, client, callsFactories, excludeDefaultFactories, requestsLimiter, jsonFormatter)
|
||||||
|
|||||||
@@ -1,66 +1,18 @@
|
|||||||
package dev.inmo.tgbotapi.bot.settings.limiters
|
package dev.inmo.tgbotapi.bot.settings.limiters
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.safely
|
|
||||||
import dev.inmo.tgbotapi.bot.exceptions.TooMuchRequestsException
|
import dev.inmo.tgbotapi.bot.exceptions.TooMuchRequestsException
|
||||||
import dev.inmo.tgbotapi.types.MilliSeconds
|
|
||||||
import dev.inmo.tgbotapi.types.RetryAfterError
|
|
||||||
import io.ktor.client.plugins.ClientRequestException
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This limiter will limit requests only after getting a [RetryAfterError] or [ClientRequestException] with
|
* Simple limiter which will lock any request when [TooMuchRequestsExceptions] is thrown and rerun request after lock time
|
||||||
* [HttpStatusCode.TooManyRequests] status code. Important thing is that in case if some of block has been blocked, all
|
|
||||||
* the others will wait until it will be possible to be called
|
|
||||||
*
|
|
||||||
* @param defaultTooManyRequestsDelay This parameter will be used in case of getting [ClientRequestException] with
|
|
||||||
* [HttpStatusCode.TooManyRequests] as a parameter for delay like it would be [TooMuchRequestsException]. The reason of
|
|
||||||
* it is that in [ClientRequestException] there is no information about required delay between requests
|
|
||||||
*/
|
*/
|
||||||
class ExceptionsOnlyLimiter(
|
object ExceptionsOnlyLimiter : RequestLimiter {
|
||||||
private val defaultTooManyRequestsDelay: MilliSeconds = 1000L
|
|
||||||
) : RequestLimiter {
|
|
||||||
private val lockState = MutableStateFlow(false)
|
|
||||||
private suspend fun lock(timeMillis: MilliSeconds) {
|
|
||||||
try {
|
|
||||||
safely {
|
|
||||||
lockState.emit(true)
|
|
||||||
delay(timeMillis)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
lockState.emit(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun <T> limit(block: suspend () -> T): T {
|
override suspend fun <T> limit(block: suspend () -> T): T {
|
||||||
while (true) {
|
return try {
|
||||||
lockState.first { !it }
|
|
||||||
var throwable: Throwable? = null
|
|
||||||
val result = safely({
|
|
||||||
throwable = when (it) {
|
|
||||||
is TooMuchRequestsException -> {
|
|
||||||
lock(it.retryAfter.leftToRetry)
|
|
||||||
it
|
|
||||||
}
|
|
||||||
is ClientRequestException -> {
|
|
||||||
if (it.response.status == HttpStatusCode.TooManyRequests) {
|
|
||||||
lock(defaultTooManyRequestsDelay)
|
|
||||||
} else {
|
|
||||||
throw it
|
|
||||||
}
|
|
||||||
it
|
|
||||||
}
|
|
||||||
else -> throw it
|
|
||||||
}
|
|
||||||
null
|
|
||||||
}) {
|
|
||||||
block()
|
block()
|
||||||
}
|
} catch (e: TooMuchRequestsException) {
|
||||||
if (throwable == null) {
|
delay(e.retryAfter.leftToRetry)
|
||||||
return result!!
|
limit(block)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package dev.inmo.tgbotapi.bot.settings.limiters
|
||||||
|
|
||||||
|
object NoLimitsLimiter : RequestLimiter {
|
||||||
|
override suspend fun <T> limit(block: suspend () -> T): T = block()
|
||||||
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
package dev.inmo.tgbotapi.bot.settings.limiters
|
package dev.inmo.tgbotapi.bot.settings.limiters
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
|
|
||||||
interface RequestLimiter {
|
interface RequestLimiter {
|
||||||
/**
|
/**
|
||||||
* Use limit for working of block (like delay between or after, for example)
|
* Use limit for working of block (like delay between or after, for example)
|
||||||
*/
|
*/
|
||||||
suspend fun <T> limit(block: suspend () -> T): T
|
suspend fun <T> limit(block: suspend () -> T): T
|
||||||
|
|
||||||
|
suspend fun <T : Any> limit(request: Request<T>, block: suspend () -> T) = limit(block)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ value class ChatIdWithThreadId(val chatIdWithThreadId: Pair<Identifier, MessageT
|
|||||||
val ChatIdentifier.threadId: MessageThreadId?
|
val ChatIdentifier.threadId: MessageThreadId?
|
||||||
get() = (this as? IdChatIdentifier) ?.threadId
|
get() = (this as? IdChatIdentifier) ?.threadId
|
||||||
|
|
||||||
|
fun IdChatIdentifier.toChatId() = when (this) {
|
||||||
|
is ChatId -> this
|
||||||
|
is ChatIdWithThreadId -> ChatId(chatId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun IdChatIdentifier.toChatWithThreadId(threadId: MessageThreadId) = IdChatIdentifier(chatId, threadId)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://core.telegram.org/bots/api#formatting-options
|
* https://core.telegram.org/bots/api#formatting-options
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -7,39 +7,61 @@ import dev.inmo.tgbotapi.types.message.textsources.link
|
|||||||
import io.ktor.http.encodeURLQueryComponent
|
import io.ktor.http.encodeURLQueryComponent
|
||||||
|
|
||||||
|
|
||||||
fun makeUsernameLink(username: String) = "$internalLinkBeginning/$username"
|
fun makeUsernameLink(username: String, threadId: MessageThreadId? = null) = "$internalLinkBeginning/$username${threadId ?.let { "/$it" } ?: ""}"
|
||||||
|
fun makeChatLink(identifier: Identifier, threadId: MessageThreadId? = null) = identifier.toString().replace(
|
||||||
|
linkIdRedundantPartRegex,
|
||||||
|
""
|
||||||
|
).let { bareId ->
|
||||||
|
"$internalLinkBeginning/c/$bareId${threadId ?.let { "/$it" } ?: ""}"
|
||||||
|
}
|
||||||
fun makeUsernameDeepLinkPrefix(username: String) = "${makeUsernameLink(username)}?start="
|
fun makeUsernameDeepLinkPrefix(username: String) = "${makeUsernameLink(username)}?start="
|
||||||
fun makeUsernameStartattachPrefix(username: String) = "$internalLinkBeginning/$username?startattach"
|
fun makeUsernameStartattachPrefix(username: String) = "$internalLinkBeginning/$username?startattach"
|
||||||
fun makeUsernameStartattachLink(username: String, data: String? = null) = "${makeUsernameStartattachPrefix(username)}${data?.let { "=$it" } ?: ""}"
|
fun makeUsernameStartattachLink(username: String, data: String? = null) = "${makeUsernameStartattachPrefix(username)}${data?.let { "=$it" } ?: ""}"
|
||||||
inline val Username.link
|
inline val Username.link
|
||||||
get() = makeUsernameLink(usernameWithoutAt)
|
get() = makeUsernameLink(usernameWithoutAt)
|
||||||
|
val IdChatIdentifier.link: String
|
||||||
|
get() = makeChatLink(chatId, threadId)
|
||||||
|
fun ChatId.link(threadId: MessageThreadId?) = makeChatLink(chatId, threadId)
|
||||||
|
inline fun Username.link(threadId: MessageThreadId?) = makeUsernameLink(usernameWithoutAt, threadId)
|
||||||
inline val Username.deepLinkPrefix
|
inline val Username.deepLinkPrefix
|
||||||
get() = makeUsernameDeepLinkPrefix(usernameWithoutAt)
|
get() = makeUsernameDeepLinkPrefix(usernameWithoutAt)
|
||||||
inline val Username.startattachPrefix
|
inline val Username.startattachPrefix
|
||||||
get() = makeUsernameStartattachPrefix(usernameWithoutAt)
|
get() = makeUsernameStartattachPrefix(usernameWithoutAt)
|
||||||
inline fun makeLink(username: Username) = username.link
|
inline fun makeLink(username: Username, threadId: MessageThreadId? = null) = username.link(threadId)
|
||||||
inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent()
|
inline fun makeTelegramDeepLink(username: String, startParameter: String) = "${makeUsernameDeepLinkPrefix(username)}$startParameter".encodeURLQueryComponent()
|
||||||
inline fun makeTelegramStartattach(username: String, data: String? = null) = makeUsernameStartattachLink(username, data)
|
inline fun makeTelegramStartattach(username: String, data: String? = null) = makeUsernameStartattachLink(username, data)
|
||||||
inline fun makeDeepLink(username: Username, startParameter: String) = makeTelegramDeepLink(username.usernameWithoutAt, startParameter)
|
inline fun makeDeepLink(username: Username, startParameter: String) = makeTelegramDeepLink(username.usernameWithoutAt, startParameter)
|
||||||
inline fun makeTelegramDeepLink(username: Username, startParameter: String) = makeDeepLink(username, startParameter)
|
inline fun makeTelegramDeepLink(username: Username, startParameter: String) = makeDeepLink(username, startParameter)
|
||||||
inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.usernameWithoutAt, data)
|
inline fun makeTelegramStartattach(username: Username, data: String? = null) = makeTelegramStartattach(username.usernameWithoutAt, data)
|
||||||
|
|
||||||
fun makeLinkToMessage(
|
|
||||||
username: String,
|
|
||||||
messageId: MessageId
|
|
||||||
): String = "$internalLinkBeginning/$username/$messageId"
|
|
||||||
fun makeLinkToMessage(
|
|
||||||
username: Username,
|
|
||||||
messageId: MessageId
|
|
||||||
): String = makeLinkToMessage(username.username, messageId)
|
|
||||||
fun makeLinkToMessage(
|
|
||||||
chat: UsernameChat,
|
|
||||||
messageId: MessageId
|
|
||||||
): String? = chat.username ?.let { makeLinkToMessage(it, messageId) }
|
|
||||||
|
|
||||||
private val linkIdRedundantPartRegex = Regex("^-100")
|
private val linkIdRedundantPartRegex = Regex("^-100")
|
||||||
private val usernameBeginSymbolRegex = Regex("^@")
|
private val usernameBeginSymbolRegex = Regex("^@")
|
||||||
|
|
||||||
|
fun makeLinkToMessage(
|
||||||
|
username: String,
|
||||||
|
messageId: MessageId,
|
||||||
|
threadId: MessageThreadId? = null
|
||||||
|
): String = "${makeUsernameLink(username, threadId)}/$messageId"
|
||||||
|
fun makeLinkToMessage(
|
||||||
|
username: Username,
|
||||||
|
messageId: MessageId,
|
||||||
|
threadId: MessageThreadId? = null
|
||||||
|
): String = makeLinkToMessage(username.username, messageId, threadId)
|
||||||
|
fun makeLinkToMessage(
|
||||||
|
chatId: Identifier,
|
||||||
|
messageId: MessageId,
|
||||||
|
threadId: MessageThreadId? = null
|
||||||
|
): String = chatId.toString().replace(
|
||||||
|
linkIdRedundantPartRegex,
|
||||||
|
""
|
||||||
|
).let { bareId ->
|
||||||
|
"$internalLinkBeginning/c/$bareId/${threadId ?.let { "$it/" } ?: ""}$messageId"
|
||||||
|
}
|
||||||
|
fun makeLinkToMessage(
|
||||||
|
chatId: IdChatIdentifier,
|
||||||
|
messageId: MessageId,
|
||||||
|
): String = makeLinkToMessage(chatId.chatId, messageId, chatId.threadId)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Link which can be used as by any user to get access to [Message]. Returns null in case when there are no
|
* Link which can be used as by any user to get access to [Message]. Returns null in case when there are no
|
||||||
* known way to build link (for [PrivateChat]s, for example)
|
* known way to build link (for [PrivateChat]s, for example)
|
||||||
@@ -49,16 +71,10 @@ fun makeLinkToMessage(
|
|||||||
messageId: MessageId
|
messageId: MessageId
|
||||||
): String? {
|
): String? {
|
||||||
return when {
|
return when {
|
||||||
chat is UsernameChat && chat.username != null -> {
|
chat is UsernameChat && chat.username != null -> chat.username ?.let {
|
||||||
"$internalLinkBeginning/${chat.username ?.username ?.replace(
|
makeLinkToMessage(it, messageId, chat.id.threadId)
|
||||||
usernameBeginSymbolRegex, "")}/$messageId"
|
} ?: makeLinkToMessage(chat.id, messageId)
|
||||||
}
|
chat !is PrivateChat -> makeLinkToMessage(chat.id, messageId)
|
||||||
chat !is PrivateChat -> chat.id.chatId.toString().replace(
|
|
||||||
linkIdRedundantPartRegex,
|
|
||||||
""
|
|
||||||
).let { bareId ->
|
|
||||||
"$internalLinkBeginning/c/$bareId/$messageId"
|
|
||||||
}
|
|
||||||
else -> return null
|
else -> return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +95,7 @@ val Message.link: String?
|
|||||||
val Chat.link: String?
|
val Chat.link: String?
|
||||||
get() {
|
get() {
|
||||||
if (this is UsernameChat) {
|
if (this is UsernameChat) {
|
||||||
username ?.link
|
username ?.link ?: id.link
|
||||||
}
|
}
|
||||||
if (this is ExtendedPublicChat) {
|
if (this is ExtendedPublicChat) {
|
||||||
inviteLink ?.let { return it }
|
inviteLink ?.let { return it }
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.launchSafely
|
||||||
|
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
|
||||||
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
|
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
|
||||||
@@ -29,15 +31,18 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
|||||||
)
|
)
|
||||||
|
|
||||||
launch {
|
launch {
|
||||||
launch {
|
launchSafelyWithoutExceptions {
|
||||||
for (update in updatesChannel) {
|
for (update in updatesChannel) {
|
||||||
when (val data = update.data) {
|
val data = update.data
|
||||||
is PossiblyMediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
|
when {
|
||||||
|
data is PossiblyMediaGroupMessage<*> && data.mediaGroupId != null -> {
|
||||||
|
mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
|
||||||
|
}
|
||||||
else -> output(update)
|
else -> output(update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launchSafelyWithoutExceptions {
|
||||||
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
|
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
|
||||||
mediaGroup.convertWithMediaGroupUpdates().forEach {
|
mediaGroup.convertWithMediaGroupUpdates().forEach {
|
||||||
output(it)
|
output(it)
|
||||||
|
|||||||
@@ -41,10 +41,9 @@ fun Route.includeWebhookHandlingInRoute(
|
|||||||
post {
|
post {
|
||||||
try {
|
try {
|
||||||
runCatchingSafely {
|
runCatchingSafely {
|
||||||
val asJson = nonstrictJsonFormat.parseToJsonElement(call.receiveText())
|
val update = nonstrictJsonFormat.decodeFromString(
|
||||||
val update = nonstrictJsonFormat.decodeFromJsonElement(
|
|
||||||
UpdateDeserializationStrategy,
|
UpdateDeserializationStrategy,
|
||||||
asJson
|
call.receiveText()
|
||||||
)
|
)
|
||||||
transformer(update)
|
transformer(update)
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
|
|||||||
Reference in New Issue
Block a user