diff --git a/MemberUpdatedWatcherBot/README.md b/MemberUpdatedWatcherBot/README.md new file mode 100644 index 0000000..3f53d94 --- /dev/null +++ b/MemberUpdatedWatcherBot/README.md @@ -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" +``` diff --git a/MemberUpdatedWatcherBot/build.gradle b/MemberUpdatedWatcherBot/build.gradle new file mode 100644 index 0000000..e851296 --- /dev/null +++ b/MemberUpdatedWatcherBot/build.gradle @@ -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" +} diff --git a/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt new file mode 100644 index 0000000..0480d2d --- /dev/null +++ b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt @@ -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) { + val token = args.first() + + val bot = telegramBot(token) + + bot.buildBehaviourWithLongPolling { + val me = getMe() + val filterSelfUpdates = SimpleFilter { + 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() +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 637922f..698fbac 100644 --- a/settings.gradle +++ b/settings.gradle @@ -53,3 +53,5 @@ include ":BusinessConnectionsBot" include ":StarTransactionsBot" include ":CustomBot" + +include ":MemberUpdatedWatcherBot"