mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-22 08:37:18 +00:00
commit
b6c92f754f
10
MemberUpdatedWatcherBot/README.md
Normal file
10
MemberUpdatedWatcherBot/README.md
Normal 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"
|
||||
```
|
21
MemberUpdatedWatcherBot/build.gradle
Normal file
21
MemberUpdatedWatcherBot/build.gradle
Normal 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"
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
import dev.inmo.kslog.common.*
|
||||
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.filters.chatMemberGotRestrictedFilter
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.chatMemberGotRestrictionsChangedFilter
|
||||
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 isDebug = args.any { it == "debug" }
|
||||
|
||||
if (isDebug) {
|
||||
setDefaultKSLog(
|
||||
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
val internalLogger = KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||
println(defaultMessageFormatter(level, tag ?: "ChatMemberUpdates", message, throwable))
|
||||
}
|
||||
|
||||
val bot = telegramBot(token)
|
||||
|
||||
bot.buildBehaviourWithLongPolling {
|
||||
val me = getMe()
|
||||
val filterSelfUpdates = SimpleFilter<ChatMemberUpdated> {
|
||||
it.member.id == me.id
|
||||
}
|
||||
|
||||
// This bot updates
|
||||
onChatMemberJoined(initialFilter = filterSelfUpdates) {
|
||||
internalLogger.i("Bot was added to chat")
|
||||
send(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) {
|
||||
internalLogger.i("Bot was granted admin permissions")
|
||||
send(it.chat.id, "I was promoted to admin. I now can watch other users' events")
|
||||
}
|
||||
|
||||
onChatMemberGotDemoted(initialFilter = filterSelfUpdates) {
|
||||
internalLogger.i("Admin permissions were revoked")
|
||||
send(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events")
|
||||
}
|
||||
|
||||
// All users updates
|
||||
onChatMemberJoined {
|
||||
val member = it.member
|
||||
internalLogger.i("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
|
||||
send(it.chat.id, "Welcome ${member.firstName}")
|
||||
}
|
||||
|
||||
onChatMemberLeft {
|
||||
val member = it.member
|
||||
internalLogger.i("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
|
||||
send(it.chat.id, "Goodbye ${member.firstName}")
|
||||
}
|
||||
|
||||
onChatMemberGotPromoted {
|
||||
val newState = it.newChatMemberState.administratorChatMemberOrThrow()
|
||||
internalLogger.i("${newState.user.firstName} got promoted to ${newState.customTitle ?: "Admin"}: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
|
||||
send(it.chat.id, "${newState.user.firstName} is now an ${newState.customTitle ?: "Admin"}")
|
||||
}
|
||||
|
||||
onChatMemberGotDemoted {
|
||||
val member = it.member
|
||||
internalLogger.i("${member.firstName} got demoted: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
|
||||
send(it.chat.id, "${member.firstName} is now got demoted back to member")
|
||||
}
|
||||
|
||||
onChatMemberGotPromotionChanged {
|
||||
val member = it.member
|
||||
val message = "${member.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}"
|
||||
internalLogger.i(message)
|
||||
send(it.chat.id, message)
|
||||
}
|
||||
|
||||
onChatMemberUpdated(
|
||||
initialFilter = chatMemberGotRestrictedFilter + chatMemberGotRestrictionsChangedFilter,
|
||||
) {
|
||||
val member = it.member
|
||||
val message = "${member.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}"
|
||||
internalLogger.i(message)
|
||||
send(it.chat.id, message)
|
||||
}
|
||||
}.join()
|
||||
}
|
@ -106,7 +106,7 @@ suspend fun main(args: Array<String>) {
|
||||
|
||||
suspend fun BehaviourContext.getUserChatPermissions(chatId: ChatId, userId: UserId): ChatPermissions? {
|
||||
val chatMember = getChatMember(chatId, userId)
|
||||
return chatMember.restrictedChatMemberOrNull() ?: chatMember.whenMemberChatMember {
|
||||
return chatMember.restrictedMemberChatMemberOrNull() ?: chatMember.whenMemberChatMember {
|
||||
getChat(chatId).extendedGroupChatOrNull() ?.permissions
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ org.gradle.jvmargs=-Xmx3148m
|
||||
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
||||
|
||||
|
||||
kotlin_version=2.0.10
|
||||
telegram_bot_api_version=17.0.0
|
||||
micro_utils_version=0.22.0
|
||||
serialization_version=1.7.1
|
||||
ktor_version=2.3.11
|
||||
kotlin_version=2.0.20
|
||||
telegram_bot_api_version=18.0.0
|
||||
micro_utils_version=0.22.2
|
||||
serialization_version=1.7.2
|
||||
ktor_version=2.3.12
|
||||
|
@ -53,3 +53,5 @@ include ":BusinessConnectionsBot"
|
||||
include ":StarTransactionsBot"
|
||||
|
||||
include ":CustomBot"
|
||||
|
||||
include ":MemberUpdatedWatcherBot"
|
||||
|
Loading…
Reference in New Issue
Block a user