I have telegram channel/group where i add members manually and i remove/kick users manually after one/two/three month. Is there any way to do this task automatically, I mean can we remove/kick user from channel/group automatically or by scheduling?
Any sample python code will help for understanding. Thank you…!
3
Answers
you can schedule any action using
python-telegram-bot
,and can also schedule
kick_chat_member
Read Full Documentation Here
https://python-telegram-bot.readthedocs.io
You can ban a user from a group for a predefined amount of time using kick_chat_member. The function will return
True
if the user is successfully removed.You need to obtain the
chat_id
of the Group chat and theuser_id
of the user to ban: you can do that by intercepting events likenew_chat_members
which provides all the information when a new user is added to the groupThe bot needs to be able to perform this type of operation, so make sure it is an admin or has the
ban user
privilege.Scheduling
Scheduling operations like that are (obviously) not covered by the Telegram BOT API, but it is logic that the application/chatbot should implement.
You need to take care of the following:
A good way to implement tasks in Python is to use Advanced Python Scheduler
Due to Official Telegram Bot API docs, it’s not possible to schedule a banChatMember.
What you can do is to register users in a database including a field called
kick_date
and have cron jobs run a script to check if theirkick_date
is before current time and usebanChatMember
to ban them.I didn’t post any codes since your question;
I’m gonna give you the steps you can take to achieve your goal.
new_chat_members
:You’re gonna iterate through
new_chat_members
and add them to your database along with akick_at
time.Write a script to get your members from the database and check if it’s time to kick them. (
kick_at
<now
) and use banChatMember for that matter.