1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt

146 lines
5.1 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.requests.webhook
2019-02-26 01:45:56 +00:00
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.requests.abstracts.*
2020-11-10 14:44:39 +00:00
import dev.inmo.tgbotapi.requests.send.media.base.*
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.*
2019-02-26 01:45:56 +00:00
import kotlinx.serialization.*
2020-03-22 07:37:01 +00:00
import kotlinx.serialization.builtins.serializer
2019-02-26 01:45:56 +00:00
2020-05-16 14:43:09 +00:00
private fun correctWebhookUrl(sourceUrl: String) = if (sourceUrl.contains("://")) {
sourceUrl
} else {
"https://$sourceUrl"
}
2020-11-10 14:44:39 +00:00
sealed class SetWebhookRequest : Request<Boolean>
class MultipartSetWebhookRequest(
2019-02-26 01:45:56 +00:00
url: String,
certificate: MultipartFile,
2020-11-04 16:43:26 +00:00
ipAddress: String? = null,
2019-02-26 01:45:56 +00:00
maxAllowedConnections: Int? = null,
2020-11-04 16:43:26 +00:00
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
2020-11-10 14:44:39 +00:00
) : SetWebhookRequest(), MultipartRequest<Boolean> by MultipartRequestImpl(
SetWebhook(
2020-05-16 14:43:09 +00:00
correctWebhookUrl(url),
2020-11-04 16:43:26 +00:00
null as String?,
ipAddress,
2019-02-26 01:45:56 +00:00
maxAllowedConnections,
2020-11-04 16:43:26 +00:00
allowedUpdates,
dropPendingUpdates
),
mapOf(certificateField to certificate)
)
2020-11-10 14:44:39 +00:00
fun SetWebhook(
url: String,
certificate: MultipartFile,
ipAddress: String? = null,
maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
): MultipartSetWebhookRequest = MultipartSetWebhookRequest(
correctWebhookUrl(url),
certificate,
ipAddress,
maxAllowedConnections,
allowedUpdates,
dropPendingUpdates
)
fun SetWebhook(
url: String,
certificate: FileId,
2020-11-04 16:43:26 +00:00
ipAddress: String? = null,
maxAllowedConnections: Int? = null,
2020-11-04 16:43:26 +00:00
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
): SetWebhook = SetWebhook(
2020-05-16 14:43:09 +00:00
correctWebhookUrl(url),
certificate.fileId,
2020-11-04 16:43:26 +00:00
ipAddress,
maxAllowedConnections,
2020-11-04 16:43:26 +00:00
allowedUpdates,
dropPendingUpdates
)
2019-02-26 01:45:56 +00:00
2020-11-04 16:43:26 +00:00
/**
* Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update
* for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update.
*
* If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the [url],
* e.g. https://www.example.com/<token>. Since nobody else knows your bot's token, you can be pretty sure it's us.
*/
2020-05-16 16:02:57 +00:00
@Suppress("USELESS_CAST")
2020-05-15 12:39:12 +00:00
fun SetWebhook(
url: String,
2020-11-04 17:21:25 +00:00
certificate: InputFile,
2020-11-04 16:43:26 +00:00
ipAddress: String? = null,
2020-05-15 12:39:12 +00:00
maxAllowedConnections: Int? = null,
2020-11-04 16:43:26 +00:00
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
2020-11-10 14:44:39 +00:00
) = when (certificate) {
2020-11-04 16:43:26 +00:00
is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates)
is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates)
2020-05-15 12:39:12 +00:00
}
2020-11-04 17:21:25 +00:00
/**
* Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update
* for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update.
*
* If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the [url],
* e.g. https://www.example.com/<token>. Since nobody else knows your bot's token, you can be pretty sure it's us.
*/
@Suppress("USELESS_CAST")
fun SetWebhook(
url: String,
ipAddress: String? = null,
maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
2020-11-10 14:44:39 +00:00
) = SetWebhook(
2020-11-04 17:21:25 +00:00
correctWebhookUrl(url),
null,
ipAddress,
maxAllowedConnections,
allowedUpdates,
dropPendingUpdates
)
2020-11-04 16:43:26 +00:00
/**
* Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update
* for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update.
*
* If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the [url],
* e.g. https://www.example.com/<token>. Since nobody else knows your bot's token, you can be pretty sure it's us.
*/
2019-02-26 01:45:56 +00:00
@Serializable
data class SetWebhook internal constructor(
@SerialName(urlField)
val url: String,
@SerialName(certificateField)
val certificateFile: String? = null,
2020-11-04 16:43:26 +00:00
@SerialName(ipAddressField)
val ipAddress: String? = null,
2019-02-26 01:45:56 +00:00
@SerialName(maxAllowedConnectionsField)
val maxAllowedConnections: Int? = null,
@SerialName(allowedUpdatesField)
2020-11-04 16:43:26 +00:00
val allowedUpdates: List<String>? = null,
@SerialName(dropPendingUpdatesField)
val dropPendingUpdates: Boolean? = null
2020-11-10 14:44:39 +00:00
) : SetWebhookRequest(), DataRequest<Boolean> {
2019-02-26 01:45:56 +00:00
override fun method(): String = "setWebhook"
2019-12-02 08:35:37 +00:00
override val resultDeserializer: DeserializationStrategy<Boolean>
2020-03-22 07:37:01 +00:00
get() = Boolean.serializer()
2019-12-02 08:35:37 +00:00
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
2019-02-26 01:45:56 +00:00
init {
maxAllowedConnections ?.let {
if (it !in allowedConnectionsLength) {
throw IllegalArgumentException("Allowed connection for webhook must be in $allowedConnectionsLength range (but passed $it)")
}
}
}
}