simbot-component-telegram
0.0.12indexedEfficiently create Telegram bots with a versatile library supporting asynchronous operations, component collaboration, advanced encapsulation, and integration with frameworks like Spring Boot.
Efficiently create Telegram bots with a versatile library supporting asynchronous operations, component collaboration, advanced encapsulation, and integration with frameworks like Spring Boot.
中文 | English
[!caution] WIP.
Some thoughts? Feel free to share or join the communities.
[!warning] This content is machine-translated.
This is a Telegram Bot API/SDK Kotlin multi-platform library based on Kotlin coroutines, efficient and asynchronous, Java-friendly.
It is also a component library of Simple Robot v4 (simbot), which is a subproject of simbot. With the capabilities provided by the simbot core library, it can support more advanced encapsulation, as well as component collaboration, Spring support, and more.
Serialization and network requests are based on Kotlin serialization and Ktor.
To use the simbot component library, you first need to add the core implementation of simbot
(such as the core library (simbot-core) or Spring Boot starter (simbot-core-spring-boot-starter)),
and then add the component library dependencies of the Telegram (simbot-component-telegram-core).
[!note] The version of the simbot core implementation library (
SIMBOT_VERSIONbelow) goes here for reference;Telegram Component library versions (
VERSIONbelow) go to the release reference.
With simbot core
build.gradle.kts
plugins {
kotlin("...") version "..."
}
dependencies {
implementation("love.forte.simbot:simbot-core:${SIMBOT_VERSION}")
implementation("love.forte.simbot.component:simbot-component-telegram-core:$VERSION")
}
pom.xml
With simbot spring boot starter
build.gradle.kts
plugins {
kotlin("jvm") version "..."
}
dependencies {
implementation("love.forte.simbot:simbot-core-spring-boot-starter:${SIMBOT_VERSION}")
implementation("love.forte.simbot.component:simbot-component-telegram-core:$VERSION")
}
pom.xml
The Telegram component uses Ktor as the HTTP client implementation, but does not rely on any specific engine by default.
Therefore, you need to choose and use a Ktor Client engine implementation.
You can go to the Ktor documentation to select a suitable Client Engine for your platform.
Take the JVM platform as an example:
runtimeOnly("io.ktor:ktor-client-java:$ktor_version")
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-java-jvm
${ktor_version}
runtime
simbot core
simbot Spring Boot starter
The configuration file *.bot.json
Comments are not supported. Remember to clean them up when you use them.
{
"component": "simbot.telegram",
"ticket": {
"token": "Your FULL Bot Token, e.g. Bot123456789:aaaabbbbcccc"
},
// config and its properties are optional and default to `null`.
"config": {
"server": null,
"proxy": null,
"longPolling": null
}
}
{
"component": "simbot.telegram",
"ticket": {
"token": "Your FULL Bot Token, e.g. Bot123456789:aaaabbbbcccc"
},
// config and its properties are optional and default to `null`.
"config": {
"server": null,
"proxy": null,
// config the `longPolling` to subscribe evnets
"longPolling": {
"limit": 100
}
}
}
See CONTRIBUTING.md for more information!
We welcome you and look forward to it feedback or pull request, Thank you for your contribution and support!
simbot-component-telegram is open source under the LGPLv3 licence。
This program free software: you can redistribute it / modify it under the terms the GNU Lesser General
License published the Free Software Foundation, either version the License, (at your )
any later version.
This program distributed the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty MERCHANTABILITY FITNESS A PARTICULAR PURPOSE. See the GNU Lesser General License more
details.
You should have received a copy the GNU Lesser General License along this program.
, see <https://www.gnu.org/licenses/>.
<dependencies>
<dependency>
<groupId>love.forte.simbot</groupId>
<artifactId>simbot-core-jvm</artifactId>
<version>${SIMBOT_VERSION}</version>
</dependency>
<dependency>
<groupId>love.forte.simbot.component</groupId>
<artifactId>simbot-component-telegram-core-jvm</artifactId>
<version>${VERSION}</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>love.forte.simbot</groupId>
<artifactId>simbot-core-spring-boot-starter</artifactId>
<version>${SIMBOT_VERSION}</version>
</dependency>
<dependency>
<groupId>love.forte.simbot.component</groupId>
<artifactId>simbot-component-telegram-core-jvm</artifactId>
<version>${VERSION}</version>
</dependency>
</dependencies>
suspend fun main() {
val app = launchSimpleApplication {
useTelegram() // install Telegram Component
}
// subscribe to events
app.listeners {
// subscribe to ChatGroupMessageEvent
listen<ChatGroupMessageEvent> { event ->
// process event...
event.reply("Hello!")
event.reply(At(event.authorId) + "Where are you?".toText())
// Required an result
EventResult.empty()
}
// subscribe to ChatGroupMessageEvent
process<TelegramPrivateMessageEvent> { event ->
// process event...
event.content().send("Welcome, " + At(event.member().id))
// Without result in `process<T>`
}
}
// register bots
app.telegramBots {
// register a bot via token
val bot = register(token = "botaaabbb.123123444") {
// Config...
// The source stdlib bot config
botConfiguration {
apiClientConfigurer {
engine {
// A proxy?
proxy = ProxyBuilder.http("http://127.0.0.1:7790")
}
}
// Enable longPolling?
longPolling = LongPolling(
limit = 100,
timeout = 10.minutes.inWholeSeconds.toInt(),
allowedUpdates = setOf(UpdateValues.MESSAGE_NAME),
// Enable retry?
retry = LongPolling.Retry()
)
}
}
// start the bot
bot.start()
}
app.join()
}
// enable
class App
fun main(args: Array<String>) {
runApplication<App>(*args)
}
class MyHandles {
// subscribe to ChatGroupMemberIncreaseEvent
suspend fun handleMemberIncrease(event: ChatGroupMemberIncreaseEvent) {
// ...
}
// subscribe to ChatGroupMessageEvent
suspend fun handleGroupMessage(event: ChatGroupMessageEvent) {
event.reply("Hello!")
}
}
Surfaced from shared tags and platforms — no rankings paid for.