ban_chat_member()
- Client.ban_chat_member()
Ban a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. You must be an administrator in the chat for this to work and must have the appropriate admin rights.
Note
In regular groups (non-supergroups), this method will only work if the “All Members Are Admins” setting is off in the target group. Otherwise members may only be removed by the group’s creator or by the member that added them.
Usable by Users Bots
- Parameters:
chat_id (
int
|str
) – Unique identifier (int) or username (str) of the target chat. You can also use chat public link in form of t.me/<username> (str).user_id (
int
|str
) – Unique identifier (int) or username (str) of the target user. For a contact that exists in your Telegram address book you can use his phone number (str). You can also use user profile link in form of t.me/<username> (str).until_date (
datetime
, optional) – Date when the user will be unbanned. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever. Defaults to epoch (ban forever).revoke_messages (
bool
, optional) – Pass True to delete all messages from the chat for the user that is being removed. If False, the user will be able to see messages in the group that were sent before the user was removed. Always True for supergroups and channels.
- Returns:
Message
|bool
– On success, a service message will be returned (when applicable), otherwise, in case a message object couldn’t be returned, True is returned.
Example
from datetime import datetime, timedelta # Ban chat member forever await app.ban_chat_member(chat_id, user_id) # Ban chat member and automatically unban after 24h await app.ban_chat_member(chat_id, user_id, datetime.now() + timedelta(days=1))