|
| 1 | +.. Copyright (c) 2016 Pietro Albini <[email protected]> |
| 2 | + Released under the MIT license |
| 3 | +
|
| 4 | +.. _groups-management: |
| 5 | + |
| 6 | +====================== |
| 7 | +Moderating group chats |
| 8 | +====================== |
| 9 | + |
| 10 | +Group chats are an awesome way to communicate with your circle of friends, but |
| 11 | +you can also use them to discuss with other users in public groups. |
| 12 | +Unfortunately, there are people who just want to spam their own groups or post |
| 13 | +inappropriate content and, without some moderators always online, a public |
| 14 | +group will get messy sooner or later. |
| 15 | + |
| 16 | +Telegram bots are able to moderate groups easily, so you can programmatically |
| 17 | +ban and unban users of the groups your bot is an administrator of. |
| 18 | + |
| 19 | +.. versionadded:: 0.3 |
| 20 | + |
| 21 | +.. _groups-management-preparation: |
| 22 | + |
| 23 | +Allowing your bot to moderate groups |
| 24 | +==================================== |
| 25 | + |
| 26 | +Bots can't moderate groups by default, but they need to be authorized to do so. |
| 27 | +How to authorize them is specific to the client you use, but take the following |
| 28 | +advices: |
| 29 | + |
| 30 | +* If you're trying to add your bot as administrator to a **group**, you need to |
| 31 | + disable the "Everyone is an admin" setting of it, and then explicitly mark |
| 32 | + your bot as administrator. |
| 33 | + |
| 34 | +* If you're trying to add your bot as administrator to a **supergroup**, just |
| 35 | + add it to the administrators list. |
| 36 | + |
| 37 | +Keep in mind your bot might lose its status of administrator during the |
| 38 | +conversion from a group to a supergroup, so if you converted a group check if |
| 39 | +your bot is still an administrator of it. |
| 40 | + |
| 41 | +.. _groups-management-ban: |
| 42 | + |
| 43 | +Banning nasty users |
| 44 | +=================== |
| 45 | + |
| 46 | +Nobody wants users joining a public groups just to spam their own group but, if |
| 47 | +there isn't a moderator always online, a spammer can join in the middle of the |
| 48 | +night, and multiple users might see their message before a moderator gets |
| 49 | +online to ban him. |
| 50 | + |
| 51 | +As an example, with the aid of :py:class:`~botogram.ParsedText` you can detect |
| 52 | +all the posted URLs, and ban all the user who sends a ``telegram.me`` link, |
| 53 | +with :py:meth:`botogram.Chat.ban`. Unfortunately bots can't delete messages |
| 54 | +yet, but they can mention all the administrators of the group, so they can |
| 55 | +erase that message later: |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | + :emphasize-lines: 15,16 |
| 59 | +
|
| 60 | + @bot.process_message |
| 61 | + def ban_telegram_links(chat, message): |
| 62 | + # Don't do anything if there is no text message |
| 63 | + if message.text is None: |
| 64 | + return |
| 65 | +
|
| 66 | + # Check all the links in the message |
| 67 | + for link in message.parsed_text.filter("link"): |
| 68 | + # Match both HTTP and HTTPS links |
| 69 | + if "://telegram.me" in link.url: |
| 70 | + break |
| 71 | + else: |
| 72 | + return |
| 73 | +
|
| 74 | + # Ban the sender of the message |
| 75 | + chat.ban(message.sender) |
| 76 | +
|
| 77 | + # Tell all the admins of the chat to delete the message |
| 78 | + admins = " ".join("@"+a.username for a in chat.admins if a.username) |
| 79 | + if admins: |
| 80 | + message.reply("%s! Please delete this message!" % admins) |
| 81 | + return |
| 82 | +
|
| 83 | +.. _groups-management-unban: |
| 84 | + |
| 85 | +Unbanning users from supergroups |
| 86 | +================================ |
| 87 | + |
| 88 | +If an user is banned from a group chat, he can rejoin the group if he uses a |
| 89 | +join link or it's added by another member. Instead, when an user is banned from |
| 90 | +a supergroup he's put in a blacklist, and he needs to be explicitly removed |
| 91 | +from it if he want to join the supergroup again. |
| 92 | + |
| 93 | +The following example is a working tempban code, which stores when the user was |
| 94 | +banned in :ref:`the shared memory <shared-memory>`, with a :ref:`timer |
| 95 | +<tasks-repeated>` to unban the user later. The actual unban is executed via the |
| 96 | +:py:meth:`botogram.Chat.unban` method: |
| 97 | + |
| 98 | +.. code-block:: python |
| 99 | + :emphasize-lines: 60 |
| 100 | +
|
| 101 | + import time |
| 102 | +
|
| 103 | + # This means `/tempban 1` bans the user for 60 seconds |
| 104 | + BAN_DURATION_MULTIPLIER = 60 |
| 105 | +
|
| 106 | + @bot.prepare_memory |
| 107 | + def prepare_memory(shared): |
| 108 | + shared["bans"] = {} |
| 109 | +
|
| 110 | + @bot.command("tempban") |
| 111 | + def tempban_command(shared, chat, message, args): |
| 112 | + """Tempban an user from the group""" |
| 113 | + # Handle some possible errors |
| 114 | + if message.sender not in chat.admins: |
| 115 | + message.reply("You're not an admin of this chat!") |
| 116 | + return |
| 117 | + if not message.reply_to_message: |
| 118 | + message.reply("You must reply to a message the user sent!") |
| 119 | + return |
| 120 | + if message.reply_to_message.sender in chat.admins: |
| 121 | + message.reply("You can't ban another admin!") |
| 122 | + return |
| 123 | +
|
| 124 | + # Get the exact ban duration |
| 125 | + try: |
| 126 | + length = int(args[0]) |
| 127 | + except (IndexError, TypeError): |
| 128 | + message.reply("You must provide the length of the ban!") |
| 129 | + return |
| 130 | +
|
| 131 | + # Store the ban in the shared memory |
| 132 | + with shared.lock("bans-change"): |
| 133 | + bans = shared["bans"] |
| 134 | + if chat.id not in bans: |
| 135 | + bans[chat.id] = {} |
| 136 | +
|
| 137 | + # Calculate the expiry time and store it |
| 138 | + expiry = time.time()+length*BAN_DURATION_MULTIPLIER |
| 139 | + bans[chat.id][message.reply_to_message.sender.id] = expiry |
| 140 | + shared["bans"] = bans |
| 141 | +
|
| 142 | + # Let's finally ban this guy! |
| 143 | + chat.ban(message.reply_to_message.sender) |
| 144 | +
|
| 145 | + @bot.timer(BAN_DURATION_MULTIPLIER) |
| 146 | + def unban_timer(bot, shared): |
| 147 | + """Unban the users""" |
| 148 | + now = time.time() |
| 149 | +
|
| 150 | + # Everything is locked so no there are no races |
| 151 | + with shared.lock("bans-change"): |
| 152 | + global_bans = shared["bans"] |
| 153 | +
|
| 154 | + # Here .copy() is used because we're changing the dicts in-place |
| 155 | + for chat_id, bans in global_bans.copy().items(): |
| 156 | + for user, expires in bans.copy().items(): |
| 157 | + # Unban the user if his ban expired |
| 158 | + if expires <= now: |
| 159 | + continue |
| 160 | + bot.chat(chat_id).unban(user) |
| 161 | + del global_bans[chat_id][user] |
| 162 | +
|
| 163 | + # Cleaning up is not such a bad thing... |
| 164 | + if not global_bans[chat_id]: |
| 165 | + del global_bans[chat_id] |
| 166 | +
|
| 167 | + shared["bans"] = global_bans |
0 commit comments