1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

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
* `CallbackGame` currently is an object
* It is possible to use `CallbackGame` for now
* Now it is possible to pass exception handler in webhook
### 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.UpdatesFilter
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
import com.github.insanusmokrassar.TelegramBotAPI.utils.*
import io.ktor.application.call
import io.ktor.request.receiveText
import io.ktor.response.respond
@ -45,6 +44,7 @@ suspend fun RequestsExecutor.setWebhook(
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null,
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
block: UpdateReceiver<Update>
): Job {
val executeDeferred = certificate ?.let {
@ -74,12 +74,18 @@ suspend fun RequestsExecutor.setWebhook(
module {
routing {
post(listenRoute) {
val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
val update = nonstrictJsonFormat.fromJson(
RawUpdate.serializer(),
asJson
)
updatesChannel.send(update.asUpdate(asJson))
handleSafely(
{
exceptionsHandler ?.invoke(it)
}
) {
val asJson = nonstrictJsonFormat.parseJson(call.receiveText())
val update = nonstrictJsonFormat.fromJson(
RawUpdate.serializer(),
asJson
)
updatesChannel.send(update.asUpdate(asJson))
}
call.respond("Ok")
}
}
@ -113,11 +119,6 @@ suspend fun RequestsExecutor.setWebhook(
launch {
for (update in updatesChannel) {
val data = update.data
if (data is MediaGroupUpdate) {
} else {
}
when (data) {
is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate)
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(
url: String,
port: Int,