|
| 1 | +import dev.inmo.kslog.common.KSLog |
| 2 | +import dev.inmo.kslog.common.LogLevel |
| 3 | +import dev.inmo.kslog.common.defaultMessageFormatter |
| 4 | +import dev.inmo.kslog.common.setDefaultKSLog |
| 5 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 6 | +import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators |
| 7 | +import dev.inmo.tgbotapi.extensions.api.chat.members.getChatMember |
| 8 | +import dev.inmo.tgbotapi.extensions.api.send.deleteAllUserMessageReactions |
| 9 | +import dev.inmo.tgbotapi.extensions.api.send.deleteUserMessageReaction |
| 10 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 11 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.chatMemberGotRestrictedFilter |
| 12 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.chatMemberGotRestrictionsChangedFilter |
| 13 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 14 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatMemberUpdated |
| 15 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 16 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus |
| 18 | +import dev.inmo.tgbotapi.extensions.utils.fromUserChatMessageOrNull |
| 19 | +import dev.inmo.tgbotapi.extensions.utils.fromUserMessageOrNull |
| 20 | +import dev.inmo.tgbotapi.extensions.utils.publicChatOrNull |
| 21 | +import dev.inmo.tgbotapi.extensions.utils.requireRestrictedChatMember |
| 22 | +import dev.inmo.tgbotapi.extensions.utils.restrictedMemberChatMemberOrNull |
| 23 | +import dev.inmo.tgbotapi.extensions.utils.specialRightsChatMemberOrNull |
| 24 | +import dev.inmo.tgbotapi.types.chat.CommonBot |
| 25 | +import dev.inmo.tgbotapi.types.chat.ChatPermissions |
| 26 | +import kotlinx.coroutines.CoroutineScope |
| 27 | +import kotlinx.coroutines.Dispatchers |
| 28 | + |
| 29 | +/** |
| 30 | + * This bot demonstrates Chat Management API features added in Bot API 9.x: |
| 31 | + * |
| 32 | + * 1. `can_react_to_messages` field in `ChatMemberRestricted` — printed when a member's |
| 33 | + * restrictions are changed (requires the bot to be an admin in the group). |
| 34 | + * `RestrictedMemberChatMember` also implements `ChatPermissions`, so the same field |
| 35 | + * covers both `ChatMemberRestricted` and `ChatPermissions` from the spec. |
| 36 | + * |
| 37 | + * 2. `return_bots` in `getChatAdministrators` — `/admins` command lists all admins |
| 38 | + * including other bots (retrieveOtherBots = true). |
| 39 | + * |
| 40 | + * 3. `deleteAllMessageReactions` — `/deleteallreactions` in reply to a message removes |
| 41 | + * all reactions that the replied message's author has left across the entire chat. |
| 42 | + * |
| 43 | + * 4. `deleteMessageReaction` — `/deletereaction` in reply to a message removes the |
| 44 | + * reaction the replied message's author placed on that specific message. |
| 45 | + * |
| 46 | + * 5. Seeing messages from other bots in groups — demonstrated via `canReadAllGroupMessages` |
| 47 | + * from `getMe()`. When true (privacy mode off), the bot receives messages from other bots. |
| 48 | + * All such messages are logged. |
| 49 | + * |
| 50 | + * Usage: pass the bot token as the first argument. Optional: `debug`, `testServer`. |
| 51 | + */ |
| 52 | +suspend fun main(vararg args: String) { |
| 53 | + val botToken = args.first() |
| 54 | + val isDebug = args.any { it == "debug" } |
| 55 | + val isTestServer = args.any { it == "testServer" } |
| 56 | + |
| 57 | + if (isDebug) { |
| 58 | + setDefaultKSLog( |
| 59 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 60 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 61 | + } |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + telegramBotWithBehaviourAndLongPolling( |
| 66 | + botToken, |
| 67 | + CoroutineScope(Dispatchers.IO), |
| 68 | + testServer = isTestServer |
| 69 | + ) { |
| 70 | + val me = getMe() |
| 71 | + println("Bot: ${me.firstName} (@${me.username?.username})") |
| 72 | + |
| 73 | + // Feature 5: canReadAllGroupMessages (can_read_all_group_messages) from getMe() |
| 74 | + // When true, the bot receives messages from other bots in groups (privacy mode off) |
| 75 | + println("canReadAllGroupMessages: ${me.canReadAllGroupMessages}") |
| 76 | + |
| 77 | + // Feature 1: can_react_to_messages in ChatMemberRestricted and ChatPermissions |
| 78 | + // RestrictedMemberChatMember implements ChatPermissions, so canReactToMessages |
| 79 | + // appears in both types as required by the Telegram Bot API spec |
| 80 | + onChatMemberUpdated( |
| 81 | + initialFilter = chatMemberGotRestrictedFilter + chatMemberGotRestrictionsChangedFilter |
| 82 | + ) { update -> |
| 83 | + val restricted = update.newChatMemberState.restrictedMemberChatMemberOrNull() |
| 84 | + ?: return@onChatMemberUpdated |
| 85 | + println("Restriction update for ${update.member.firstName}:") |
| 86 | + // canReactToMessages as ChatMemberRestricted field |
| 87 | + println(" canReactToMessages (ChatMemberRestricted): ${restricted.canReactToMessages}") |
| 88 | + // same field via ChatPermissions — RestrictedMemberChatMember : ChatPermissions |
| 89 | + val permissions: ChatPermissions = restricted |
| 90 | + println(" canReactToMessages (ChatPermissions): ${permissions.canReactToMessages}") |
| 91 | + } |
| 92 | + |
| 93 | + // Feature 1: can_react_to_messages in ChatMemberRestricted and ChatPermissions |
| 94 | + // RestrictedMemberChatMember implements ChatPermissions, so canReactToMessages |
| 95 | + // appears in both types as required by the Telegram Bot API spec |
| 96 | + onCommand( |
| 97 | + "retrieveRights" |
| 98 | + ) { message -> |
| 99 | + val replyMessage = message.replyTo ?.fromUserChatMessageOrNull() ?: run { |
| 100 | + reply(message) { +"This command works only in groups/supergroups/channels" } |
| 101 | + return@onCommand |
| 102 | + } |
| 103 | + val chatMember = getChatMember(message.chat.id, replyMessage.user.id) |
| 104 | + val chatPermissions = chatMember.restrictedMemberChatMemberOrNull() |
| 105 | + |
| 106 | + val canReactToMessages = chatPermissions ?.canReactToMessages |
| 107 | + reply(message) { +"Can react to messages: $canReactToMessages" } |
| 108 | + } |
| 109 | + |
| 110 | + // Feature 2: return_bots parameter in getChatAdministrators |
| 111 | + // retrieveOtherBots = true corresponds to return_bots = true in the Telegram API |
| 112 | + onCommand("admins") { message -> |
| 113 | + val chat = message.chat.publicChatOrNull() ?: run { |
| 114 | + reply(message) { +"This command works only in groups/supergroups/channels" } |
| 115 | + return@onCommand |
| 116 | + } |
| 117 | + val admins = getChatAdministrators(chat, retrieveOtherBots = true) |
| 118 | + reply(message) { |
| 119 | + +"Administrators (retrieveOtherBots=true, includes bots):\n" |
| 120 | + admins.forEach { admin -> |
| 121 | + val kind = if (admin.user is CommonBot) "bot" else "user" |
| 122 | + +"• ${admin.user.firstName} [$kind]\n" |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Feature 4: deleteMessageReaction |
| 128 | + // Deletes a specific reaction by the replied message's author on that message |
| 129 | + onCommand("deleteReaction") { message -> |
| 130 | + val replied = message.replyTo ?.fromUserChatMessageOrNull() ?: run { |
| 131 | + reply(message) { +"Reply to a message to remove that user's reaction from it" } |
| 132 | + return@onCommand |
| 133 | + } |
| 134 | + deleteUserMessageReaction(replied, replied.user.id) |
| 135 | + reply(message) { +"Deleted reaction by ${replied.user.firstName} on the replied message" } |
| 136 | + } |
| 137 | + |
| 138 | + // Feature 3: deleteAllMessageReactions |
| 139 | + // Deletes all reactions that the replied message's author has left in this chat |
| 140 | + onCommand("deleteAllReactions") { message -> |
| 141 | + val replied = message.replyTo?.fromUserMessageOrNull() ?: run { |
| 142 | + reply(message) { +"Reply to a message to clear all reactions of that user in this chat" } |
| 143 | + return@onCommand |
| 144 | + } |
| 145 | + deleteAllUserMessageReactions(message.chat, replied.user.id) |
| 146 | + reply(message) { +"Deleted all reactions by ${replied.user.firstName} in this chat" } |
| 147 | + } |
| 148 | + |
| 149 | + // Feature 5: messages from other bots in groups |
| 150 | + // Bots with canReadAllGroupMessages=true (privacy mode off) receive messages from other bots. |
| 151 | + // This handler logs all such messages to demonstrate the feature. |
| 152 | + onContentMessage( |
| 153 | + initialFilter = { msg -> |
| 154 | + val user = msg.fromUserMessageOrNull()?.user |
| 155 | + user is CommonBot && user.id != me.id |
| 156 | + } |
| 157 | + ) { message -> |
| 158 | + val sender = message.fromUserMessageOrNull()?.user |
| 159 | + println("Message from other bot received (canReadAllGroupMessages=${me.canReadAllGroupMessages}):") |
| 160 | + println(" sender: ${sender?.firstName} (@${(sender as? CommonBot)?.username?.username})") |
| 161 | + println(" content: ${message.content}") |
| 162 | + } |
| 163 | + }.second.join() |
| 164 | +} |
0 commit comments