Skip to content

Commit

Permalink
Added DSL and simplified polling logic.
Browse files Browse the repository at this point in the history
Changes:
- Most of the polling logic has been replaced with coroutine code.
- Added TelegramBotUpdateBuilder DSL to build the bot
- Exposed TelegramBotApiClient's properties to allows custom extension
- Simplified TelegramBotApiClient's HttpClient defaults
- Replaced sealed classes with sealed interfaces
- Added logger and json to TelegramBotApiContext
- Removed @OptIn annotations
- Expanded star imports for code readability outside IDE
- bump library versions
- bump gradle wrapper version
  • Loading branch information
lamba92 committed Dec 26, 2023
1 parent 2c37933 commit 9df1523
Show file tree
Hide file tree
Showing 20 changed files with 1,737 additions and 1,892 deletions.
14 changes: 12 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,24 @@ tasks.test {
}

val generateTelegramBotApi by tasks.registering(GenerateTelegramBotApiTask::class) {
group = "telegram"
apiSpecFile = layout.projectDirectory.file("api-spec/telegram-bot-api.html")
packageName = "me.alllex.tbot.api.model"
telegramClientPackage = "me.alllex.tbot.api.client"
outputDirectory = layout.projectDirectory.dir("src/main/generated-kotlin")
}

kotlin.sourceSets.main {
kotlin.srcDir(generateTelegramBotApi)
kotlin.sourceSets {
all {
languageSettings {
optIn("me.alllex.tbot.api.client.BotKitInternalAPI")
optIn("kotlinx.serialization.ExperimentalSerializationApi")
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
}
}
main {
kotlin.srcDir(generateTelegramBotApi)
}
}

kotlin.compilerOptions {
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ repositories {
}

dependencies {
implementation("org.jsoup:jsoup:1.16.1")
implementation("me.alllex.parsus:parsus-jvm:0.5.5")
implementation("org.jsoup:jsoup:1.17.1")
implementation("me.alllex.parsus:parsus-jvm:0.6.0")
}

val updateApiSpec by tasks.registering {
Expand Down
8 changes: 7 additions & 1 deletion buildSrc/src/main/kotlin/GenerateTelegramBotApiTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction

@CacheableTask
abstract class GenerateTelegramBotApiTask : DefaultTask() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package me.alllex.tbot.apigen

import me.alllex.parsus.parser.*
import me.alllex.parsus.parser.Grammar
import me.alllex.parsus.parser.ParseException
import me.alllex.parsus.parser.getOrElse
import me.alllex.parsus.parser.map
import me.alllex.parsus.parser.or
import me.alllex.parsus.parser.parser
import me.alllex.parsus.parser.repeatOneOrMore
import me.alllex.parsus.token.regexToken
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
Expand Down
36 changes: 17 additions & 19 deletions buildSrc/src/main/kotlin/me/alllex/tbot/apigen/BotApiGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -554,20 +554,18 @@ class BotApiGenerator {
append("requestBody: $requestTypeName")
}
appendLine("): TelegramResponse<${returnType.value}> =")
appendLine(" executeRequest(\"$apiMethodName\", ${if (hasParams) "requestBody" else "null"}) {")
appendLine(" httpClient.$httpMethod {")
appendLine(" url {")
appendLine(" protocol = apiProtocol")
appendLine(" host = apiHost")
appendLine(" port = apiPort")
appendLine(" path(\"bot\$apiToken\", \"$apiMethodName\")")
appendLine(" }")
appendLine(" httpClient.$httpMethod {")
appendLine(" url {")
appendLine(" protocol = apiProtocol")
appendLine(" host = apiHost")
appendLine(" port = apiPort")
appendLine(" path(\"bot\$apiToken\", \"$apiMethodName\")")
appendLine(" }")
if (hasParams) {
appendLine(" contentType(ContentType.Application.Json)")
appendLine(" setBody(requestBody)")
appendLine(" contentType(ContentType.Application.Json)")
appendLine(" setBody(requestBody)")
}
appendLine(" }.body()")
appendLine(" }")
appendLine(" }.body()")
}

val tryMethodSourceCode = buildString {
Expand Down Expand Up @@ -738,9 +736,9 @@ class BotApiGenerator {
}
appendLine(" */")
appendLine("@Serializable(with = ${name}Serializer::class)")
appendLine("sealed class $name {")
appendLine(" abstract val updateId: Long")
appendLine(" abstract val updateType: UpdateType")
appendLine("sealed interface $name {")
appendLine(" val updateId: Long")
appendLine(" val updateType: UpdateType")
appendLine("}")
for (updateField in types) {
appendLine()
Expand All @@ -749,7 +747,7 @@ class BotApiGenerator {
appendLine("data class ${updateField.updateTypeName()}(")
appendLine(" override val updateId: Long,")
appendLine(" val ${updateField.name}: ${updateField.type},")
appendLine("): $name() {")
appendLine("): $name {")
appendLine(" override val updateType: UpdateType get() = UpdateType.${updateField.enumValue()}")
appendLine("}")
}
Expand Down Expand Up @@ -798,7 +796,7 @@ class BotApiGenerator {
appendLine("@JsonClassDiscriminator(\"$discriminatorFieldName\")")
}

appendLine("sealed class ${name.value}")
appendLine("sealed interface ${name.value}")

if (discriminatorFieldName == null) {
val avoidFields = setOf("description")
Expand Down Expand Up @@ -872,7 +870,7 @@ class BotApiGenerator {
append("data object ")
append(typeName.value)
if (sealedParentName != null) {
append(" : ${sealedParentName.value}()")
append(" : ${sealedParentName.value}")
}
appendLine()
} else {
Expand All @@ -884,7 +882,7 @@ class BotApiGenerator {

append(")")
if (sealedParentName != null) {
append(" : ${sealedParentName.value}()")
append(" : ${sealedParentName.value}")
}
appendLine(" {")
appendLine(" ${generateDebugToString(typeName.value, trueFields)}")
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/me/alllex/tbot/apigen/util.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.alllex.tbot.apigen

import java.util.*
import java.util.Locale


/**
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[versions]
jdkTarget = "8"
jvmToolchain = "21"
kotlinTarget = "1.9.0"
kotlinPlugin = "1.9.10"
kotlinx-coroutines = "1.6.4"
kotlinx-serialization = "1.5.1"
ktor = "2.3.2"
log4j = "2.20.0"
kotlinTarget = "1.9.20"
kotlinPlugin = "1.9.20"
kotlinx-coroutines = "1.7.3"
kotlinx-serialization = "1.6.2"
ktor = "2.3.7"
log4j = "2.22.0"
junit5 = "5.10.0"

[libraries]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading

0 comments on commit 9df1523

Please sign in to comment.