-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kTelegram for Kotlin Support (#53)
* Add kTelegram as a Kotlin extension * Add appropriate dependency scopes * Add typealias for ease of use
- Loading branch information
Showing
21 changed files
with
359 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
jtelegrambotapi-core/src/main/java/com/jtelegram/api/events/EventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package com.jtelegram.api.events; | ||
|
||
public interface EventHandler<E extends Event> { | ||
|
||
void onEvent(E event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.jtelegram</groupId> | ||
<artifactId>jtelegrambotapi</artifactId> | ||
<version>4.0.10</version> | ||
</parent> | ||
|
||
<properties> | ||
<kotlin.version>1.3.72</kotlin.version> | ||
</properties> | ||
|
||
<artifactId>ktelegrambotapi</artifactId> | ||
<name>jTelegramBotAPI as Kotlin (AKA kTelegramBotAPI)</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.jtelegram</groupId> | ||
<artifactId>jtelegrambotapi-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
<version>${kotlin.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlinx</groupId> | ||
<artifactId>kotlinx-coroutines-core</artifactId> | ||
<version>1.3.7</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<jvmTarget>1.8</jvmTarget> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
57 changes: 57 additions & 0 deletions
57
ktelegrambotapi/src/main/kotlin/com/jtelegram/api/kotlin/BotContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.jtelegram.api.kotlin | ||
|
||
import com.jtelegram.api.chat.Chat | ||
import com.jtelegram.api.chat.id.ChatId | ||
import com.jtelegram.api.message.Message | ||
import com.jtelegram.api.message.impl.TextMessage | ||
import com.jtelegram.api.requests.message.framework.ParseMode | ||
import com.jtelegram.api.requests.message.framework.req.SendableChatRequest | ||
import com.jtelegram.api.requests.message.framework.req.SendableMessageRequest | ||
import com.jtelegram.api.requests.message.send.SendText | ||
import com.jtelegram.api.user.User | ||
|
||
class BotContext(val bot: KTelegramBot) { | ||
private suspend fun <T> sendText(chatId: ChatId<T>, text: String, parseMode: ParseMode): TextMessage { | ||
return bot.execute ( | ||
SendText.builder() | ||
.chatId(chatId) | ||
.text(text) | ||
.parseMode(parseMode) | ||
.build() | ||
) | ||
} | ||
|
||
suspend fun Chat.sendText(text: String, parseMode: ParseMode = ParseMode.MARKDOWN): TextMessage { | ||
return sendText(chatId, text, parseMode) | ||
} | ||
|
||
suspend fun <ST, S: Message<ST>> Chat.sendMessage(request: SendableMessageRequest<S>): S { | ||
return sendAction(request) | ||
} | ||
|
||
suspend fun <S> Chat.sendAction(request: SendableChatRequest<S>): S { | ||
val chatIdO = chatId | ||
|
||
return bot.execute ( | ||
request.apply { | ||
chatId = chatIdO | ||
} | ||
) | ||
} | ||
|
||
suspend fun User.sendText(text: String, parseMode: ParseMode = ParseMode.MARKDOWN): TextMessage { | ||
return sendText(ChatId.of(id), text, parseMode) | ||
} | ||
|
||
suspend fun <ST, S: Message<ST>> User.sendMessage(request: SendableMessageRequest<S>): S { | ||
return sendAction(request) | ||
} | ||
|
||
suspend fun <S> User.sendAction(request: SendableChatRequest<S>): S { | ||
return bot.execute ( | ||
request.apply { | ||
chatId = ChatId.of(id) | ||
} | ||
) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
ktelegrambotapi/src/main/kotlin/com/jtelegram/api/kotlin/KTelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.jtelegram.api.kotlin | ||
|
||
import com.jtelegram.api.TelegramBot | ||
import com.jtelegram.api.TelegramBotRegistry | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.asCoroutineDispatcher | ||
|
||
class KTelegramBot constructor(registry: TelegramBotRegistry?, apiKey: String?) : TelegramBot(registry, apiKey) { | ||
val coroutineScope = CoroutineScope(eventRegistry.threadPool.asCoroutineDispatcher()) | ||
} |
36 changes: 36 additions & 0 deletions
36
ktelegrambotapi/src/main/kotlin/com/jtelegram/api/kotlin/TelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.jtelegram.api.kotlin | ||
|
||
import com.jtelegram.api.TelegramBot | ||
import com.jtelegram.api.requests.framework.AbstractTelegramRequest | ||
import com.jtelegram.api.requests.framework.QueryTelegramRequest | ||
import com.jtelegram.api.requests.framework.UpdateTelegramRequest | ||
import kotlin.coroutines.Continuation | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
suspend fun <T> TelegramBot.execute(request: QueryTelegramRequest<T>): T = suspendCoroutine { cont -> | ||
request.useContinuationForErrors(cont) | ||
|
||
request.setCallback { | ||
cont.resume(it) | ||
} | ||
|
||
perform(request) | ||
} | ||
|
||
suspend fun TelegramBot.execute(request: UpdateTelegramRequest) = suspendCoroutine<Unit?> { cont -> | ||
request.useContinuationForErrors(cont) | ||
|
||
request.setCallback { | ||
cont.resume(null) | ||
} | ||
|
||
perform(request) | ||
} | ||
|
||
private fun <T> AbstractTelegramRequest.useContinuationForErrors(cont: Continuation<T>) { | ||
setErrorHandler { | ||
cont.resumeWithException(it) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
ktelegrambotapi/src/main/kotlin/com/jtelegram/api/kotlin/TelegramBotRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.jtelegram.api.kotlin | ||
|
||
import com.jtelegram.api.TelegramBotRegistry | ||
import com.jtelegram.api.requests.GetMe | ||
|
||
suspend fun TelegramBotRegistry.registerBot(key: String): KTelegramBot { | ||
val bot = KTelegramBot(this, key) | ||
|
||
bot.botInfo = bot.execute(GetMe.builder().build()) | ||
|
||
bots.add(bot) | ||
updateProvider.listenFor(bot) | ||
|
||
return bot | ||
} |
22 changes: 22 additions & 0 deletions
22
ktelegrambotapi/src/main/kotlin/com/jtelegram/api/kotlin/commands/CommandRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jtelegram.api.kotlin.commands | ||
|
||
import com.jtelegram.api.commands.Command | ||
import com.jtelegram.api.commands.filters.CommandFilter | ||
import com.jtelegram.api.events.message.TextMessageEvent | ||
import com.jtelegram.api.kotlin.BotContext | ||
import com.jtelegram.api.kotlin.KTelegramBot | ||
import kotlinx.coroutines.launch | ||
|
||
fun suspendCommand(filter: suspend BotContext.(TextMessageEvent, Command) -> Unit) = CommandFilter { event, command -> | ||
val bot = event.bot | ||
|
||
if (bot !is KTelegramBot) { | ||
throw IllegalStateException("Suspending command filters can only be used with KTelegramBots!") | ||
} | ||
|
||
bot.coroutineScope.launch { | ||
filter.invoke(BotContext(bot), event, command) | ||
} | ||
|
||
true | ||
} |
Oops, something went wrong.