Skip to content

Instantly share code, notes, and snippets.

@Natanel-Shitrit
Created November 26, 2022 23:34
Show Gist options
  • Select an option

  • Save Natanel-Shitrit/8b81021e00a09e1cb887cdbc5dac04ad to your computer and use it in GitHub Desktop.

Select an option

Save Natanel-Shitrit/8b81021e00a09e1cb887cdbc5dac04ad to your computer and use it in GitHub Desktop.
Find all common users in multiple channels
import argparse
from typing import List, Tuple
from telethon import functions, types
from telethon.sync import TelegramClient
from telethon.tl.functions.channels import GetParticipantsRequest
def find_users_in_all_groups(telethon_args: Tuple[str, str, str], channels: List[str]):
common_users = set()
with TelegramClient(*telethon_args) as telegram_client:
for channel in channels:
channel_users = get_channel_discussion_group_users(telegram_client, channel)
if len(common_users) == 0:
common_users.update(channel_users)
else:
common_users.intersection_update(channel_users)
print(f'{len(common_users)} common user(s) found in {len(channels)} channels {channels}:')
for index, user in enumerate(common_users):
print(f'[{index}] Username: {user[1]}\n\tName: {user[2]}\n\tTelegram ID: {user[0]}')
def get_channel_discussion_group_users(telegram_client, channel_name):
channel: types.ChannelFull = telegram_client(functions.channels.GetFullChannelRequest(channel_name))
channel_users = set()
for chat in channel.chats:
if chat.broadcast:
continue
for user in telegram_client.iter_participants(chat, filter=types.ChannelParticipantsSearch("")):
channel_users.add((user.id, user.username, f'{user.first_name} {user.last_name}'))
return channel_users
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
dest='session_name',
help='telethon session name'
)
parser.add_argument(
dest='api_id'
)
parser.add_argument(
dest='api_hash'
)
parser.add_argument(
dest='chats',
nargs='+',
help='Names of the chats (should)'
)
return parser.parse_args()
arguments = parse_arguments()
find_users_in_all_groups(
(arguments.session_name, arguments.api_id, arguments.api_hash),
arguments.chats
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment