All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook.kt Maven / Gradle / Ivy

There is a newer version: 0.28.3
Show newest version
package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook

import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.DataRequest
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.*
import kotlinx.serialization.internal.BooleanSerializer

fun SetWebhook(
    url: String,
    certificate: InputFile,
    maxAllowedConnections: Int? = null,
    allowedUpdates: List? = null
) : Request {
    val data = SetWebhook(
        url,
        (certificate as? FileId) ?.fileId,
        maxAllowedConnections,
        allowedUpdates
    )
    return when (certificate) {
        is FileId -> data
        is MultipartFile -> MultipartRequestImpl(
            data,
            mapOf(certificateField to certificate)
        )
    }
}

fun SetWebhook(
    url: String,
    maxAllowedConnections: Int? = null,
    allowedUpdates: List? = null
) : Request = SetWebhook(
    url,
    null,
    maxAllowedConnections,
    allowedUpdates
)

@Serializable
data class SetWebhook internal constructor(
    @SerialName(urlField)
    val url: String,
    @SerialName(certificateField)
    val certificateFile: String? = null,
    @SerialName(maxAllowedConnectionsField)
    val maxAllowedConnections: Int? = null,
    @SerialName(allowedUpdatesField)
    val allowedUpdates: List? = null
) : DataRequest {
    override fun method(): String = "setWebhook"
    override val resultDeserializer: DeserializationStrategy
        get() = BooleanSerializer
    override val requestSerializer: SerializationStrategy<*>
        get() = serializer()

    init {
        maxAllowedConnections ?.let {
            if (it !in allowedConnectionsLength) {
                throw IllegalArgumentException("Allowed connection for webhook must be in $allowedConnectionsLength range (but passed $it)")
            }
        }
    }
}

@Deprecated("Deprecated due to extracting into separated library")
suspend fun RequestsExecutor.setWebhookInfo(
    url: String,
    certificate: FileId,
    maxAllowedConnections: Int? = null,
    allowedUpdates: List? = null
) = execute(
    SetWebhook(
        url, certificate.fileId, maxAllowedConnections, allowedUpdates
    )
)

@Deprecated("Deprecated due to extracting into separated library")
suspend fun RequestsExecutor.setWebhookInfo(
    url: String,
    certificate: MultipartFile,
    maxAllowedConnections: Int? = null,
    allowedUpdates: List? = null
) = execute(
    MultipartRequestImpl(
        SetWebhook(
            url, null, maxAllowedConnections, allowedUpdates
        ),
        mapOf(certificateField to certificate)
    )
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy