mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-08-29 12:26:25 +00:00
update readmes
This commit is contained in:
48
ChatManagementBot/README.md
Normal file
48
ChatManagementBot/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# ChatManagementBot
|
||||
|
||||
This long-polling example demonstrates chat-management features introduced in Telegram Bot API 10.0. It inspects a member's `can_react_to_messages` permission, includes other bot administrators in an administrator query, deletes reactions, and logs content messages received from other bots.
|
||||
|
||||
At startup, the bot prints its name, username, and `canReadAllGroupMessages` value returned by `getMe`. The latter indicates whether Group Privacy Mode is disabled; it does not enable bot-to-bot communication by itself.
|
||||
|
||||
## Commands and triggers
|
||||
|
||||
| Command or trigger | Behavior |
|
||||
| --- | --- |
|
||||
| A member becomes restricted or their restrictions change | Prints the member's new `canReactToMessages` value twice: directly from the restricted member and through the `ChatPermissions` interface. |
|
||||
| `/retrieveRights` | Reply to a user-authored message. The bot calls `getChatMember` for that user and replies with their `canReactToMessages` value. It reports `null` when the returned member state is not restricted. |
|
||||
| `/admins` | In a group, supergroup, or channel, lists the chat administrators. It passes `retrieveOtherBots = true`, the library equivalent of Telegram's `return_bots = true`, so other bot administrators are included. |
|
||||
| `/deleteReaction` | Reply to a user-authored message in a group or supergroup. Removes that user's reaction from the replied-to message. |
|
||||
| `/deleteAllReactions` | Reply to a user-authored message in a group or supergroup. Removes up to 10,000 recent reactions made by that user in the current chat. |
|
||||
| A content message from another bot arrives | Prints the sender and content to standard output; messages from this bot itself are ignored. |
|
||||
|
||||
No command reads positional arguments. The commands that operate on a user take that user from the replied-to message. Command failures from Telegram, including missing permissions, are left to the library's normal error handling.
|
||||
|
||||
This is an API example, not a production moderation bot: it does not check whether the person invoking a reaction-deletion command is an administrator.
|
||||
|
||||
## Telegram setup and permissions
|
||||
|
||||
1. Create a bot with [@BotFather](https://t.me/BotFather) and obtain its token.
|
||||
2. Add the bot to the group or supergroup used for the examples and promote it to administrator. Telegram only sends `chat_member` updates about other members to administrators, and `getChatMember` is only guaranteed to work for other users when the bot is an administrator.
|
||||
3. Grant the bot the **Delete messages** (`can_delete_messages`) administrator right to use either reaction-deletion command.
|
||||
4. To exercise the other-bot message handler, enable **Bot-to-Bot Communication Mode** for the receiving bot in @BotFather. For ordinary messages that are neither an addressed command nor a direct reply, the receiving bot must also be a group administrator and have **Group Privacy Mode** disabled. Re-add the bot after changing Group Privacy Mode so the change takes effect.
|
||||
|
||||
See Telegram's documentation for [`chat_member` updates and chat-management methods](https://core.telegram.org/bots/api) and [bot-to-bot communication](https://core.telegram.org/api/bots%2Fbot-to-bot).
|
||||
|
||||
## Launch
|
||||
|
||||
From the repository root, run:
|
||||
|
||||
```bash
|
||||
./gradlew :ChatManagementBot:run --args="<BOT_TOKEN> [debug] [testServer]"
|
||||
```
|
||||
|
||||
The bot token must be the first argument. The optional, case-sensitive flags may follow it in either order:
|
||||
|
||||
- `debug` sends the library's logging to standard output.
|
||||
- `testServer` uses Telegram's Bot API test environment.
|
||||
|
||||
For example:
|
||||
|
||||
```bash
|
||||
./gradlew :ChatManagementBot:run --args="123456:ABCDEF debug"
|
||||
```
|
||||
@@ -27,27 +27,16 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
/**
|
||||
* This bot demonstrates Chat Management API features added in Bot API 9.x:
|
||||
* Runs a long-polling demonstration of the chat-management features introduced in Telegram Bot API 10.0.
|
||||
*
|
||||
* 1. `can_react_to_messages` field in `ChatMemberRestricted` — printed when a member's
|
||||
* restrictions are changed (requires the bot to be an admin in the group).
|
||||
* `RestrictedMemberChatMember` also implements `ChatPermissions`, so the same field
|
||||
* covers both `ChatMemberRestricted` and `ChatPermissions` from the spec.
|
||||
* The bot logs changes to a restricted member's `canReactToMessages` permission, exposes commands for querying
|
||||
* member rights and administrators, removes a user's reactions, and logs content messages received from other
|
||||
* bots. Reaction deletion requires the bot's `can_delete_messages` administrator right. Receiving unrestricted
|
||||
* bot-authored group messages additionally requires Bot-to-Bot Communication Mode, administrator status, and
|
||||
* disabled Group Privacy Mode; `canReadAllGroupMessages` only reports the last of those settings.
|
||||
*
|
||||
* 2. `return_bots` in `getChatAdministrators` — `/admins` command lists all admins
|
||||
* including other bots (retrieveOtherBots = true).
|
||||
*
|
||||
* 3. `deleteAllMessageReactions` — `/deleteallreactions` in reply to a message removes
|
||||
* all reactions that the replied message's author has left across the entire chat.
|
||||
*
|
||||
* 4. `deleteMessageReaction` — `/deletereaction` in reply to a message removes the
|
||||
* reaction the replied message's author placed on that specific message.
|
||||
*
|
||||
* 5. Seeing messages from other bots in groups — demonstrated via `canReadAllGroupMessages`
|
||||
* from `getMe()`. When true (privacy mode off), the bot receives messages from other bots.
|
||||
* All such messages are logged.
|
||||
*
|
||||
* Usage: pass the bot token as the first argument. Optional: `debug`, `testServer`.
|
||||
* @param args the bot token followed by optional, case-sensitive `debug` and `testServer` flags. `debug` routes
|
||||
* library logs to standard output; `testServer` selects Telegram's Bot API test environment.
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
@@ -70,8 +59,8 @@ suspend fun main(vararg args: String) {
|
||||
val me = getMe()
|
||||
println("Bot: ${me.firstName} (@${me.username?.username})")
|
||||
|
||||
// Feature 5: canReadAllGroupMessages (can_read_all_group_messages) from getMe()
|
||||
// When true, the bot receives messages from other bots in groups (privacy mode off)
|
||||
// canReadAllGroupMessages (can_read_all_group_messages) reports whether Group Privacy Mode is disabled.
|
||||
// Bot-to-bot delivery has additional requirements described in the entry-point KDoc and README.
|
||||
println("canReadAllGroupMessages: ${me.canReadAllGroupMessages}")
|
||||
|
||||
// Feature 1: can_react_to_messages in ChatMemberRestricted and ChatPermissions
|
||||
@@ -136,7 +125,7 @@ suspend fun main(vararg args: String) {
|
||||
}
|
||||
|
||||
// Feature 3: deleteAllMessageReactions
|
||||
// Deletes all reactions that the replied message's author has left in this chat
|
||||
// Deletes up to 10,000 recent reactions that the replied message's author has left in this chat
|
||||
onCommand("deleteAllReactions") { message ->
|
||||
val replied = message.replyTo?.fromUserMessageOrNull() ?: run {
|
||||
reply(message) { +"Reply to a message to clear all reactions of that user in this chat" }
|
||||
@@ -147,8 +136,7 @@ suspend fun main(vararg args: String) {
|
||||
}
|
||||
|
||||
// Feature 5: messages from other bots in groups
|
||||
// Bots with canReadAllGroupMessages=true (privacy mode off) receive messages from other bots.
|
||||
// This handler logs all such messages to demonstrate the feature.
|
||||
// This handler logs bot-authored content messages that Telegram delivers to this bot.
|
||||
onContentMessage(
|
||||
initialFilter = { msg ->
|
||||
val user = msg.fromUserMessageOrNull()?.user
|
||||
|
||||
Reference in New Issue
Block a user