Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add customizable executor for TelegramChannel #251

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.github.kotlintelegrambot.dispatcher.video
import com.github.kotlintelegrambot.dispatcher.videoNote
import com.github.kotlintelegrambot.dispatcher.voice
import com.github.kotlintelegrambot.entities.Update
import com.github.kotlintelegrambot.logging.LogLevel
import com.github.kotlintelegrambot.network.serialization.GsonFactory
import com.github.kotlintelegrambot.updater.Updater
import com.justai.jaicf.api.BotApi
Expand All @@ -31,13 +32,17 @@ import com.justai.jaicf.channel.jaicp.JaicpLiveChatProvider
import com.justai.jaicf.context.RequestContext
import com.justai.jaicf.helpers.http.withTrailingSlash
import com.justai.jaicf.helpers.kotlin.PropertyWithBackingField
import java.util.*
import java.util.UUID
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class TelegramChannel(
override val botApi: BotApi,
private val telegramBotToken: String,
private val telegramApiUrl: String = "https://api.telegram.org/"
private val telegramApiUrl: String = "https://api.telegram.org/",
private val telegramLogLevel: LogLevel = LogLevel.None,
private val requestExecutor: Executor = Executors.newFixedThreadPool(10)
) : JaicpCompatibleAsyncBotChannel, InvocableBotChannel {

private val gson = GsonFactory.createForApiClient()
Expand All @@ -48,12 +53,19 @@ class TelegramChannel(
apiUrl = telegramApiUrl.withTrailingSlash()
token = telegramBotToken
botUpdater = updater
logLevel = telegramLogLevel

botUpdater.startCheckingUpdates()

dispatch {
fun process(request: TelegramBotRequest) {
botApi.process(request, TelegramReactions(bot, request, liveChatProvider), RequestContext.fromHttp(request.update.httpBotRequest))
requestExecutor.execute {
botApi.process(
request,
TelegramReactions(bot, request, liveChatProvider),
RequestContext.fromHttp(request.update.httpBotRequest)
)
}
}

text {
Expand Down Expand Up @@ -150,13 +162,29 @@ class TelegramChannel(
override fun create(
botApi: BotApi,
apiUrl: String,
liveChatProvider: JaicpLiveChatProvider
liveChatProvider: JaicpLiveChatProvider,
) = TelegramChannel(botApi, telegramApiUrl = apiUrl, telegramBotToken = "").apply {
this.liveChatProvider = liveChatProvider
}

private const val REQUEST_TEMPLATE_PATH = "/TelegramRequestTemplate.json"
}

class Jaicp(
private val executor: Executor,
private val logLevel: LogLevel
) : JaicpCompatibleAsyncChannelFactory {

override fun create(
botApi: BotApi,
apiUrl: String,
liveChatProvider: JaicpLiveChatProvider
): JaicpCompatibleAsyncBotChannel = TelegramChannel(botApi, "", apiUrl, logLevel, executor).apply {
this.liveChatProvider = liveChatProvider
}

override val channelType: String = "telegram"
}
}

internal var Update.httpBotRequest: HttpBotRequest by PropertyWithBackingField {
Expand Down