Randomly Join Telegram Channels by Username

  • Share this:

Code introduction


This function uses the Telethon library to randomly join a Telegram channel specified by username.


Technology Stack : Telethon

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from telethon import TelegramClient, errors, events

def random_user_join_channel(client, username):
    """
    This function randomly joins a channel by username.
    """
    try:
        # Try to enter the channel by username
        channel = client.get_entity(username)
        client.join_channel(channel)
        print(f"Successfully joined {username}")
    except errors.UserPrivacyRestrictedError:
        print(f"Unable to join {username}, the user has restricted privacy settings.")
    except errors.FloodWaitError as e:
        print(f"Cannot join {username} at the moment, please try again after {e.seconds} seconds.")
    except Exception as e:
        print(f"An error occurred: {e}")                
              
Tags: