tgbotapi/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBot.kt

91 lines
3.1 KiB
Kotlin
Raw Normal View History

2021-06-23 15:51:30 +00:00
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.ExceptionHandler
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
import dev.inmo.tgbotapi.bot.ktor.telegramBot
2021-06-23 15:51:30 +00:00
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
2023-02-06 07:28:38 +00:00
import dev.inmo.tgbotapi.types.Seconds
2021-06-23 15:51:30 +00:00
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
/**
* Create bot using [telegramBot] and start listening for updates using [buildBehaviour].
* Use this method in case you wish to make some additional actions with [flowsUpdatesFilter].
*
* **WARNING** This method WILL NOT launch any listening of updates. Use something like
* [startGettingOfUpdatesByLongPolling] or tools for work with webhooks
*
* @return Created bot which has been used to create [BehaviourContext] via [buildBehaviour]
*
* @see [BehaviourContext]
* @see [buildBehaviour]
* @see startGettingOfUpdatesByLongPolling
*/
suspend fun telegramBotWithBehaviour(
token: String,
2021-11-08 11:33:02 +00:00
flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(),
2021-06-23 15:51:30 +00:00
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2022-05-02 07:21:43 +00:00
testServer: Boolean = false,
2021-06-23 15:51:30 +00:00
block: BehaviourContextReceiver<Unit>
): TelegramBot = telegramBot(
token,
apiUrl,
2022-05-02 07:21:43 +00:00
testServer,
2021-06-23 15:51:30 +00:00
builder
).apply {
buildBehaviour(
flowsUpdatesFilter,
2021-10-01 18:52:29 +00:00
scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
2021-06-23 15:51:30 +00:00
block
)
}
/**
* Create bot using [telegramBot] and start listening for updates using [buildBehaviourWithLongPolling].
2021-06-23 15:51:30 +00:00
* Use this method in case you wish to make some additional actions with [flowsUpdatesFilter].
*
* **WARNING** This method WILL launch updates listening inside of calling [buildBehaviourWithLongPolling]
2021-06-23 15:51:30 +00:00
*
* @return Pair of [TelegramBot] and [Job]. This [Job] can be used to stop listening updates in your [block] you passed
* here
*
* @see [BehaviourContext]
* @see buildBehaviourWithLongPolling
2021-06-23 15:51:30 +00:00
* @see startGettingOfUpdatesByLongPolling
*/
suspend fun telegramBotWithBehaviourAndLongPolling(
2021-06-23 15:51:30 +00:00
token: String,
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
2022-05-02 07:21:43 +00:00
testServer: Boolean = false,
2023-02-06 07:28:38 +00:00
timeoutSeconds: Seconds = 30,
autoDisableWebhooks: Boolean = true,
autoSkipTimeoutExceptions: Boolean = true,
2021-06-23 15:51:30 +00:00
block: BehaviourContextReceiver<Unit>
): Pair<TelegramBot, Job> {
return telegramBot(
token,
apiUrl,
2022-05-02 07:21:43 +00:00
testServer,
2021-06-23 15:51:30 +00:00
builder
).let {
it to it.buildBehaviourWithLongPolling(
2021-06-23 15:51:30 +00:00
scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
2023-02-06 07:28:38 +00:00
timeoutSeconds,
autoDisableWebhooks,
autoSkipTimeoutExceptions,
2021-06-23 15:51:30 +00:00
block
)
}
}