2020-05-13 17:22:35 +00:00
|
|
|
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving
|
2020-05-13 15:21:07 +00:00
|
|
|
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
2020-05-13 17:22:35 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat
|
2020-08-10 04:46:18 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.flowsUpdatesFilter
|
2020-05-15 12:35:16 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl
|
2020-05-13 15:21:07 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
|
2020-05-13 17:22:35 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy
|
2020-08-10 04:46:18 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
|
2020-05-13 15:21:07 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
|
2020-05-14 18:28:16 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.ExceptionHandler
|
2020-05-13 15:21:07 +00:00
|
|
|
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
|
|
|
|
import io.ktor.application.call
|
|
|
|
import io.ktor.request.receiveText
|
|
|
|
import io.ktor.response.respond
|
|
|
|
import io.ktor.routing.*
|
|
|
|
import io.ktor.server.engine.*
|
2020-07-02 10:15:06 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.asCoroutineDispatcher
|
2020-05-13 15:21:07 +00:00
|
|
|
import java.util.concurrent.Executors
|
|
|
|
|
2020-05-13 17:22:35 +00:00
|
|
|
|
2020-05-13 15:21:07 +00:00
|
|
|
/**
|
2020-05-14 07:11:46 +00:00
|
|
|
* Allows to include webhook in custom route everywhere in your server
|
|
|
|
*
|
2020-05-13 15:21:07 +00:00
|
|
|
* @param [scope] Will be used for mapping of media groups
|
|
|
|
* @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates
|
|
|
|
* @param [block] Some receiver block like [com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter]
|
|
|
|
*
|
|
|
|
* @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
|
|
|
* @see UpdatesFilter
|
|
|
|
* @see UpdatesFilter.asUpdateReceiver
|
|
|
|
*/
|
2020-05-14 18:28:16 +00:00
|
|
|
fun Route.includeWebhookHandlingInRoute(
|
2020-05-13 15:21:07 +00:00
|
|
|
scope: CoroutineScope,
|
2020-05-14 18:28:16 +00:00
|
|
|
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
2020-05-13 15:21:07 +00:00
|
|
|
block: UpdateReceiver<Update>
|
|
|
|
) {
|
|
|
|
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block)
|
|
|
|
post {
|
|
|
|
handleSafely(
|
|
|
|
exceptionsHandler ?: {}
|
|
|
|
) {
|
|
|
|
val asJson =
|
|
|
|
nonstrictJsonFormat.parseJson(call.receiveText())
|
|
|
|
val update = nonstrictJsonFormat.fromJson(
|
|
|
|
UpdateDeserializationStrategy,
|
|
|
|
asJson
|
|
|
|
)
|
|
|
|
transformer(update)
|
|
|
|
}
|
|
|
|
call.respond("Ok")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 04:46:18 +00:00
|
|
|
fun Route.includeWebhookHandlingInRouteWithFlows(
|
|
|
|
scope: CoroutineScope,
|
|
|
|
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
|
|
|
block: FlowsUpdatesFilter.() -> Unit
|
|
|
|
) = includeWebhookHandlingInRoute(
|
|
|
|
scope,
|
|
|
|
exceptionsHandler,
|
|
|
|
flowsUpdatesFilter(block = block).asUpdateReceiver
|
|
|
|
)
|
|
|
|
|
2020-05-13 15:21:07 +00:00
|
|
|
/**
|
|
|
|
* Setting up ktor server, set webhook info via [SetWebhook] request.
|
|
|
|
*
|
2020-05-14 18:28:16 +00:00
|
|
|
* @param listenPort port which will be listen by bot
|
2020-05-15 16:14:19 +00:00
|
|
|
* @param listenRoute address to listen by bot. If null - will be set up in root of host
|
2020-05-13 15:21:07 +00:00
|
|
|
* @param scope Scope which will be used for
|
2020-05-14 07:11:46 +00:00
|
|
|
* @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise
|
2020-05-13 15:21:07 +00:00
|
|
|
*
|
|
|
|
* @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
|
|
|
* @see UpdatesFilter
|
|
|
|
* @see UpdatesFilter.asUpdateReceiver
|
|
|
|
*/
|
2020-05-14 18:28:16 +00:00
|
|
|
fun startListenWebhooks(
|
|
|
|
listenPort: Int,
|
2020-05-13 15:21:07 +00:00
|
|
|
engineFactory: ApplicationEngineFactory<*, *>,
|
2020-05-14 18:28:16 +00:00
|
|
|
exceptionsHandler: ExceptionHandler<Unit>,
|
2020-05-13 15:21:07 +00:00
|
|
|
listenHost: String = "0.0.0.0",
|
2020-05-15 16:14:19 +00:00
|
|
|
listenRoute: String? = null,
|
2020-05-13 15:21:07 +00:00
|
|
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
|
|
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
|
|
|
block: UpdateReceiver<Update>
|
|
|
|
): ApplicationEngine {
|
|
|
|
val env = applicationEngineEnvironment {
|
|
|
|
|
|
|
|
module {
|
|
|
|
routing {
|
2020-05-15 16:14:19 +00:00
|
|
|
listenRoute ?.also {
|
2020-05-16 14:43:09 +00:00
|
|
|
createRouteFromPath(it).includeWebhookHandlingInRoute(scope, exceptionsHandler, block)
|
2020-05-15 16:14:19 +00:00
|
|
|
} ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, block)
|
2020-05-13 15:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
privateKeyConfig ?.let {
|
|
|
|
sslConnector(
|
|
|
|
privateKeyConfig.keyStore,
|
|
|
|
privateKeyConfig.aliasName,
|
|
|
|
privateKeyConfig::keyStorePassword,
|
|
|
|
privateKeyConfig::aliasPassword
|
|
|
|
) {
|
|
|
|
host = listenHost
|
2020-05-16 14:43:09 +00:00
|
|
|
port = listenPort
|
2020-05-13 15:21:07 +00:00
|
|
|
}
|
|
|
|
} ?: connector {
|
|
|
|
host = listenHost
|
2020-05-16 14:43:09 +00:00
|
|
|
port = listenPort
|
2020-05-13 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:09:48 +00:00
|
|
|
return embeddedServer(engineFactory, env).also {
|
|
|
|
it.start(false)
|
|
|
|
}
|
2020-05-13 15:21:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 14:57:57 +00:00
|
|
|
private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks(
|
2020-05-15 12:35:16 +00:00
|
|
|
listenPort: Int,
|
|
|
|
engineFactory: ApplicationEngineFactory<*, *>,
|
|
|
|
setWebhookRequest: Request<Boolean>,
|
|
|
|
exceptionsHandler: ExceptionHandler<Unit> = {},
|
|
|
|
listenHost: String = "0.0.0.0",
|
2020-05-16 14:43:09 +00:00
|
|
|
listenRoute: String? = null,
|
2020-05-15 12:35:16 +00:00
|
|
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
|
|
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
|
|
|
block: UpdateReceiver<Update>
|
2020-05-17 09:09:48 +00:00
|
|
|
): ApplicationEngine {
|
2020-05-15 12:35:16 +00:00
|
|
|
return try {
|
|
|
|
execute(setWebhookRequest)
|
2020-05-17 09:09:48 +00:00
|
|
|
startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block)
|
2020-05-15 12:35:16 +00:00
|
|
|
} catch (e: Exception) {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:21:07 +00:00
|
|
|
/**
|
|
|
|
* Setting up ktor server, set webhook info via [SetWebhook] request.
|
|
|
|
*
|
2020-05-14 18:28:16 +00:00
|
|
|
* @param listenPort port which will be listen by bot
|
2020-05-13 15:21:07 +00:00
|
|
|
* @param listenRoute address to listen by bot
|
|
|
|
* @param scope Scope which will be used for
|
|
|
|
*
|
|
|
|
* @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
|
|
|
* @see UpdatesFilter
|
|
|
|
* @see UpdatesFilter.asUpdateReceiver
|
|
|
|
*/
|
2020-05-14 18:28:16 +00:00
|
|
|
@Suppress("unused")
|
|
|
|
suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
|
|
|
|
listenPort: Int,
|
2020-05-13 15:21:07 +00:00
|
|
|
engineFactory: ApplicationEngineFactory<*, *>,
|
2020-05-14 18:28:16 +00:00
|
|
|
setWebhookRequest: SetWebhook,
|
|
|
|
exceptionsHandler: ExceptionHandler<Unit> = {},
|
2020-05-13 15:21:07 +00:00
|
|
|
listenHost: String = "0.0.0.0",
|
|
|
|
listenRoute: String = "/",
|
|
|
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
|
|
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
|
|
|
block: UpdateReceiver<Update>
|
2020-05-17 09:09:48 +00:00
|
|
|
): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks(
|
2020-05-15 12:35:16 +00:00
|
|
|
listenPort,
|
|
|
|
engineFactory,
|
|
|
|
setWebhookRequest as Request<Boolean>,
|
|
|
|
exceptionsHandler,
|
|
|
|
listenHost,
|
|
|
|
listenRoute,
|
|
|
|
privateKeyConfig,
|
|
|
|
scope,
|
|
|
|
block
|
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setting up ktor server, set webhook info via [SetWebhook] request.
|
|
|
|
*
|
|
|
|
* @param listenPort port which will be listen by bot
|
|
|
|
* @param listenRoute address to listen by bot
|
|
|
|
* @param scope Scope which will be used for
|
|
|
|
*
|
|
|
|
* @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
|
|
|
* @see UpdatesFilter
|
|
|
|
* @see UpdatesFilter.asUpdateReceiver
|
|
|
|
*/
|
|
|
|
@Suppress("unused")
|
|
|
|
suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
|
|
|
|
listenPort: Int,
|
|
|
|
engineFactory: ApplicationEngineFactory<*, *>,
|
|
|
|
setWebhookRequest: MultipartRequestImpl<SetWebhook, Map<String, MultipartFile>, Boolean>,
|
|
|
|
exceptionsHandler: ExceptionHandler<Unit> = {},
|
|
|
|
listenHost: String = "0.0.0.0",
|
2020-05-16 14:43:09 +00:00
|
|
|
listenRoute: String? = null,
|
2020-05-15 12:35:16 +00:00
|
|
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
|
|
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
|
|
|
block: UpdateReceiver<Update>
|
2020-05-17 09:09:48 +00:00
|
|
|
): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks(
|
2020-05-15 12:35:16 +00:00
|
|
|
listenPort,
|
|
|
|
engineFactory,
|
|
|
|
setWebhookRequest as Request<Boolean>,
|
|
|
|
exceptionsHandler,
|
|
|
|
listenHost,
|
|
|
|
listenRoute,
|
|
|
|
privateKeyConfig,
|
|
|
|
scope,
|
|
|
|
block
|
|
|
|
)
|