mirror of
https://github.com/InsanusMokrassar/ConfigurableInlineTelegramBot.git
synced 2026-04-01 13:42:22 +00:00
init
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package dev.inmo.configurable_inline_telegram_bot.config
|
||||
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class BotConfig(
|
||||
val botToken: String,
|
||||
val apiUrl: String = telegramBotAPIDefaultUrl,
|
||||
val clientConfig: HttpClientConfig? = null,
|
||||
val webhookConfig: WebhookConfig? = null
|
||||
) {
|
||||
fun createBot(): RequestsExecutor = telegramBot(
|
||||
botToken
|
||||
) {
|
||||
client = HttpClient(OkHttp.create(clientConfig ?.builder ?: {}))
|
||||
telegramAPIUrlsKeeper = TelegramAPIUrlsKeeper(
|
||||
botToken,
|
||||
apiUrl
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package dev.inmo.configurable_inline_telegram_bot.config
|
||||
|
||||
import io.ktor.client.engine.okhttp.OkHttpConfig
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import okhttp3.*
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Proxy
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@Serializable
|
||||
data class HttpClientConfig(
|
||||
val proxy: ProxySettings? = null,
|
||||
val connectTimeout: Long = 0,
|
||||
val writeTimeout: Long = 0,
|
||||
val readTimeout: Long = 0
|
||||
) {
|
||||
@Transient
|
||||
val builder: OkHttpConfig.() -> Unit = {
|
||||
config {
|
||||
this@HttpClientConfig.proxy ?.let {
|
||||
proxy(
|
||||
Proxy(
|
||||
Proxy.Type.SOCKS,
|
||||
InetSocketAddress(
|
||||
it.host,
|
||||
it.port
|
||||
)
|
||||
)
|
||||
)
|
||||
it.password ?.let { password ->
|
||||
proxyAuthenticator (
|
||||
object : Authenticator {
|
||||
override fun authenticate(route: Route?, response: Response): Request? {
|
||||
return response.request.newBuilder().apply {
|
||||
addHeader(
|
||||
"Proxy-Authorization",
|
||||
Credentials.basic(it.username ?: "", password)
|
||||
)
|
||||
}.build()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
|
||||
writeTimeout(writeTimeout, TimeUnit.MILLISECONDS)
|
||||
readTimeout(readTimeout, TimeUnit.MILLISECONDS)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dev.inmo.configurable_inline_telegram_bot.config
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ProxySettings(
|
||||
val host: String = "localhost",
|
||||
val port: Int = 1080,
|
||||
val username: String? = null,
|
||||
val password: String? = null
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
package dev.inmo.configurable_inline_telegram_bot.config
|
||||
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Restrictions(
|
||||
val allowedUsers: List<ChatIdentifier> = emptyList()
|
||||
) {
|
||||
fun check(query: InlineQuery): Boolean {
|
||||
return query.from.id in allowedUsers || query.from.username ?.let { it in allowedUsers } ?: false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package dev.inmo.configurable_inline_telegram_bot.config
|
||||
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.setWebhookInfoAndStartListenWebhooks
|
||||
import dev.inmo.tgbotapi.requests.abstracts.toInputFile
|
||||
import dev.inmo.tgbotapi.requests.webhook.SetWebhook
|
||||
import dev.inmo.tgbotapi.updateshandlers.UpdatesFilter
|
||||
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||
import io.ktor.server.engine.ApplicationEngine
|
||||
import io.ktor.server.tomcat.Tomcat
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.io.File
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@Serializable
|
||||
data class WebhookConfig(
|
||||
val url: String,
|
||||
val listenHost: String = "0.0.0.0",
|
||||
val listenRoute: String = "/",
|
||||
val port: Int = System.getenv("PORT").toInt(),
|
||||
val certificatePath: String? = null,
|
||||
val maxConnections: Int = 40,
|
||||
val privateKeyConfig: WebhookPrivateKeyConfig? = null
|
||||
) {
|
||||
init {
|
||||
println(this)
|
||||
}
|
||||
|
||||
suspend fun setWebhookAndCreateServer(
|
||||
requestsExecutor: RequestsExecutor,
|
||||
filter: UpdatesFilter,
|
||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(maxConnections / 2).asCoroutineDispatcher())
|
||||
): ApplicationEngine = (certificatePath ?.let {
|
||||
requestsExecutor.setWebhookInfoAndStartListenWebhooks(
|
||||
port,
|
||||
Tomcat,
|
||||
SetWebhook(url, File(it).toInputFile(), maxAllowedConnections = maxConnections, allowedUpdates = filter.allowedUpdates),
|
||||
{ throwable: Throwable ->
|
||||
throwable.printStackTrace()
|
||||
},
|
||||
listenHost,
|
||||
listenRoute,
|
||||
privateKeyConfig = privateKeyConfig,
|
||||
scope = scope,
|
||||
block = filter.asUpdateReceiver
|
||||
)
|
||||
} ?: requestsExecutor.setWebhookInfoAndStartListenWebhooks(
|
||||
port,
|
||||
Tomcat,
|
||||
SetWebhook(
|
||||
url,
|
||||
maxAllowedConnections = maxConnections,
|
||||
allowedUpdates = filter.allowedUpdates
|
||||
),
|
||||
{ throwable: Throwable ->
|
||||
throwable.printStackTrace()
|
||||
},
|
||||
listenHost,
|
||||
listenRoute,
|
||||
privateKeyConfig = privateKeyConfig,
|
||||
scope = scope,
|
||||
block = filter.asUpdateReceiver
|
||||
)
|
||||
).also {
|
||||
it.environment.connectors.forEach {
|
||||
println(it)
|
||||
}
|
||||
it.start(false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user