mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2026-03-05 02:12:40 +00:00
migration
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.Response
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
onFail: (suspend (Response) -> Unit)? = null,
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
onSuccess: (suspend (T) -> Unit)? = null
|
||||
): Job {
|
||||
return scope.launch {
|
||||
try {
|
||||
val result = execute(request)
|
||||
onSuccess ?.invoke(result)
|
||||
} catch (e: RequestException) {
|
||||
onFail ?.invoke(e.response)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
scope: CoroutineScope = GlobalScope
|
||||
): Deferred<T> {
|
||||
return scope.async { execute(request) }
|
||||
}
|
||||
|
||||
suspend fun <T: Any> RequestsExecutor.executeUnsafe(
|
||||
request: Request<T>,
|
||||
retries: Int = 0,
|
||||
retriesDelay: Long = 1000L
|
||||
): T? {
|
||||
var leftRetries = retries
|
||||
do {
|
||||
try {
|
||||
return execute(request)
|
||||
} catch (e: RequestException) {
|
||||
leftRetries--
|
||||
delay(retriesDelay)
|
||||
}
|
||||
} while(leftRetries >= 0)
|
||||
return null
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
|
||||
private sealed class DebounceAction<T> {
|
||||
abstract val value: T
|
||||
}
|
||||
|
||||
private data class AddValue<T>(override val value: T) : DebounceAction<T>()
|
||||
private data class RemoveJob<T>(override val value: T, val job: Job) : DebounceAction<T>()
|
||||
|
||||
fun <T> ReceiveChannel<T>.debounceByValue(
|
||||
delayMillis: Long,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
resultBroadcastChannelCapacity: Int = 32
|
||||
): ReceiveChannel<T> {
|
||||
val outChannel = Channel<T>(resultBroadcastChannelCapacity)
|
||||
val values = HashMap<T, Job>()
|
||||
|
||||
val channel = Channel<DebounceAction<T>>(Channel.UNLIMITED)
|
||||
scope.launch {
|
||||
for (action in channel) {
|
||||
when (action) {
|
||||
is AddValue -> {
|
||||
val msg = action.value
|
||||
values[msg] ?.cancel()
|
||||
lateinit var job: Job
|
||||
job = launch {
|
||||
delay(delayMillis)
|
||||
|
||||
outChannel.send(msg)
|
||||
channel.send(RemoveJob(msg, job))
|
||||
}
|
||||
values[msg] = job
|
||||
}
|
||||
is RemoveJob -> if (values[action.value] == action.job) {
|
||||
values.remove(action.value)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
for (msg in this@debounceByValue) {
|
||||
channel.send(AddValue(msg))
|
||||
}
|
||||
}
|
||||
|
||||
return outChannel
|
||||
}
|
||||
|
||||
typealias AccumulatedValues<K, V> = Pair<K, List<V>>
|
||||
|
||||
fun <K, V> ReceiveChannel<Pair<K, V>>.accumulateByKey(
|
||||
delayMillis: Long,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
resultBroadcastChannelCapacity: Int = 32
|
||||
): ReceiveChannel<AccumulatedValues<K, V>> {
|
||||
val outChannel = Channel<AccumulatedValues<K, V>>(resultBroadcastChannelCapacity)
|
||||
val values = HashMap<K, MutableList<V>>()
|
||||
val jobs = HashMap<K, Job>()
|
||||
|
||||
val channel = Channel<DebounceAction<Pair<K, V>>>(Channel.UNLIMITED)
|
||||
scope.launch {
|
||||
for (action in channel) {
|
||||
val (key, value) = action.value
|
||||
when (action) {
|
||||
is AddValue -> {
|
||||
jobs[key] ?.cancel()
|
||||
(values[key] ?: mutableListOf<V>().also { values[key] = it }).add(value)
|
||||
lateinit var job: Job
|
||||
job = launch {
|
||||
delay(delayMillis)
|
||||
|
||||
values[key] ?.let {
|
||||
outChannel.send(key to it)
|
||||
channel.send(RemoveJob(key to value, job))
|
||||
}
|
||||
}
|
||||
jobs[key] = job
|
||||
}
|
||||
is RemoveJob -> if (values[key] == action.job) {
|
||||
values.remove(key)
|
||||
jobs.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
for (msg in this@accumulateByKey) {
|
||||
channel.send(AddValue(msg))
|
||||
}
|
||||
}
|
||||
|
||||
return outChannel
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
|
||||
fun String.toMarkdown(): String {
|
||||
return replace(
|
||||
"*",
|
||||
"\\*"
|
||||
).replace(
|
||||
"_",
|
||||
"\\_"
|
||||
)
|
||||
}
|
||||
|
||||
fun String.toHtml(): String = replace(
|
||||
"<",
|
||||
"<"
|
||||
).replace(
|
||||
">",
|
||||
">"
|
||||
).replace(
|
||||
"&",
|
||||
"&"
|
||||
).replace(
|
||||
"\"",
|
||||
"""
|
||||
)
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.UpdatesPoller
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.KtorUpdatesPoller
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
typealias UpdateReceiver<T> = suspend (T) -> Unit
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
timeoutMillis: Long = 30 * 1000,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
allowedUpdates: List<String>? = null,
|
||||
block: UpdateReceiver<Update>
|
||||
): UpdatesPoller {
|
||||
return KtorUpdatesPoller(
|
||||
this,
|
||||
timeoutMillis.toInt() / 1000,
|
||||
allowedUpdates = allowedUpdates ?: ALL_UPDATES_LIST,
|
||||
updatesReceiver = block
|
||||
).also {
|
||||
it.start(scope)
|
||||
}
|
||||
}
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||
messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
|
||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||
editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
|
||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||
channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||
editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
|
||||
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
|
||||
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
||||
pollCallback: UpdateReceiver<PollUpdate>? = null,
|
||||
timeoutMillis: Long = 30 * 1000,
|
||||
scope: CoroutineScope = GlobalScope
|
||||
): UpdatesPoller {
|
||||
val filter = UpdatesFilter(
|
||||
messageCallback,
|
||||
messageMediaGroupCallback,
|
||||
editedMessageCallback,
|
||||
editedMessageMediaGroupCallback,
|
||||
channelPostCallback,
|
||||
channelPostMediaGroupCallback,
|
||||
editedChannelPostCallback,
|
||||
editedChannelPostMediaGroupCallback,
|
||||
chosenInlineResultCallback,
|
||||
inlineQueryCallback,
|
||||
callbackQueryCallback,
|
||||
shippingQueryCallback,
|
||||
preCheckoutQueryCallback,
|
||||
pollCallback
|
||||
)
|
||||
return startGettingOfUpdates(
|
||||
timeoutMillis,
|
||||
scope,
|
||||
filter.allowedUpdates,
|
||||
filter.asUpdateReceiver
|
||||
)
|
||||
}
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||
mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
|
||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
|
||||
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
||||
pollCallback: UpdateReceiver<PollUpdate>? = null,
|
||||
timeoutMillis: Long = 30 * 1000,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
): UpdatesPoller = startGettingOfUpdates(
|
||||
messageCallback = messageCallback,
|
||||
messageMediaGroupCallback = mediaGroupCallback,
|
||||
editedMessageCallback = editedMessageCallback,
|
||||
editedMessageMediaGroupCallback = mediaGroupCallback,
|
||||
channelPostCallback = channelPostCallback,
|
||||
channelPostMediaGroupCallback = mediaGroupCallback,
|
||||
editedChannelPostCallback = editedChannelPostCallback,
|
||||
editedChannelPostMediaGroupCallback = mediaGroupCallback,
|
||||
chosenInlineResultCallback = chosenInlineResultCallback,
|
||||
inlineQueryCallback = inlineQueryCallback,
|
||||
callbackQueryCallback = callbackQueryCallback,
|
||||
shippingQueryCallback = shippingQueryCallback,
|
||||
preCheckoutQueryCallback = preCheckoutQueryCallback,
|
||||
pollCallback = pollCallback,
|
||||
timeoutMillis = timeoutMillis,
|
||||
scope = scope
|
||||
)
|
||||
Reference in New Issue
Block a user