File size: 1,914 Bytes
ef21e12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
# Replace with your bot token
TOKEN = '7339830560:AAE43s3Uv025zSX-vvu2k4GbWaHr-P9UDTc'
# Welcome handler
def welcome(update: Update, context: CallbackContext):
# Check for new members
new_members = update.message.new_chat_members
if new_members:
for member in new_members:
welcome_message = f"Welcome {member.full_name}! 👋\nEnjoy your time here!"
update.message.reply_text(welcome_message)
# Ban handler
def ban_user(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
user_id = update.message.reply_to_message.from_user.id if update.message.reply_to_message else None
# Only allow admins to use the ban command
if update.message.from_user.id == update.message.chat.admins[0].user.id: # Check if the bot is an admin
if user_id:
context.bot.ban_chat_member(chat_id, user_id)
update.message.reply_text(f"User {user_id} has been banned.")
else:
update.message.reply_text("Please reply to a user's message you want to ban.")
else:
update.message.reply_text("You don't have permission to ban users!")
# Main function
def main():
# Set up the Updater with the bot token
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# Register the welcome handler (for new members)
dp.add_handler(MessageHandler(Filters.status_update.new_chat_members, welcome))
# Register the ban handler (for banning users)
dp.add_handler(CommandHandler('ban', ban_user, pass_args=True, filters=Filters.reply))
# Start polling
updater.start_polling()
# Run the bot until you manually stop it (Ctrl + C)
updater.idle()
if __name__ == '__main__':
main() |