mirror of
				https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
				synced 2025-10-31 20:20:09 +00:00 
			
		
		
		
	Merge pull request #286 from Nik-mmzd/chatmemberupdated
Add MemberUpdatedWatcherBot example utilizing new 18.0.0 extensions
This commit is contained in:
		
							
								
								
									
										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,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() | ||||||
|  | } | ||||||
| @@ -53,3 +53,5 @@ include ":BusinessConnectionsBot" | |||||||
| include ":StarTransactionsBot" | include ":StarTransactionsBot" | ||||||
|  |  | ||||||
| include ":CustomBot" | include ":CustomBot" | ||||||
|  |  | ||||||
|  | include ":MemberUpdatedWatcherBot" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user