webhook update handling enhancement

This commit is contained in:
InsanusMokrassar 2020-04-22 13:16:46 +06:00
parent 17f64f9b48
commit 459942de36
2 changed files with 42 additions and 13 deletions

View File

@ -45,6 +45,7 @@
`SerializationException` or `NotImplemented` error `SerializationException` or `NotImplemented` error
* `CallbackGame` currently is an object * `CallbackGame` currently is an object
* It is possible to use `CallbackGame` for now * It is possible to use `CallbackGame` for now
* Now it is possible to pass exception handler in webhook
### 0.26.3 ### 0.26.3

View File

@ -11,8 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.utils.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
import io.ktor.application.call import io.ktor.application.call
import io.ktor.request.receiveText import io.ktor.request.receiveText
import io.ktor.response.respond import io.ktor.response.respond
@ -45,6 +44,7 @@ suspend fun RequestsExecutor.setWebhook(
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
): Job { ): Job {
val executeDeferred = certificate ?.let { val executeDeferred = certificate ?.let {
@ -74,12 +74,18 @@ suspend fun RequestsExecutor.setWebhook(
module { module {
routing { routing {
post(listenRoute) { post(listenRoute) {
val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) handleSafely(
val update = nonstrictJsonFormat.fromJson( {
RawUpdate.serializer(), exceptionsHandler ?.invoke(it)
asJson }
) ) {
updatesChannel.send(update.asUpdate(asJson)) val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
val update = nonstrictJsonFormat.fromJson(
RawUpdate.serializer(),
asJson
)
updatesChannel.send(update.asUpdate(asJson))
}
call.respond("Ok") call.respond("Ok")
} }
} }
@ -113,11 +119,6 @@ suspend fun RequestsExecutor.setWebhook(
launch { launch {
for (update in updatesChannel) { for (update in updatesChannel) {
val data = update.data val data = update.data
if (data is MediaGroupUpdate) {
} else {
}
when (data) { when (data) {
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
else -> block(update) else -> block(update)
@ -139,6 +140,33 @@ suspend fun RequestsExecutor.setWebhook(
} }
} }
suspend fun RequestsExecutor.setWebhook(
url: String,
port: Int,
engineFactory: ApplicationEngineFactory<*, *>,
listenHost: String = "0.0.0.0",
listenRoute: String = "/",
certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null,
block: UpdateReceiver<Update>
) = setWebhook(
url,
port,
engineFactory,
listenHost,
listenRoute,
certificate,
privateKeyConfig,
scope,
allowedUpdates,
maxAllowedConnections,
null,
block
)
suspend fun RequestsExecutor.setWebhook( suspend fun RequestsExecutor.setWebhook(
url: String, url: String,
port: Int, port: Int,