Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save centralhardware/cf06718bb5da620e0715b5e76baba116 to your computer and use it in GitHub Desktop.
Save centralhardware/cf06718bb5da620e0715b5e76baba116 to your computer and use it in GitHub Desktop.
package me.centralhardware.znatoki.telegram.statistic.telegram
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.bot.ktor.KtorPipelineStepsHolder
import dev.inmo.tgbotapi.requests.GetUpdates
import dev.inmo.tgbotapi.requests.abstracts.Request
import io.ktor.client.plugins.HttpRequestTimeoutException
import kotlinx.coroutines.flow.MutableStateFlow
class HealthCheckKtorPipelineStepsHolder : KtorPipelineStepsHolder {
val health: MutableStateFlow<Boolean> = MutableStateFlow(false);
override suspend fun <T : Any> onRequestException(request: Request<T>, t: Throwable): T? {
if (t is HttpRequestTimeoutException &&
t.message!!.startsWith("Request timeout has expired [url=https://api.telegram.org/bot")) {
health.value = true
} else {
health.value = false
}
return super.onRequestException(request, t)
}
override suspend fun <T : Any> onRequestReturnResult(
result: Result<T>,
request: Request<T>,
callsFactories: List<KtorCallFactory>
): T {
if (request is GetUpdates && result.isSuccess) {
health.value = true
}
return super.onRequestReturnResult(result, request, callsFactories)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment