update new samples

This commit is contained in:
InsanusMokrassar 2024-08-30 19:06:38 +06:00
parent 999c33b2f5
commit 777604e5a0

View File

@ -1,3 +1,4 @@
import dev.inmo.kslog.common.*
import dev.inmo.tgbotapi.extensions.api.* import dev.inmo.tgbotapi.extensions.api.*
import dev.inmo.tgbotapi.extensions.api.bot.* import dev.inmo.tgbotapi.extensions.api.bot.*
import dev.inmo.tgbotapi.extensions.api.send.* import dev.inmo.tgbotapi.extensions.api.send.*
@ -13,56 +14,72 @@ import dev.inmo.tgbotapi.utils.*
suspend fun main(args: Array<String>) { suspend fun main(args: Array<String>) {
val token = args.first() 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) val bot = telegramBot(token)
bot.buildBehaviourWithLongPolling { bot.buildBehaviourWithLongPolling {
val me = getMe() val me = getMe()
val filterSelfUpdates = SimpleFilter<ChatMemberUpdated> { val filterSelfUpdates = SimpleFilter<ChatMemberUpdated> {
it.newChatMemberState.user.id == me.id it.member.id == me.id
} }
// This bot updates
onChatMemberJoined(initialFilter = filterSelfUpdates) { onChatMemberJoined(initialFilter = filterSelfUpdates) {
println("Bot was added to chat") internalLogger.i("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") 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) { onChatMemberGotPromoted(initialFilter = filterSelfUpdates) {
println("Bot was granted admin permissions") internalLogger.i("Bot was granted admin permissions")
sendMessage(it.chat.id, "I was promoted to admin. I now can watch other users' events") sendMessage(it.chat.id, "I was promoted to admin. I now can watch other users' events")
} }
onChatMemberGotDemoted(initialFilter = filterSelfUpdates) { onChatMemberGotDemoted(initialFilter = filterSelfUpdates) {
println("Admin permissions were revoked") internalLogger.i("Admin permissions were revoked")
sendMessage(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events") sendMessage(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events")
} }
// All users updates
onChatMemberJoined { onChatMemberJoined {
val member = it.newChatMemberState.user val member = it.member
println("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") internalLogger.i("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "Welcome ${member.firstName}") sendMessage(it.chat.id, "Welcome ${member.firstName}")
} }
onChatMemberLeft { onChatMemberLeft {
val member = it.newChatMemberState.user val member = it.member
println("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") internalLogger.i("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
sendMessage(it.chat.id, "Goodbye ${member.firstName}") sendMessage(it.chat.id, "Goodbye ${member.firstName}")
} }
onChatMemberGotPromoted { onChatMemberGotPromoted {
val newState = it.newChatMemberState.requireAdministratorChatMember() val newState = it.newChatMemberState.administratorChatMemberOrThrow()
println("${newState.user.firstName} got promoted to ${newState.customTitle ?: "Admin"}: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") internalLogger.i("${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"}") sendMessage(it.chat.id, "${newState.user.firstName} is now an ${newState.customTitle ?: "Admin"}")
} }
onChatMemberGotDemoted { onChatMemberGotDemoted {
val member = it.newChatMemberState.user val member = it.member
println("${member.firstName} got demoted: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") internalLogger.i("${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") sendMessage(it.chat.id, "${member.firstName} is now got demoted back to member")
} }
onChatMemberGotPromotionChanged { onChatMemberGotPromotionChanged {
val member = it.newChatMemberState.requireAdministratorChatMember() val member = it.newChatMemberState.administratorChatMemberOrThrow()
println("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") internalLogger.i("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}")
} }
}.join() }.join()
} }