Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
.gradle
build

# Kotlin
.kotlin

# IDEA
.idea
*.iml
Expand Down
1,275 changes: 842 additions & 433 deletions api/tbot-api.api

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ kotlin.sourceSets.main {
}

kotlin.compilerOptions {
freeCompilerArgs.add("-Xcontext-receivers")
freeCompilerArgs.add("-Xcontext-parameters")
}

val sourcesJar by tasks.registering(Jar::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ class BotApiGenerator {

for (entry in updateListenerEntries) {
appendLine()
appendLine(" context(TelegramBotApiContext)")
appendLine(" context(botApi: TelegramBotApiContext)")
appendLine(" suspend fun on${entry.typeWithoutUpdate}(${entry.field}: ${entry.fieldType}) {}")
}

appendLine()
appendLine(" context(TelegramBotApiContext)")
appendLine(" context(botApi: TelegramBotApiContext)")
appendLine(" suspend fun onUpdate(update: Update) {")
appendLine(" when (update) {")
for (entry in updateListenerEntries) {
Expand All @@ -91,7 +91,7 @@ class BotApiGenerator {
appendLine(" return object : $listenerTypeName {")
for (entry in updateListenerEntries) {
appendLine()
appendLine(" context(TelegramBotApiContext)")
appendLine(" context(botApi: TelegramBotApiContext)")
appendLine(" override suspend fun on${entry.typeWithoutUpdate}(${entry.field}: ${entry.fieldType}) =")
appendLine(" on${entry.typeWithoutUpdate}?.handle(${entry.field}) ?: Unit")
}
Expand Down Expand Up @@ -329,11 +329,11 @@ class BotApiGenerator {
val tryWithContextMethodSourceCode = buildString {
appendLine()
appendMethodDoc(description, parameters)
appendLine("context(TelegramBotApiContext)")
appendLine("context(botApi: TelegramBotApiContext)")
append("suspend fun $tryMethodName(")
appendParameters(apiMethodName, parameters)
appendLine("): TelegramResponse<${returnType.value}> =")
appendLine(" botApiClient.$tryMethodName($requestValueArg)")
appendLine(" botApi.botApiClient.$tryMethodName($requestValueArg)")
}

// Convenience method
Expand All @@ -351,21 +351,21 @@ class BotApiGenerator {
val methodWithContextSourceCode = buildString {
appendLine()
appendMethodDoc(description, parameters)
appendLine("context(TelegramBotApiContext)")
appendLine("context(botApi: TelegramBotApiContext)")
appendLine("@Throws(TelegramBotApiException::class)")
appendLine("@JvmName(\"call${methodBaseName.toTitleCase()}\")")
append("suspend fun $methodBaseName(")
appendParameters(apiMethodName, parameters)
appendLine("): ${returnType.value} =")
appendLine(" botApiClient.$tryMethodName($requestValueArg).getResultOrThrow()")
appendLine(" botApi.botApiClient.$tryMethodName($requestValueArg).getResultOrThrow()")
}

val fluentContextMethods = fluentMethods.filter { it.delegateName == methodBaseName }.map { fluent ->
val unexpectedArgs = fluent.args.keys - parameters.map { it.name }.toSet()
check(unexpectedArgs.isEmpty()) { "Unexpected replacement args for fluent method ${fluent.name}/$methodBaseName: $unexpectedArgs" }

buildString {
appendLine("context(TelegramBotApiContext)")
appendLine("context(botApi: TelegramBotApiContext)")
appendLine("@Throws(TelegramBotApiException::class)")
append("suspend fun ${fluent.receiver}.${fluent.name}(")
appendParameters(apiMethodName, parameters.filter { it.name !in fluent.args })
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[versions]
jdkTarget = "11"
jvmToolchain = "21"
kotlinTarget = "2.0.0"
kotlinPlugin = "2.1.20"
kotlinTarget = "2.2.0"
kotlinPlugin = "2.2.0"
kotlinx-coroutines = "1.7.3"
kotlinx-serialization = "1.8.1"
ktor = "2.3.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,73 @@ import me.alllex.tbot.api.model.*

interface TelegramBotUpdateListener {

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onMessage(message: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onEditedMessage(editedMessage: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onChannelPost(channelPost: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onEditedChannelPost(editedChannelPost: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onInlineQuery(inlineQuery: InlineQuery) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onChosenInlineResult(chosenInlineResult: ChosenInlineResult) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onCallbackQuery(callbackQuery: CallbackQuery) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onShippingQuery(shippingQuery: ShippingQuery) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onPreCheckoutQuery(preCheckoutQuery: PreCheckoutQuery) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onPoll(poll: Poll) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onPollAnswer(pollAnswer: PollAnswer) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onMyChatMember(myChatMember: ChatMemberUpdated) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onChatMember(chatMember: ChatMemberUpdated) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onChatJoinRequest(chatJoinRequest: ChatJoinRequest) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onBusinessConnection(businessConnection: BusinessConnection) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onBusinessMessage(businessMessage: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onDeletedBusinessMessages(deletedBusinessMessages: BusinessMessagesDeleted) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onEditedBusinessMessage(editedBusinessMessage: Message) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onMessageReactionCount(messageReactionCount: MessageReactionCountUpdated) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onMessageReaction(messageReaction: MessageReactionUpdated) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onChatBoost(chatBoost: ChatBoostUpdated) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onRemovedChatBoost(removedChatBoost: ChatBoostRemoved) {}

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
suspend fun onUpdate(update: Update) {
when (update) {
is MessageUpdate -> onMessage(update.message)
Expand Down Expand Up @@ -130,91 +130,91 @@ fun TelegramBotUpdateListener(
): TelegramBotUpdateListener {
return object : TelegramBotUpdateListener {

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onMessage(message: Message) =
onMessage?.handle(message) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onEditedMessage(editedMessage: Message) =
onEditedMessage?.handle(editedMessage) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onChannelPost(channelPost: Message) =
onChannelPost?.handle(channelPost) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onEditedChannelPost(editedChannelPost: Message) =
onEditedChannelPost?.handle(editedChannelPost) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onInlineQuery(inlineQuery: InlineQuery) =
onInlineQuery?.handle(inlineQuery) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onChosenInlineResult(chosenInlineResult: ChosenInlineResult) =
onChosenInlineResult?.handle(chosenInlineResult) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onCallbackQuery(callbackQuery: CallbackQuery) =
onCallbackQuery?.handle(callbackQuery) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onShippingQuery(shippingQuery: ShippingQuery) =
onShippingQuery?.handle(shippingQuery) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onPreCheckoutQuery(preCheckoutQuery: PreCheckoutQuery) =
onPreCheckoutQuery?.handle(preCheckoutQuery) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onPoll(poll: Poll) =
onPoll?.handle(poll) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onPollAnswer(pollAnswer: PollAnswer) =
onPollAnswer?.handle(pollAnswer) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onMyChatMember(myChatMember: ChatMemberUpdated) =
onMyChatMember?.handle(myChatMember) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onChatMember(chatMember: ChatMemberUpdated) =
onChatMember?.handle(chatMember) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onChatJoinRequest(chatJoinRequest: ChatJoinRequest) =
onChatJoinRequest?.handle(chatJoinRequest) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onBusinessConnection(businessConnection: BusinessConnection) =
onBusinessConnection?.handle(businessConnection) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onBusinessMessage(businessMessage: Message) =
onBusinessMessage?.handle(businessMessage) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onDeletedBusinessMessages(deletedBusinessMessages: BusinessMessagesDeleted) =
onDeletedBusinessMessages?.handle(deletedBusinessMessages) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onEditedBusinessMessage(editedBusinessMessage: Message) =
onEditedBusinessMessage?.handle(editedBusinessMessage) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onMessageReactionCount(messageReactionCount: MessageReactionCountUpdated) =
onMessageReactionCount?.handle(messageReactionCount) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onMessageReaction(messageReaction: MessageReactionUpdated) =
onMessageReaction?.handle(messageReaction) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onChatBoost(chatBoost: ChatBoostUpdated) =
onChatBoost?.handle(chatBoost) ?: Unit

context(TelegramBotApiContext)
context(botApi: TelegramBotApiContext)
override suspend fun onRemovedChatBoost(removedChatBoost: ChatBoostRemoved) =
onRemovedChatBoost?.handle(removedChatBoost) ?: Unit
}
Expand Down
Loading