Add MemberUpdatedWatcherBot example utilizing new 18.0.0 extensions

This commit is contained in:
McModder 2024-08-29 22:38:41 +03:00
parent 590f9ec6d8
commit 3efd3463a3
No known key found for this signature in database
GPG Key ID: A47A5642B5DA64BE
5 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,10 @@
# MemberUpdatedWatcherBot
This bot will watch for some ChatMemberUpdated events using new extensions from 18.0.0
## Launch
```bash
../gradlew run --args="BOT_TOKEN"
```

View File

@ -0,0 +1,21 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName="MemberUpdatedWatcherKt"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
}

View File

@ -0,0 +1,68 @@
import dev.inmo.tgbotapi.extensions.api.*
import dev.inmo.tgbotapi.extensions.api.bot.*
import dev.inmo.tgbotapi.extensions.api.send.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.*
import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.types.chat.member.*
import dev.inmo.tgbotapi.utils.*
@OptIn(PreviewFeature::class)
suspend fun main(args: Array<String>) {
val token = args.first()
val bot = telegramBot(token)
bot.buildBehaviourWithLongPolling {
val me = getMe()
val filterSelfUpdates = SimpleFilter<ChatMemberUpdated> {
it.newChatMemberState.user.id == me.id
}
onChatMemberJoined(initialFilter = filterSelfUpdates) {
println("Bot was added to chat")
sendMessage(it.chat.id, "I was added to chat. Please grant me admin permissions to make me able to watch other users' events")
}
onChatMemberGotPromoted(initialFilter = filterSelfUpdates) {
println("Bot was granted admin permissions")
sendMessage(it.chat.id, "I was promoted to admin. I now can watch other users' events")
}
onChatMemberGotDemoted(initialFilter = filterSelfUpdates) {
println("Admin permissions were revoked")
sendMessage(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events")
}
onChatMemberJoined {
val member = it.newChatMemberState.user
println("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "Welcome ${member.firstName}")
}
onChatMemberLeft {
val member = it.newChatMemberState.user
println("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "Goodbye ${member.firstName}")
}
onChatMemberGotPromoted {
val newState = it.newChatMemberState.requireAdministratorChatMember()
println("${newState.user.firstName} got promoted to ${newState.customTitle ?: "Admin"}: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "${newState.user.firstName} is now an ${newState.customTitle ?: "Admin"}")
}
onChatMemberGotDemoted {
val member = it.newChatMemberState.user
println("${member.firstName} got demoted: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "${member.firstName} is now got demoted back to member")
}
onChatMemberGotPromotionChanged {
val member = it.newChatMemberState.requireAdministratorChatMember()
println("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
}
}.join()
}

View File

@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
kotlin_version=2.0.10
telegram_bot_api_version=17.0.0
telegram_bot_api_version=18.0.0
micro_utils_version=0.22.0
serialization_version=1.7.1
ktor_version=2.3.11

View File

@ -53,3 +53,5 @@ include ":BusinessConnectionsBot"
include ":StarTransactionsBot"
include ":CustomBot"
include ":MemberUpdatedWatcherBot"